Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,20 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
from
|
4 |
-
import
|
|
|
5 |
import os
|
6 |
-
from datetime import datetime
|
7 |
|
8 |
-
app =
|
9 |
-
|
10 |
|
11 |
-
@app.
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
text = data.get("text", "").strip()
|
16 |
-
|
17 |
-
if not text:
|
18 |
-
return jsonify({"error": "Text is required"}), 400
|
19 |
-
if len(text) > 5000:
|
20 |
-
return jsonify({"error": "Text too long (max 5000 chars)"}), 400
|
21 |
-
|
22 |
-
# Generate audio
|
23 |
-
tts_engine = BilingualTTS()
|
24 |
-
audio_path = asyncio.run(tts_engine.generate_audio(text))
|
25 |
-
|
26 |
-
# Cleanup old files
|
27 |
-
for f in os.listdir(tts_engine.temp_dir):
|
28 |
-
if f != "final_output.mp3":
|
29 |
-
os.remove(os.path.join(tts_engine.temp_dir, f))
|
30 |
-
|
31 |
-
return send_file(
|
32 |
-
audio_path,
|
33 |
-
mimetype="audio/mpeg",
|
34 |
-
as_attachment=True,
|
35 |
-
download_name="speech.mp3"
|
36 |
-
)
|
37 |
-
|
38 |
-
except Exception as e:
|
39 |
-
return jsonify({"error": str(e)}), 500
|
40 |
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from fastapi import FastAPI, Request, Form
|
3 |
+
from fastapi.responses import HTMLResponse, FileResponse
|
4 |
+
from fastapi.staticfiles import StaticFiles
|
5 |
+
from audio import generate_speech
|
6 |
import os
|
|
|
7 |
|
8 |
+
app = FastAPI()
|
9 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
10 |
|
11 |
+
@app.get("/", response_class=HTMLResponse)
|
12 |
+
def index():
|
13 |
+
with open("index.html", "r", encoding="utf-8") as f:
|
14 |
+
return f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
@app.post("/generate", response_class=FileResponse)
|
17 |
+
def generate(text: str = Form(...)):
|
18 |
+
output_path = "static/output.wav"
|
19 |
+
generate_speech(text, output_path)
|
20 |
+
return FileResponse(output_path, media_type="audio/wav", filename="speech.wav")
|