CoolFace
Apppublic

rull/pyward

sourceHugging Faceupdated 2y agoView on Hugging Face
2likes
app.py70 linesDownload Raw Back to root
1from io import BytesIO2 3from flask import Flask, request, jsonify, render_template, send_file, Response4from bs4 import BeautifulSoup5from scraper import voicevox6import requests7 8app = Flask(__name__)9 10@app.route('/')11def index():12    return '<h1>OK</h1>'13 14@app.route("/voicevox")15def voicevox_resp():16    text = request.args.get("text")17    speaker = request.args.get("speaker")18    if text is None:19        return jsonify({'error': "Please provided parameter 'text'"}), 40020    if speaker is None:21        speaker = 122 23 24    try:25        data = voicevox(text, speaker=speaker)26        io = BytesIO(data)27 28        return send_file(io, as_attachment=True, mimetype="audio/mp3", download_name=f'rull-pyward_voicevox({text.strip()}).mp3')29    except Exception as e:30        return jsonify({'error': "Internal server error"}), 50031 32@app.route('/proxy')33def proxy():34    url = request.args.get('url')35    if not url:36        return jsonify({'error': 'URL parameter is missing'}), 40037    38    try:39        headers = {key: value for (key, value) in request.headers if key != 'Host'}40        cookies = request.cookies41        method = request.method42        data = request.get_data()43 44        response = requests.request(45            method=method,46            url=url,47          #  headers=headers,48          #  cookies=cookies,49          #  data=data,50            allow_redirects=False51        )52        53        print(response.headers.items())54        if response.headers.get('content-type', '').startswith('text/html'):55            56            soup = BeautifulSoup(response.text, 'html.parser')57            for tag in soup.find_all(href=True):58                tag['href'] = '/proxy?url=' + tag['href']59            for tag in soup.find_all(src=True):60                tag['src'] = '/proxy?url=' + tag['src']61            content = soup.prettify(formatter="html")62            return content, response.status_code63        else:64            return Response(response.content, status=response.status_code)65    except requests.RequestException as e:66        return jsonify({'error': str(e)}), 50067 68if __name__ == '__main__':69    app.run(host="0.0.0.0", port=7860)  # Use 'adhoc' to enable HTTPS locally70