sivakumar330 commited on
Commit
c6a2cab
Β·
verified Β·
1 Parent(s): 710ea89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -1,10 +1,10 @@
1
- from flask import Flask, request, jsonify, send_file
2
  from flask_cors import CORS
3
  import json
4
  import os
5
  import uuid
6
 
7
- # πŸ”Š TTS
8
  from gtts import gTTS
9
 
10
  # 🌍 Optional translator
@@ -24,7 +24,7 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
24
  DATA_PATH = os.path.join(BASE_DIR, "diseases.json")
25
 
26
  with open(DATA_PATH, "r", encoding="utf-8") as f:
27
- disease_data = json.load(f) # EXPECTED: dict
28
 
29
  # -----------------------------
30
  # Disease aliases
@@ -89,10 +89,10 @@ def home():
89
  @app.route("/chat", methods=["GET", "POST"])
90
  def chat():
91
 
92
- # GET β†’ quick test
93
  if request.method == "GET":
94
  return jsonify({
95
- "info": "Use POST",
96
  "example": {
97
  "message": "acne",
98
  "language": "en"
@@ -176,7 +176,7 @@ def chat():
176
  return jsonify({"reply": "Server error"}), 500
177
 
178
  # -----------------------------
179
- # πŸ”Š TEXT TO SPEECH API
180
  # -----------------------------
181
  @app.route("/tts", methods=["POST"])
182
  def tts():
@@ -186,16 +186,28 @@ def tts():
186
  lang = data.get("language", "en")
187
 
188
  if not text:
189
- return jsonify({"error": "No text"}), 400
190
 
191
  if lang not in SUPPORTED_LANGUAGES:
192
  lang = "en"
193
 
194
  filename = f"tts_{uuid.uuid4()}.mp3"
195
- tts = gTTS(text=text, lang=lang)
196
- tts.save(filename)
197
 
198
- return send_file(filename, mimetype="audio/mpeg")
 
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
  except Exception as e:
201
  print("TTS ERROR:", e)
 
1
+ from flask import Flask, request, jsonify, send_file, after_this_request
2
  from flask_cors import CORS
3
  import json
4
  import os
5
  import uuid
6
 
7
+ # πŸ”Š Text To Speech
8
  from gtts import gTTS
9
 
10
  # 🌍 Optional translator
 
24
  DATA_PATH = os.path.join(BASE_DIR, "diseases.json")
25
 
26
  with open(DATA_PATH, "r", encoding="utf-8") as f:
27
+ disease_data = json.load(f) # dict
28
 
29
  # -----------------------------
30
  # Disease aliases
 
89
  @app.route("/chat", methods=["GET", "POST"])
90
  def chat():
91
 
92
+ # GET β†’ simple test
93
  if request.method == "GET":
94
  return jsonify({
95
+ "info": "Use POST method",
96
  "example": {
97
  "message": "acne",
98
  "language": "en"
 
176
  return jsonify({"reply": "Server error"}), 500
177
 
178
  # -----------------------------
179
+ # πŸ”Š TEXT TO SPEECH API (FINAL)
180
  # -----------------------------
181
  @app.route("/tts", methods=["POST"])
182
  def tts():
 
186
  lang = data.get("language", "en")
187
 
188
  if not text:
189
+ return jsonify({"error": "No text provided"}), 400
190
 
191
  if lang not in SUPPORTED_LANGUAGES:
192
  lang = "en"
193
 
194
  filename = f"tts_{uuid.uuid4()}.mp3"
195
+ gTTS(text=text, lang=lang).save(filename)
 
196
 
197
+ # πŸ”₯ delete file after sending
198
+ @after_this_request
199
+ def cleanup(response):
200
+ try:
201
+ os.remove(filename)
202
+ except:
203
+ pass
204
+ return response
205
+
206
+ return send_file(
207
+ filename,
208
+ mimetype="audio/mpeg",
209
+ as_attachment=False # IMPORTANT for browser play
210
+ )
211
 
212
  except Exception as e:
213
  print("TTS ERROR:", e)