Spaces:
Sleeping
Sleeping
mrsu0994
commited on
Commit
·
391bf91
1
Parent(s):
6238ebf
upload f5-tts source
Browse files- app.py +12 -7
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
from flask import Flask, request, send_file
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
-
from
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
|
@@ -12,10 +12,12 @@ app = Flask(__name__)
|
|
| 12 |
def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2, vocoder_name="vocos"):
|
| 13 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
infer_cli_path = os.path.join(current_dir, "src", "f5_tts", "infer", "infer_cli.py")
|
| 15 |
-
vocab_file = os.path.join(current_dir, "F5-TTS-MRSU", "vocab.txt")
|
| 16 |
-
ckpt_file = os.path.join(current_dir, "F5-TTS-MRSU", "model_last.pt")
|
| 17 |
tests_dir = os.path.join(current_dir, "tests")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
| 20 |
|
| 21 |
command = [
|
|
@@ -43,7 +45,9 @@ def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2
|
|
| 43 |
if os.path.exists(tests_dir):
|
| 44 |
wav_files = [f for f in os.listdir(tests_dir) if f.endswith('.wav')]
|
| 45 |
if wav_files:
|
| 46 |
-
latest_wav = max(
|
|
|
|
|
|
|
| 47 |
output_file = os.path.join(tests_dir, latest_wav)
|
| 48 |
return True, output_file
|
| 49 |
|
|
@@ -53,14 +57,15 @@ def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2
|
|
| 53 |
except Exception as e:
|
| 54 |
return False, str(e)
|
| 55 |
|
|
|
|
| 56 |
# =========================
|
| 57 |
# Routes
|
| 58 |
# =========================
|
| 59 |
@app.route('/')
|
| 60 |
def home():
|
| 61 |
-
# Bạn có thể tạo index.html trong /templates nếu muốn giao diện
|
| 62 |
return "F5-TTS API is running. Use POST /api/generate to generate audio."
|
| 63 |
|
|
|
|
| 64 |
@app.route('/api/generate', methods=['POST'])
|
| 65 |
def generate_speech():
|
| 66 |
if 'ref_audio' not in request.files:
|
|
@@ -82,10 +87,10 @@ def generate_speech():
|
|
| 82 |
else:
|
| 83 |
return {"error": result}, 500
|
| 84 |
|
|
|
|
| 85 |
# =========================
|
| 86 |
# Main
|
| 87 |
# =========================
|
| 88 |
if __name__ == "__main__":
|
| 89 |
-
# Hugging Face Spaces tự set PORT
|
| 90 |
port = int(os.environ.get("PORT", 7860))
|
| 91 |
app.run(host="0.0.0.0", port=port, debug=False)
|
|
|
|
| 1 |
+
from flask import Flask, request, send_file
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
|
|
|
| 12 |
def run_f5_tts(ref_audio_path, ref_text, gen_text, model="F5TTS_Base", speed=1.2, vocoder_name="vocos"):
|
| 13 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
infer_cli_path = os.path.join(current_dir, "src", "f5_tts", "infer", "infer_cli.py")
|
|
|
|
|
|
|
| 15 |
tests_dir = os.path.join(current_dir, "tests")
|
| 16 |
|
| 17 |
+
# Dùng huggingface_hub để tải file model và vocab từ repo 'nguyensu27/TTS'
|
| 18 |
+
vocab_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="vocab.txt")
|
| 19 |
+
ckpt_file = hf_hub_download(repo_id="nguyensu27/TTS", filename="model_last.pt")
|
| 20 |
+
|
| 21 |
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
| 22 |
|
| 23 |
command = [
|
|
|
|
| 45 |
if os.path.exists(tests_dir):
|
| 46 |
wav_files = [f for f in os.listdir(tests_dir) if f.endswith('.wav')]
|
| 47 |
if wav_files:
|
| 48 |
+
latest_wav = max(
|
| 49 |
+
wav_files, key=lambda x: os.path.getmtime(os.path.join(tests_dir, x))
|
| 50 |
+
)
|
| 51 |
output_file = os.path.join(tests_dir, latest_wav)
|
| 52 |
return True, output_file
|
| 53 |
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
return False, str(e)
|
| 59 |
|
| 60 |
+
|
| 61 |
# =========================
|
| 62 |
# Routes
|
| 63 |
# =========================
|
| 64 |
@app.route('/')
|
| 65 |
def home():
|
|
|
|
| 66 |
return "F5-TTS API is running. Use POST /api/generate to generate audio."
|
| 67 |
|
| 68 |
+
|
| 69 |
@app.route('/api/generate', methods=['POST'])
|
| 70 |
def generate_speech():
|
| 71 |
if 'ref_audio' not in request.files:
|
|
|
|
| 87 |
else:
|
| 88 |
return {"error": result}, 500
|
| 89 |
|
| 90 |
+
|
| 91 |
# =========================
|
| 92 |
# Main
|
| 93 |
# =========================
|
| 94 |
if __name__ == "__main__":
|
|
|
|
| 95 |
port = int(os.environ.get("PORT", 7860))
|
| 96 |
app.run(host="0.0.0.0", port=port, debug=False)
|
requirements.txt
CHANGED
|
@@ -39,4 +39,5 @@ tqdm
|
|
| 39 |
transformers>=4.40
|
| 40 |
uvicorn
|
| 41 |
vocos
|
| 42 |
-
x-transformers
|
|
|
|
|
|
| 39 |
transformers>=4.40
|
| 40 |
uvicorn
|
| 41 |
vocos
|
| 42 |
+
x-transformers
|
| 43 |
+
huggingface_hub
|