wxcyn commited on
Commit
875cd0d
·
verified ·
1 Parent(s): 9f899f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -75
app.py CHANGED
@@ -164,80 +164,54 @@ from dotenv import load_dotenv
164
  load_dotenv()
165
 
166
  def create_client():
167
- """Create a client connection to the private space"""
168
  hf_token = os.getenv("HF_TOKEN")
169
  if not hf_token:
170
  raise ValueError("HF_TOKEN environment variable not set")
 
171
 
172
- return Client(
173
- "wxcyn/video-analysis",
174
- hf_token=hf_token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  )
176
 
177
- def process_video(video_file, question, password, progress=gr.Progress()):
178
- """Process video by calling the private space API"""
179
- try:
180
- if video_file is None:
181
- raise gr.Error("No video file uploaded.")
182
-
183
- local_file_path = video_file
184
- if not local_file_path or not os.path.exists(local_file_path):
185
- raise gr.Error("Uploaded file could not be accessed.")
186
-
187
- # Show initial progress
188
- progress(0.2, desc="Connecting to video analysis service...")
189
- client = create_client()
190
-
191
- # Convert the local file path to a FileData object
192
- progress(0.4, desc="Uploading and processing video...")
193
- wrapped_file = handle_file(local_file_path)
194
-
195
- # Call the private API with the properly formatted file input
196
- result = client.predict(
197
- video_file=wrapped_file,
198
- question=question,
199
- password=password,
200
- api_name="/process_video"
201
- )
202
 
203
- progress(0.8, desc="Processing results...")
204
-
205
- # Expecting result in the form (analysis_text, video_clip_info)
206
- if isinstance(result, tuple) and len(result) == 2:
207
- analysis_text, video_clip_info = result
208
-
209
- # Check if video_clip_info is a dictionary or a string path
210
- if isinstance(video_clip_info, dict):
211
- # The dict should have a 'name' key pointing to the local path of the returned clip
212
- video_path = video_clip_info.get("name")
213
- if video_path and os.path.exists(video_path):
214
- return analysis_text, video_path
215
- else:
216
- # If no valid video clip path, just return the text
217
- return analysis_text, None
218
- else:
219
- # If it's a string and that path exists, return it directly
220
- if isinstance(video_clip_info, str) and os.path.exists(video_clip_info):
221
- return analysis_text, video_clip_info
222
-
223
- # Otherwise, no video clip
224
- return analysis_text, None
225
- else:
226
- # If result doesn't match the expected structure, just return what we got
227
- return str(result), None
228
-
229
- except gr.Error as e:
230
- # Known Gradio errors are just re-raised
231
- raise e
232
- except Exception as e:
233
- # Unexpected errors are wrapped in a gr.Error
234
- raise gr.Error(f"Error processing video: {str(e)}")
235
 
236
  def create_interface():
237
- """Create the Gradio interface"""
238
  with gr.Blocks(title="Public Video Analysis Interface") as interface:
239
- gr.Markdown("# Video Content Analysis (1 file under 2.5 minutes)")
240
-
241
  with gr.Row():
242
  with gr.Column(scale=2):
243
  password = gr.Textbox(
@@ -246,7 +220,6 @@ def create_interface():
246
  info="Enter your access password",
247
  interactive=True
248
  )
249
- # Use "filepath" so that `video_file` is a local path
250
  video_file = gr.File(
251
  label="Upload Video File (up to 30MB)",
252
  file_types=['video'],
@@ -266,10 +239,7 @@ def create_interface():
266
  lines=15,
267
  interactive=False
268
  )
269
- video_output = gr.Video(
270
- label="Relevant Video Clip",
271
- interactive=False
272
- )
273
 
274
  submit_btn.click(
275
  fn=process_video,
@@ -280,16 +250,13 @@ def create_interface():
280
  return interface
281
 
282
  if __name__ == "__main__":
283
- # Create required directories
284
  Path('data').mkdir(exist_ok=True)
285
  Path('temp').mkdir(exist_ok=True)
286
-
287
- # Create and launch interface
288
  interface = create_interface()
289
- interface.queue() # Enable queuing for concurrency
290
  interface.launch(
291
  server_name="0.0.0.0",
292
  server_port=7860,
293
- show_error=True, # Enable verbose error reporting
294
- max_threads=140 # Increased thread limit
295
  )
 
164
  load_dotenv()
165
 
166
  def create_client():
 
167
  hf_token = os.getenv("HF_TOKEN")
168
  if not hf_token:
169
  raise ValueError("HF_TOKEN environment variable not set")
170
+ return Client("wxcyn/video-analysis", hf_token=hf_token)
171
 
172
+ def process_video(video_file, question, password, progress=gr.Progress()):
173
+ if video_file is None:
174
+ raise gr.Error("No video file uploaded.")
175
+
176
+ local_file_path = video_file
177
+ if not os.path.exists(local_file_path):
178
+ raise gr.Error("Uploaded file could not be accessed.")
179
+
180
+ progress(0.2, desc="Connecting to video analysis service...")
181
+ client = create_client()
182
+
183
+ progress(0.4, desc="Uploading and processing video...")
184
+ wrapped_file = handle_file(local_file_path) # Convert to FileData
185
+
186
+ result = client.predict(
187
+ video_file=wrapped_file,
188
+ question=question,
189
+ password=password,
190
+ api_name="/process_video"
191
  )
192
 
193
+ progress(0.8, desc="Processing results...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
+ if isinstance(result, tuple) and len(result) == 2:
196
+ analysis_text, video_clip_info = result
197
+
198
+ # If video_clip_info is a dict with 'name', it's a FileData dict
199
+ if isinstance(video_clip_info, dict) and "name" in video_clip_info:
200
+ video_path = video_clip_info["name"]
201
+ if os.path.exists(video_path):
202
+ return analysis_text, video_path
203
+ # If video_clip_info is a string path, check and return it directly
204
+ elif isinstance(video_clip_info, str) and os.path.exists(video_clip_info):
205
+ return analysis_text, video_clip_info
206
+
207
+ # No valid video clip found
208
+ return analysis_text, None
209
+ else:
210
+ return str(result), None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
 
212
  def create_interface():
 
213
  with gr.Blocks(title="Public Video Analysis Interface") as interface:
214
+ gr.Markdown("# Public Video Content Analysis")
 
215
  with gr.Row():
216
  with gr.Column(scale=2):
217
  password = gr.Textbox(
 
220
  info="Enter your access password",
221
  interactive=True
222
  )
 
223
  video_file = gr.File(
224
  label="Upload Video File (up to 30MB)",
225
  file_types=['video'],
 
239
  lines=15,
240
  interactive=False
241
  )
242
+ video_output = gr.Video(label="Relevant Video Clip", interactive=False)
 
 
 
243
 
244
  submit_btn.click(
245
  fn=process_video,
 
250
  return interface
251
 
252
  if __name__ == "__main__":
 
253
  Path('data').mkdir(exist_ok=True)
254
  Path('temp').mkdir(exist_ok=True)
 
 
255
  interface = create_interface()
256
+ interface.queue()
257
  interface.launch(
258
  server_name="0.0.0.0",
259
  server_port=7860,
260
+ show_error=True,
261
+ max_threads=140
262
  )