Kumaria commited on
Commit
28752c0
Β·
verified Β·
1 Parent(s): 6ad043b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -38
app.py CHANGED
@@ -5,11 +5,8 @@ import requests
5
 
6
  HF_TOKEN = os.environ.get("HF_TOKEN")
7
 
8
- MODELS = {
9
- "whisper-large-v3": "https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3",
10
- "whisper-medium": "https://router.huggingface.co/hf-inference/models/openai/whisper-medium",
11
- "whisper-base": "https://router.huggingface.co/hf-inference/models/openai/whisper-base",
12
- }
13
 
14
  CONTENT_TYPES = {
15
  ".wav": "audio/wav",
@@ -22,7 +19,7 @@ CONTENT_TYPES = {
22
  ".amr": "audio/AMR",
23
  }
24
 
25
- def transcribe(audio_file, model_choice):
26
  if audio_file is None:
27
  return "Please upload or record an audio file."
28
 
@@ -31,31 +28,26 @@ def transcribe(audio_file, model_choice):
31
 
32
  ext = os.path.splitext(audio_file)[-1].lower()
33
  content_type = CONTENT_TYPES.get(ext, "audio/wav")
34
- api_url = MODELS[model_choice]
35
 
36
  with open(audio_file, "rb") as f:
37
  audio_bytes = f.read()
38
 
39
  print(f"File: {audio_file} | Ext: {ext} | Content-Type: {content_type} | Size: {len(audio_bytes)} bytes")
40
 
41
- max_retries = 5
42
- retry_delay = 20 # seconds between retries
43
-
44
- for attempt in range(1, max_retries + 1):
45
  try:
46
- print(f"Attempt {attempt}/{max_retries}...")
47
-
48
  response = requests.post(
49
- api_url,
50
  headers={
51
  "Authorization": f"Bearer {HF_TOKEN}",
52
  "Content-Type": content_type,
53
  },
54
  data=audio_bytes,
55
- timeout=120, # 2 min timeout per request
56
  )
57
 
58
- print(f"Status: {response.status_code}")
59
 
60
  if response.status_code == 200:
61
  result = response.json()
@@ -64,42 +56,34 @@ def transcribe(audio_file, model_choice):
64
  return str(result)
65
 
66
  elif response.status_code in (503, 504):
67
- # Model loading or gateway timeout β€” wait and retry
68
- if attempt < max_retries:
69
- print(f"Model not ready (HTTP {response.status_code}), retrying in {retry_delay}s...")
70
- time.sleep(retry_delay)
71
- continue
72
  else:
73
- return f"⏳ Model is still loading after {max_retries} attempts. Please try again in a minute."
 
 
 
74
 
75
  else:
76
  return f"❌ Error {response.status_code}: {response.text[:300]}"
77
 
78
  except requests.exceptions.Timeout:
79
- if attempt < max_retries:
80
- print(f"Request timed out, retrying in {retry_delay}s...")
81
- time.sleep(retry_delay)
82
  else:
83
- return "❌ Request timed out after multiple attempts. Try a smaller model like whisper-base."
84
 
85
  except Exception as e:
86
  return f"❌ {type(e).__name__}: {str(e)}"
87
 
88
- return "❌ All retry attempts failed."
89
 
90
 
91
  with gr.Blocks() as demo:
92
- with gr.Sidebar():
93
- gr.Markdown("### βš™οΈ Settings")
94
- gr.Markdown("πŸ’‘ **Tip:** Use `whisper-base` for faster cold starts.")
95
- model_choice = gr.Dropdown(
96
- choices=list(MODELS.keys()),
97
- value="whisper-base", # fastest to cold-start
98
- label="Whisper Model",
99
- )
100
-
101
  gr.Markdown("# 🎀 Whisper Audio Transcription")
102
- gr.Markdown("Upload or record audio to get an instant transcript. First request may take ~1 min to warm up.")
103
 
104
  with gr.Row():
105
  with gr.Column():
@@ -119,7 +103,7 @@ with gr.Blocks() as demo:
119
 
120
  transcribe_btn.click(
121
  fn=transcribe,
122
- inputs=[audio_input, model_choice],
123
  outputs=transcript_output,
124
  )
125
 
 
5
 
6
  HF_TOKEN = os.environ.get("HF_TOKEN")
7
 
8
+ # Only confirmed working free model on hf-inference router
9
+ API_URL = "https://router.huggingface.co/hf-inference/models/openai/whisper-large-v3"
 
 
 
10
 
11
  CONTENT_TYPES = {
12
  ".wav": "audio/wav",
 
19
  ".amr": "audio/AMR",
20
  }
21
 
22
+ def transcribe(audio_file):
23
  if audio_file is None:
24
  return "Please upload or record an audio file."
25
 
 
28
 
29
  ext = os.path.splitext(audio_file)[-1].lower()
30
  content_type = CONTENT_TYPES.get(ext, "audio/wav")
 
31
 
32
  with open(audio_file, "rb") as f:
33
  audio_bytes = f.read()
34
 
35
  print(f"File: {audio_file} | Ext: {ext} | Content-Type: {content_type} | Size: {len(audio_bytes)} bytes")
36
 
37
+ for attempt in range(1, 6):
 
 
 
38
  try:
39
+ print(f"Attempt {attempt}/5...")
 
40
  response = requests.post(
41
+ API_URL,
42
  headers={
43
  "Authorization": f"Bearer {HF_TOKEN}",
44
  "Content-Type": content_type,
45
  },
46
  data=audio_bytes,
47
+ timeout=120,
48
  )
49
 
50
+ print(f"Status: {response.status_code} | Body: {response.text[:200]}")
51
 
52
  if response.status_code == 200:
53
  result = response.json()
 
56
  return str(result)
57
 
58
  elif response.status_code in (503, 504):
59
+ if attempt < 5:
60
+ print(f"Model warming up, retrying in 20s...")
61
+ time.sleep(20)
 
 
62
  else:
63
+ return "⏳ Model still loading after 5 attempts. Please try again in a minute."
64
+
65
+ elif response.status_code == 429:
66
+ return "⚠️ Rate limit hit. Please wait a moment and try again."
67
 
68
  else:
69
  return f"❌ Error {response.status_code}: {response.text[:300]}"
70
 
71
  except requests.exceptions.Timeout:
72
+ if attempt < 5:
73
+ print("Timeout, retrying in 20s...")
74
+ time.sleep(20)
75
  else:
76
+ return "❌ Request timed out repeatedly. Try a shorter audio clip."
77
 
78
  except Exception as e:
79
  return f"❌ {type(e).__name__}: {str(e)}"
80
 
81
+ return "❌ All attempts failed."
82
 
83
 
84
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
85
  gr.Markdown("# 🎀 Whisper Audio Transcription")
86
+ gr.Markdown("Using `openai/whisper-large-v3` via HF free inference. First request may take ~30s to warm up.")
87
 
88
  with gr.Row():
89
  with gr.Column():
 
103
 
104
  transcribe_btn.click(
105
  fn=transcribe,
106
+ inputs=[audio_input],
107
  outputs=transcript_output,
108
  )
109