navidved commited on
Commit
74a71cb
Β·
verified Β·
1 Parent(s): 5421e82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -22
app.py CHANGED
@@ -9,13 +9,12 @@ if not ASR_API_URL or not AUTH_TOKEN:
9
  print("⚠️ ASR_API_URL or AUTH_TOKEN is not set; API calls will fail.")
10
 
11
  # ---------- Core Transcription Function ----------
12
- def transcribe_audio(file_path: str | None): # Added None type hint for clarity on clearing
13
- # Handle case where audio is cleared (input might be None)
14
  if file_path is None:
15
- return "Audio cleared.", "", None # Return default/empty values
16
 
17
  if not ASR_API_URL or not AUTH_TOKEN:
18
- return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", "", file_path # Keep file path on error
19
 
20
  headers = {
21
  "accept": "application/json",
@@ -25,14 +24,13 @@ def transcribe_audio(file_path: str | None): # Added None type hint for clarity
25
  start = time.time()
26
  try:
27
  with open(file_path, "rb") as f:
28
- # Ensure the filename is correctly extracted, especially for temp files
29
  file_name = os.path.basename(file_path)
30
- files = {"file": (file_name, f, "audio/mpeg")} # Use common mpeg, adjust if specific format needed
31
- resp = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120) # Increased timeout just in case
 
32
  except requests.exceptions.Timeout:
33
  return f"❌ Error: Request timed out after 120 seconds.", "", file_path
34
  except Exception as e:
35
- # Provide more specific error context if possible
36
  return f"❌ Error during API call or file handling: {e}", "", file_path
37
 
38
  elapsed = time.time() - start
@@ -40,13 +38,11 @@ def transcribe_audio(file_path: str | None): # Added None type hint for clarity
40
  try:
41
  data = resp.json()
42
  text = data.get("transcription", "No transcription returned.")
43
- # Use the 'time' field from response if available, otherwise use measured elapsed time
44
  processing_time = data.get('time', elapsed)
45
- return text, f"{processing_time:.2f} s", file_path # Return filepath to keep it in the input widget if needed
46
  except requests.exceptions.JSONDecodeError:
47
  return f"❌ Error: Could not decode JSON response. Status: {resp.status_code}, Response: {resp.text}", "", file_path
48
  else:
49
- # Return error details from the response
50
  return f"❌ Error: API returned status {resp.status_code}. Response: {resp.text}", "", file_path
51
 
52
 
@@ -68,7 +64,6 @@ custom_css = f"""
68
 
69
  # ---------- UI ----------
70
  with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
71
- # Optional: Add a title using Markdown or HTML
72
  gr.Markdown("# Gooya ASR v1.4 Transcription", elem_id="gooya-title")
73
  with gr.Row():
74
  with gr.Column():
@@ -81,14 +76,14 @@ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
81
  processing_time_tb = gr.Textbox(
82
  label="⏱️ Processing Time",
83
  interactive=False,
84
- elem_classes="gooya-badge", # Use elem_classes for multiple classes
85
  )
86
  transcription_tb = gr.Textbox(
87
  label="πŸ“ Transcription",
88
  lines=5,
89
  show_copy_button=True,
90
  placeholder="The transcription will appear here...",
91
- elem_id="gooya-textbox", # elem_id should be unique if used
92
  )
93
 
94
  with gr.Row():
@@ -106,25 +101,23 @@ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
106
  )
107
 
108
  # ---------- Callbacks ----------
109
- # Update outputs to potentially include audio_input if you want to keep it on error
110
  btn_transcribe.click(
111
  fn=transcribe_audio,
112
  inputs=[audio_input],
113
- outputs=[transcription_tb, processing_time_tb, audio_input], # Keep audio input displayed
114
  )
115
 
116
- # Clear function
117
  def clear_all():
118
- return "", "", None # Clears transcription, time, and audio input
119
 
120
  btn_clear.click(
121
- fn=clear_all, # Use a named function for clarity
122
  inputs=None,
123
  outputs=[transcription_tb, processing_time_tb, audio_input],
124
  )
125
 
126
  # ---------- Launch ----------
127
  if __name__ == "__main__":
128
- # Set share=True to generate a public link when localhost is not accessible
129
- # This is necessary in environments like Docker containers or cloud platforms (e.g., HF Spaces)
130
- demo.queue().launch(debug=True, share=True) # <-- Changed share=False to share=True
 
9
  print("⚠️ ASR_API_URL or AUTH_TOKEN is not set; API calls will fail.")
10
 
11
  # ---------- Core Transcription Function ----------
12
+ def transcribe_audio(file_path: str | None):
 
13
  if file_path is None:
14
+ return "Audio cleared.", "", None
15
 
16
  if not ASR_API_URL or not AUTH_TOKEN:
17
+ return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", "", file_path
18
 
19
  headers = {
20
  "accept": "application/json",
 
24
  start = time.time()
25
  try:
26
  with open(file_path, "rb") as f:
 
27
  file_name = os.path.basename(file_path)
28
+ # Adjusted mime type slightly - more general
29
+ files = {"file": (file_name, f, "audio/wav")} # Use audio/wav or audio/mpeg as appropriate for your API
30
+ resp = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120)
31
  except requests.exceptions.Timeout:
32
  return f"❌ Error: Request timed out after 120 seconds.", "", file_path
33
  except Exception as e:
 
34
  return f"❌ Error during API call or file handling: {e}", "", file_path
35
 
36
  elapsed = time.time() - start
 
38
  try:
39
  data = resp.json()
40
  text = data.get("transcription", "No transcription returned.")
 
41
  processing_time = data.get('time', elapsed)
42
+ return text, f"{processing_time:.2f} s", file_path
43
  except requests.exceptions.JSONDecodeError:
44
  return f"❌ Error: Could not decode JSON response. Status: {resp.status_code}, Response: {resp.text}", "", file_path
45
  else:
 
46
  return f"❌ Error: API returned status {resp.status_code}. Response: {resp.text}", "", file_path
47
 
48
 
 
64
 
65
  # ---------- UI ----------
66
  with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
 
67
  gr.Markdown("# Gooya ASR v1.4 Transcription", elem_id="gooya-title")
68
  with gr.Row():
69
  with gr.Column():
 
76
  processing_time_tb = gr.Textbox(
77
  label="⏱️ Processing Time",
78
  interactive=False,
79
+ elem_classes="gooya-badge",
80
  )
81
  transcription_tb = gr.Textbox(
82
  label="πŸ“ Transcription",
83
  lines=5,
84
  show_copy_button=True,
85
  placeholder="The transcription will appear here...",
86
+ elem_id="gooya-textbox",
87
  )
88
 
89
  with gr.Row():
 
101
  )
102
 
103
  # ---------- Callbacks ----------
 
104
  btn_transcribe.click(
105
  fn=transcribe_audio,
106
  inputs=[audio_input],
107
+ outputs=[transcription_tb, processing_time_tb, audio_input],
108
  )
109
 
 
110
  def clear_all():
111
+ return "", "", None
112
 
113
  btn_clear.click(
114
+ fn=clear_all,
115
  inputs=None,
116
  outputs=[transcription_tb, processing_time_tb, audio_input],
117
  )
118
 
119
  # ---------- Launch ----------
120
  if __name__ == "__main__":
121
+ # On Hugging Face Spaces, share=True is not needed and causes a warning.
122
+ # debug=True might also cause issues in some restricted environments, remove if necessary.
123
+ demo.queue().launch(debug=False) # <-- Reverted share=True, optionally removed debug=True