wxcyn commited on
Commit
5bd411f
·
verified ·
1 Parent(s): 06d281d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -32
app.py CHANGED
@@ -21,58 +21,40 @@ def create_client():
21
 
22
  def process_video(video_file, question, password, progress=gr.Progress()):
23
  """Process video by calling the private space API"""
24
- if video_file is None:
25
- raise gr.Error("Please upload a video file")
26
-
27
- if not question.strip():
28
- raise gr.Error("Please enter a question")
29
-
30
- if not password.strip():
31
- raise gr.Error("Please enter the password")
32
-
33
  try:
 
 
 
34
  progress(0.2, desc="Connecting to video analysis service...")
35
  client = create_client()
36
 
37
  progress(0.4, desc="Uploading and processing video...")
38
-
39
- # Add timeout for the API call
40
  try:
41
  result = client.predict(
42
  video_file.name, # video_file path
43
  question, # question
44
  password, # password
45
- api_name="/process_video",
46
- timeout=300 # 5 minute timeout
47
  )
48
- except Exception as api_error:
49
- error_msg = str(api_error)
50
- if "Invalid password" in error_msg:
51
- raise gr.Error("Invalid password. Please check and try again.")
52
- elif "Video must be shorter" in error_msg:
53
  raise gr.Error("Video must be shorter than 2.5 minutes.")
54
- elif "show_error=True" in error_msg:
55
- raise gr.Error("Service temporarily unavailable. Please try again in a few minutes.")
56
  else:
57
- print(f"API Error: {error_msg}") # Log the error
58
- raise gr.Error("Error processing video. Please try again or contact support.")
59
 
60
- # Handle the result
61
  if isinstance(result, tuple) and len(result) == 2:
62
  analysis_text, video_clip = result
63
- if not analysis_text or analysis_text.startswith("Error:"):
64
- raise gr.Error(analysis_text or "Failed to analyze video")
65
  return analysis_text, video_clip
66
  else:
67
- raise gr.Error("Unexpected response format from video service")
68
 
69
- except gr.Error:
70
- raise # Re-raise Gradio errors directly
71
  except Exception as e:
72
- print(f"Unexpected error: {str(e)}") # Log the error
73
- raise gr.Error("An unexpected error occurred. Please try again later.")
74
-
75
-
76
 
77
 
78
  def create_interface():
 
21
 
22
  def process_video(video_file, question, password, progress=gr.Progress()):
23
  """Process video by calling the private space API"""
 
 
 
 
 
 
 
 
 
24
  try:
25
+ if video_file is None:
26
+ raise gr.Error("No video file uploaded.")
27
+
28
  progress(0.2, desc="Connecting to video analysis service...")
29
  client = create_client()
30
 
31
  progress(0.4, desc="Uploading and processing video...")
 
 
32
  try:
33
  result = client.predict(
34
  video_file.name, # video_file path
35
  question, # question
36
  password, # password
37
+ api_name="/process_video"
 
38
  )
39
+ except Exception as e:
40
+ if "Invalid password" in str(e):
41
+ raise gr.Error("Invalid password provided.")
42
+ elif "Video must be shorter" in str(e):
 
43
  raise gr.Error("Video must be shorter than 2.5 minutes.")
 
 
44
  else:
45
+ raise gr.Error(f"Error from video service: {str(e)}")
 
46
 
47
+ # Unpack results
48
  if isinstance(result, tuple) and len(result) == 2:
49
  analysis_text, video_clip = result
 
 
50
  return analysis_text, video_clip
51
  else:
52
+ return str(result), None
53
 
54
+ except gr.Error as e:
55
+ raise e # Re-raise gr.Error directly
56
  except Exception as e:
57
+ raise gr.Error(f"Error processing video: {str(e)}")
 
 
 
58
 
59
 
60
  def create_interface():