Oviya commited on
Commit
abef1d2
·
1 Parent(s): 58309bf
Files changed (1) hide show
  1. Findingword.py +22 -7
Findingword.py CHANGED
@@ -21,9 +21,8 @@ finding_bp = Blueprint("findingword", __name__)
21
 
22
  # Directories for video, audio, and transcripts
23
  VIDEO_FOLDER = 'static/videos'
24
- AUDIO_FOLDER = 'static/audio' # Ensure this folder exists
25
  TRANSCRIPT_FOLDER = 'static/transcripts'
26
- os.makedirs(AUDIO_FOLDER, exist_ok=True)
27
 
28
  # --- OpenAI key handling (same as vocab builder) ---
29
  _OPENAI_API_KEY_FALLBACK = os.getenv("OPENAI_API_KEY", "")
@@ -164,9 +163,16 @@ def generate_audio(word):
164
  # Return only the filename; /static/audio/<filename> will proxy from S3
165
  return filename
166
 
167
- # ---- Local Google TTS path (lazy import; no GCP on HF) ----
168
- os.makedirs(AUDIO_FOLDER, exist_ok=True)
169
- audio_file_path = os.path.join(AUDIO_FOLDER, filename)
 
 
 
 
 
 
 
170
 
171
  if not os.path.exists(audio_file_path):
172
  try:
@@ -249,8 +255,17 @@ def serve_audio(filename):
249
  except (BotoCoreError, ClientError, Exception) as e:
250
  return jsonify({"error": f"S3 fetch failed: {str(e)}"}), 404
251
 
252
- # Local: serve file from disk as before
253
- return send_from_directory(AUDIO_FOLDER, filename)
 
 
 
 
 
 
 
 
 
254
 
255
 
256
  # Run the Flask server (local dev): keep URLs unchanged by registering with empty prefix
 
21
 
22
  # Directories for video, audio, and transcripts
23
  VIDEO_FOLDER = 'static/videos'
24
+ AUDIO_FOLDER = 'static/audio' # used only in local mode
25
  TRANSCRIPT_FOLDER = 'static/transcripts'
 
26
 
27
  # --- OpenAI key handling (same as vocab builder) ---
28
  _OPENAI_API_KEY_FALLBACK = os.getenv("OPENAI_API_KEY", "")
 
163
  # Return only the filename; /static/audio/<filename> will proxy from S3
164
  return filename
165
 
166
+ # ---- Local Google TTS path (lazy import; create dir here only) ----
167
+ audio_dir = AUDIO_FOLDER
168
+ try:
169
+ os.makedirs(audio_dir, exist_ok=True)
170
+ except Exception:
171
+ # Fallback if CWD is restricted
172
+ audio_dir = "/tmp/audio"
173
+ os.makedirs(audio_dir, exist_ok=True)
174
+
175
+ audio_file_path = os.path.join(audio_dir, filename)
176
 
177
  if not os.path.exists(audio_file_path):
178
  try:
 
255
  except (BotoCoreError, ClientError, Exception) as e:
256
  return jsonify({"error": f"S3 fetch failed: {str(e)}"}), 404
257
 
258
+ # Local: serve file from disk as before (with /tmp fallback)
259
+ local_path = os.path.join(AUDIO_FOLDER, filename)
260
+ if os.path.exists(local_path):
261
+ return send_from_directory(AUDIO_FOLDER, filename)
262
+
263
+ alt_dir = "/tmp/audio"
264
+ alt_path = os.path.join(alt_dir, filename)
265
+ if os.path.exists(alt_path):
266
+ return send_from_directory(alt_dir, filename)
267
+
268
+ return jsonify({"error": "File not found"}), 404
269
 
270
 
271
  # Run the Flask server (local dev): keep URLs unchanged by registering with empty prefix