hareballak commited on
Commit
77e4b34
·
verified ·
1 Parent(s): b5675ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -2
app.py CHANGED
@@ -6,6 +6,7 @@ import torch
6
  import json
7
  import urllib.parse
8
  import soundfile as sf
 
9
 
10
  # Fetch Hugging Face API Token securely from environment variables
11
  HF_API_TOKEN = os.getenv("HF") # This fetches the token securely
@@ -22,12 +23,38 @@ with open("qa_dataset.json", "r", encoding="utf-8") as f:
22
  qa_data = json.load(f)
23
 
24
  # Function to transcribe audio using Whisper
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def transcribe_audio(audio_file):
26
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
 
 
 
 
 
 
27
  with open(audio_file, "rb") as f:
28
  response = requests.post(WHISPER_API_URL, headers=headers, data=f)
29
- print(response.json()) # Add this to log the entire response
30
- return response.json().get("text", "Error: No transcription text returned.")
 
 
 
31
 
32
  # Function to generate TTS audio URL (Google Translate API for Tamil Voice)
33
  def get_tts_audio_url(text, lang="ta"):
 
6
  import json
7
  import urllib.parse
8
  import soundfile as sf
9
+ import time
10
 
11
  # Fetch Hugging Face API Token securely from environment variables
12
  HF_API_TOKEN = os.getenv("HF") # This fetches the token securely
 
23
  qa_data = json.load(f)
24
 
25
  # Function to transcribe audio using Whisper
26
+ def wait_for_model_ready(model_url, headers, timeout=300):
27
+ start_time = time.time()
28
+ while time.time() - start_time < timeout:
29
+ # Send a "dummy" GET request to check status
30
+ response = requests.get(model_url, headers=headers)
31
+ result = response.json()
32
+
33
+ if not ("error" in result and "loading" in result["error"].lower()):
34
+ print("✅ Model is ready!")
35
+ return True
36
+
37
+ print("⏳ Model is still loading, waiting 10 seconds...")
38
+ time.sleep(10)
39
+
40
+ print("❌ Model did not become ready in time.")
41
+ return False # timeout
42
+
43
  def transcribe_audio(audio_file):
44
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
45
+
46
+ # Wait for Whisper model to be ready
47
+ if not wait_for_model_ready(WHISPER_API_URL, headers):
48
+ return "Error: Whisper model did not load in time. Please try again later."
49
+
50
+ # Now send the audio after model is ready
51
  with open(audio_file, "rb") as f:
52
  response = requests.post(WHISPER_API_URL, headers=headers, data=f)
53
+
54
+ result = response.json()
55
+ print(result) # log response
56
+
57
+ return result.get("text", "Error: No transcription text returned.")
58
 
59
  # Function to generate TTS audio URL (Google Translate API for Tamil Voice)
60
  def get_tts_audio_url(text, lang="ta"):