ntdservices commited on
Commit
dbf1685
·
verified ·
1 Parent(s): dd0b027

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -16
app.py CHANGED
@@ -73,6 +73,19 @@ def fetch_sheet_rows_and_pronunciations(retries=5, delay=3):
73
  except Exception as e:
74
  time.sleep(delay)
75
  return [], {}
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  # REPLACE your existing /tts with this
78
  @app.route("/tts", methods=["POST"])
@@ -90,37 +103,48 @@ def tts():
90
  rate = max(-50, min(50, rate))
91
  pitch = max(-20, min(20, pitch))
92
 
93
- # 9k-char chunks to beat ~10-min timeouts
94
- parts = chunk_text(text_input, 9000)
95
 
96
- # your existing HF client base stays the same
97
  client = Client("https://altafo-free-tts-unlimted-words.hf.space/")
98
 
99
  os.makedirs("static", exist_ok=True)
100
  out_name = f"{uuid.uuid4().hex}.mp3"
101
  out_path = os.path.join("static", out_name)
102
 
103
- # single-shot path (no merge)
104
  if len(parts) == 1:
105
- result = client.predict(parts[0], voice, rate, pitch, api_name="/tts_interface")
 
 
 
 
 
106
  tmp_mp3 = result[0]
107
  if not os.path.exists(tmp_mp3):
108
  return jsonify({"error": "TTS failed"}), 500
109
  shutil.copy(tmp_mp3, out_path)
110
  return jsonify({"url": f"/static/{out_name}", "chunks": 1})
111
 
112
- # multi-part: fetch each piece, then concatenate with pydub
113
- combined = None
114
- for i, chunk in enumerate(parts, 1):
115
- result = client.predict(chunk, voice, rate, pitch, api_name="/tts_interface")
116
- tmp_mp3 = result[0]
117
- if not os.path.exists(tmp_mp3):
118
- return jsonify({"error": f"TTS failed on chunk {i}"}), 500
119
- seg = AudioSegment.from_file(tmp_mp3, format="mp3")
120
- combined = seg if combined is None else (combined + seg)
 
 
 
 
 
 
 
 
 
121
 
122
- combined.export(out_path, format="mp3")
123
- return jsonify({"url": f"/static/{out_name}", "chunks": len(parts)})
124
 
125
  # ======= New from index-cleaning app =======
126
  def extract_text_from_pdf(path):
 
73
  except Exception as e:
74
  time.sleep(delay)
75
  return [], {}
76
+
77
+ def call_tts(client, chunk, voice, rate, pitch):
78
+ """Try TTS with retries + fallback signature."""
79
+ for attempt in range(3):
80
+ try:
81
+ try:
82
+ return client.predict(chunk, voice, rate, pitch, api_name="/tts_interface")
83
+ except Exception:
84
+ # Some Spaces only accept (text, voice)
85
+ return client.predict(chunk, voice, api_name="/tts_interface")
86
+ except Exception:
87
+ time.sleep(1.2 * (attempt + 1))
88
+ raise RuntimeError("Upstream TTS failed after retries")
89
 
90
  # REPLACE your existing /tts with this
91
  @app.route("/tts", methods=["POST"])
 
103
  rate = max(-50, min(50, rate))
104
  pitch = max(-20, min(20, pitch))
105
 
106
+ # smaller chunks for stability
107
+ parts = chunk_text(text_input, 1800)
108
 
 
109
  client = Client("https://altafo-free-tts-unlimted-words.hf.space/")
110
 
111
  os.makedirs("static", exist_ok=True)
112
  out_name = f"{uuid.uuid4().hex}.mp3"
113
  out_path = os.path.join("static", out_name)
114
 
115
+ # single-chunk fast path
116
  if len(parts) == 1:
117
+ try:
118
+ result = call_tts(client, parts[0], voice, rate, pitch)
119
+ except Exception as e:
120
+ app.logger.exception("TTS failed")
121
+ return jsonify({"error": str(e)}), 502
122
+
123
  tmp_mp3 = result[0]
124
  if not os.path.exists(tmp_mp3):
125
  return jsonify({"error": "TTS failed"}), 500
126
  shutil.copy(tmp_mp3, out_path)
127
  return jsonify({"url": f"/static/{out_name}", "chunks": 1})
128
 
129
+ # multi-chunk path (note the indentation under `else:`)
130
+ else:
131
+ combined = None
132
+ for i, chunk in enumerate(parts, 1):
133
+ try:
134
+ result = call_tts(client, chunk, voice, rate, pitch)
135
+ except Exception as e:
136
+ app.logger.exception("TTS failed")
137
+ return jsonify({"error": str(e)}), 502
138
+
139
+ tmp_mp3 = result[0]
140
+ seg = AudioSegment.from_file(tmp_mp3, format="mp3")
141
+ combined = seg if combined is None else (combined + seg)
142
+
143
+ time.sleep(0.4) # small pause between chunk requests
144
+
145
+ combined.export(out_path, format="mp3")
146
+ return jsonify({"url": f"/static/{out_name}", "chunks": len(parts)})
147
 
 
 
148
 
149
  # ======= New from index-cleaning app =======
150
  def extract_text_from_pdf(path):