jomasego commited on
Commit
19947b7
·
1 Parent(s): 4e6ea02

Update Hugging Face Space to output JSON and enhance API documentation

Browse files
Files changed (1) hide show
  1. app.py +71 -14
app.py CHANGED
@@ -93,12 +93,18 @@ def download_video(url_string: str, temp_dir: str) -> str | None:
93
  return None
94
 
95
 
96
- def process_video_input(input_string: str) -> str:
97
  """
98
- Processes the video (from URL or local file path) and returns its transcription status.
99
  """
100
  if not input_string:
101
- return "Error: No video URL or file path provided."
 
 
 
 
 
 
102
 
103
  video_path_to_process = None
104
  created_temp_dir = None # To store path of temp directory if created for download
@@ -116,13 +122,25 @@ def process_video_input(input_string: str) -> str:
116
  # Error message is already printed by download_video or this block
117
  print(f"Failed to download or locate video from URL: {input_string}")
118
  # Cleanup is handled in finally, so just return error
119
- return "Error: Failed to download video from URL."
 
 
 
 
 
 
120
 
121
  elif os.path.exists(input_string):
122
  print(f"Input is a local file path: {input_string}")
123
  video_path_to_process = input_string
124
  else:
125
- return f"Error: Input '{input_string}' is not a valid URL or an existing file path."
 
 
 
 
 
 
126
 
127
  if video_path_to_process:
128
  print(f"Processing video: {video_path_to_process}")
@@ -155,28 +173,63 @@ def process_video_input(input_string: str) -> str:
155
  # the lookup `modal.Function.lookup("whisper-transcriber", "transcribe_video_audio")` is standard.
156
  # If it was deployed as part of the default app, then just "transcribe_video_audio" might work.
157
  # Given the deployment log, the first lookup should be correct.
158
- return "Error: Could not find the deployed Modal function. Please check deployment status and name."
 
 
 
 
 
 
159
 
160
  print("Calling Modal function for transcription...")
161
  # Using .remote() for asynchronous execution, .call() for synchronous
162
  # For Gradio, synchronous (.call()) might be simpler to handle the response directly.
163
  transcription = f.remote(video_bytes_content) # Use .remote() for Modal function call
164
  print(f"Received transcription from Modal: {transcription[:100]}...")
165
- return transcription
 
 
 
 
 
166
  except FileNotFoundError:
167
  print(f"Error: Video file not found at {video_path_to_process} before sending to Modal.")
168
- return f"Error: Video file disappeared before processing."
 
 
 
 
 
 
169
  except modal.Error as e: # Using modal.Error as the base Modal exception
170
  print(f"Modal specific error: {e}")
171
- return f"Error during Modal operation: {str(e)}"
 
 
 
 
 
 
172
  except Exception as e:
173
  print(f"An unexpected error occurred while calling Modal: {e}")
174
  import traceback
175
  traceback.print_exc()
176
- return f"Error: Failed to get transcription. {str(e)}"
 
 
 
 
 
 
177
  else:
178
  # This case should ideally be caught by earlier checks
179
- return "Error: No video available to process after input handling."
 
 
 
 
 
 
180
 
181
  finally:
182
  if created_temp_dir and os.path.exists(created_temp_dir):
@@ -192,10 +245,14 @@ api_interface = gr.Interface(
192
  fn=process_video_input,
193
  inputs=gr.Textbox(label="Video URL or Local File Path for Transcription",
194
  placeholder="Enter YouTube URL, direct video URL (.mp4, .mov, etc.), or local file path..."),
195
- outputs="text",
196
  title="Video Transcription API",
197
- description="Provide a video URL or local file path to get its audio transcription status.",
198
- allow_flagging="never"
 
 
 
 
199
  )
200
 
201
  # Gradio Interface for a simple user-facing demo
 
93
  return None
94
 
95
 
96
+ def process_video_input(input_string: str) -> Dict[str, Any]:
97
  """
98
+ Processes the video (from URL or local file path) and returns its transcription status as a JSON object.
99
  """
100
  if not input_string:
101
+ return {
102
+ "status": "error",
103
+ "error_details": {
104
+ "message": "No video URL or file path provided.",
105
+ "input_received": input_string
106
+ }
107
+ }
108
 
109
  video_path_to_process = None
110
  created_temp_dir = None # To store path of temp directory if created for download
 
122
  # Error message is already printed by download_video or this block
123
  print(f"Failed to download or locate video from URL: {input_string}")
124
  # Cleanup is handled in finally, so just return error
125
+ return {
126
+ "status": "error",
127
+ "error_details": {
128
+ "message": "Failed to download video from URL.",
129
+ "input_received": input_string
130
+ }
131
+ }
132
 
133
  elif os.path.exists(input_string):
134
  print(f"Input is a local file path: {input_string}")
135
  video_path_to_process = input_string
136
  else:
137
+ return {
138
+ "status": "error",
139
+ "error_details": {
140
+ "message": f"Input '{input_string}' is not a valid URL or an existing file path.",
141
+ "input_received": input_string
142
+ }
143
+ }
144
 
145
  if video_path_to_process:
146
  print(f"Processing video: {video_path_to_process}")
 
173
  # the lookup `modal.Function.lookup("whisper-transcriber", "transcribe_video_audio")` is standard.
174
  # If it was deployed as part of the default app, then just "transcribe_video_audio" might work.
175
  # Given the deployment log, the first lookup should be correct.
176
+ return {
177
+ "status": "error",
178
+ "error_details": {
179
+ "message": "Could not find the deployed Modal function. Please check deployment status and name.",
180
+ "modal_function_name": "whisper-transcriber/transcribe_video_audio"
181
+ }
182
+ }
183
 
184
  print("Calling Modal function for transcription...")
185
  # Using .remote() for asynchronous execution, .call() for synchronous
186
  # For Gradio, synchronous (.call()) might be simpler to handle the response directly.
187
  transcription = f.remote(video_bytes_content) # Use .remote() for Modal function call
188
  print(f"Received transcription from Modal: {transcription[:100]}...")
189
+ return {
190
+ "status": "success",
191
+ "data": {
192
+ "transcription": transcription
193
+ }
194
+ }
195
  except FileNotFoundError:
196
  print(f"Error: Video file not found at {video_path_to_process} before sending to Modal.")
197
+ return {
198
+ "status": "error",
199
+ "error_details": {
200
+ "message": "Video file disappeared before processing.",
201
+ "path_attempted": video_path_to_process
202
+ }
203
+ }
204
  except modal.Error as e: # Using modal.Error as the base Modal exception
205
  print(f"Modal specific error: {e}")
206
+ return {
207
+ "status": "error",
208
+ "error_details": {
209
+ "message": f"Error during Modal operation: {str(e)}",
210
+ "exception_type": type(e).__name__
211
+ }
212
+ }
213
  except Exception as e:
214
  print(f"An unexpected error occurred while calling Modal: {e}")
215
  import traceback
216
  traceback.print_exc()
217
+ return {
218
+ "status": "error",
219
+ "error_details": {
220
+ "message": f"Failed to get transcription: {str(e)}",
221
+ "exception_type": type(e).__name__
222
+ }
223
+ }
224
  else:
225
  # This case should ideally be caught by earlier checks
226
+ return {
227
+ "status": "error",
228
+ "error_details": {
229
+ "message": "No video available to process after input handling.",
230
+ "input_received": input_string
231
+ }
232
+ }
233
 
234
  finally:
235
  if created_temp_dir and os.path.exists(created_temp_dir):
 
245
  fn=process_video_input,
246
  inputs=gr.Textbox(label="Video URL or Local File Path for Transcription",
247
  placeholder="Enter YouTube URL, direct video URL (.mp4, .mov, etc.), or local file path..."),
248
+ outputs=gr.JSON(label="API Response"),
249
  title="Video Transcription API",
250
+ description="Provide a video URL or local file path to get its audio transcription. Output is JSON.",
251
+ allow_flagging="never",
252
+ examples=[
253
+ ["https://www.youtube.com/watch?v=dQw4w9WgXcQ"],
254
+ ["https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"]
255
+ ]
256
  )
257
 
258
  # Gradio Interface for a simple user-facing demo