Spaces:
Sleeping
Sleeping
from flask import Flask, request, jsonify, send_from_directory | |
from flask_cors import CORS | |
import os | |
from werkzeug.utils import secure_filename | |
from app_rvc import SoniTranslate # Importuj SoniTranslate z app_rvc.py | |
app = Flask(__name__) | |
CORS(app) | |
UPLOAD_FOLDER = "uploads" | |
OUTPUT_FOLDER = "outputs" # Výstupní složka | |
if not os.path.exists(UPLOAD_FOLDER): | |
os.makedirs(UPLOAD_FOLDER) | |
if not os.path.exists(OUTPUT_FOLDER): | |
os.makedirs(OUTPUT_FOLDER) | |
API_KEY = "MY_SECRET_API_KEY" | |
# Endpoint pro stahování souborů | |
def download_file(filename): | |
return send_from_directory(OUTPUT_FOLDER, filename, as_attachment=True) | |
def process_video(): | |
# Ověření API klíče | |
api_key = request.headers.get("Authorization") | |
if api_key != f"Bearer {API_KEY}": | |
return jsonify({"status": "error", "message": "Invalid API key"}), 403 | |
# Získání vstupních parametrů | |
video_file = request.files.get("video") | |
youtube_url = request.form.get("youtube_url") | |
target_language = request.form.get("target_language") | |
if not target_language: | |
return jsonify({"status": "error", "message": "Missing target language"}), 400 | |
if not video_file and not youtube_url: | |
return jsonify({"status": "error", "message": "Missing video or YouTube URL"}), 400 | |
file_path = None | |
try: | |
# Pokud je nahraný soubor, ulož ho | |
if video_file: | |
filename = secure_filename(video_file.filename) | |
file_path = os.path.join(UPLOAD_FOLDER, filename) | |
video_file.save(file_path) | |
# Spusť překlad pomocí SoniTranslate | |
translator = SoniTranslate(cpu_mode=False) | |
result_files = translator.multilingual_media_conversion( | |
media_file=file_path if video_file else None, | |
link_media=youtube_url if youtube_url else "", | |
target_language=target_language, | |
is_gui=False, | |
) | |
# Vytvoření URL odkazů ke stažení | |
result_urls = [] | |
for result_file in result_files: | |
filename = os.path.basename(result_file) | |
result_urls.append(f"http://{request.host}/downloads/{filename}") | |
return jsonify({"status": "success", "result": result_urls}), 200 | |
except Exception as e: | |
return jsonify({"status": "error", "message": str(e)}), 500 | |
finally: | |
# Smazání dočasného souboru, pokud bylo použito nahrané video | |
if file_path and os.path.exists(file_path): | |
os.remove(file_path) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=5000, debug=True) | |