sreepathi-ravikumar commited on
Commit
f18e3c9
·
verified ·
1 Parent(s): e80998a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -38
app.py CHANGED
@@ -1,42 +1,20 @@
1
- from flask import Flask, request, jsonify, send_file
2
- from flask_cors import CORS
3
- from tts_engine import BilingualTTS
4
- import asyncio
 
5
  import os
6
- from datetime import datetime
7
 
8
- app = Flask(__name__)
9
- CORS(app, resources={r"/tts": {"origins": "*"}})
10
 
11
- @app.route('/tts', methods=['POST'])
12
- def text_to_speech():
13
- try:
14
- data = request.get_json()
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
- if __name__ == "__main__":
42
- app.run(host="0.0.0.0", port=7860)
 
 
 
 
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")