Claude commited on
Commit
e4e907e
·
unverified ·
1 Parent(s): c229f06

fix: Remove type parameter from Chatbot (default in Gradio 6)

Browse files

- Remove deprecated type='messages' parameter (now default in Gradio 6)
- Add error handling for list_organizations API call
- Add retries and better error messages for video download
- Add timeout and error handling for ffmpeg audio extraction

Files changed (1) hide show
  1. app.py +47 -28
app.py CHANGED
@@ -43,10 +43,13 @@ def hello(profile: gr.OAuthProfile | None) -> str:
43
  def list_organizations(oauth_token: gr.OAuthToken | None) -> str:
44
  if oauth_token is None:
45
  return ""
46
- org_names = [org["name"] for org in whoami(oauth_token.token)["orgs"]]
47
- if org_names:
48
- return f"You belong to: {', '.join(org_names)}"
49
- return "You don't belong to any organizations."
 
 
 
50
 
51
 
52
  def get_device():
@@ -77,41 +80,57 @@ CHAT_MODEL = "Qwen/Qwen2.5-72B-Instruct"
77
  def download_video(url: str, output_dir: str) -> list[dict]:
78
  """Download video from YouTube URL (video or playlist)."""
79
  ydl_opts = {
80
- "format": "best[height<=720]",
81
  "outtmpl": os.path.join(output_dir, "%(title)s.%(ext)s"),
82
  "quiet": True,
83
  "no_warnings": True,
 
 
84
  }
85
 
86
  downloaded = []
87
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
88
- info = ydl.extract_info(url, download=True)
89
- if "entries" in info:
90
- for entry in info["entries"]:
91
- if entry:
92
- ext = entry.get("ext", "mp4")
93
- downloaded.append({
94
- "title": entry.get("title", "Unknown"),
95
- "path": os.path.join(output_dir, f"{entry['title']}.{ext}"),
96
- "duration": entry.get("duration", 0),
97
- })
98
- else:
99
- ext = info.get("ext", "mp4")
100
- downloaded.append({
101
- "title": info.get("title", "Unknown"),
102
- "path": os.path.join(output_dir, f"{info['title']}.{ext}"),
103
- "duration": info.get("duration", 0),
104
- })
 
 
 
 
 
105
  return downloaded
106
 
107
 
108
  def extract_audio(video_path: str, output_dir: str) -> str:
109
  """Extract audio from video file."""
110
  audio_path = os.path.join(output_dir, "audio.mp3")
111
- subprocess.run([
112
- "ffmpeg", "-i", video_path, "-vn", "-acodec", "libmp3lame",
113
- "-q:a", "2", audio_path, "-y"
114
- ], capture_output=True)
 
 
 
 
 
 
 
 
 
115
  return audio_path
116
 
117
 
@@ -430,7 +449,7 @@ with gr.Blocks() as demo:
430
 
431
  with gr.TabItem("Chat with Videos"):
432
  kb_stats = gr.Markdown()
433
- chatbot = gr.Chatbot(label="Video Chat", type="messages")
434
  chat_input = gr.Textbox(
435
  label="Ask a question about your videos",
436
  placeholder="What did the video say about...?",
 
43
  def list_organizations(oauth_token: gr.OAuthToken | None) -> str:
44
  if oauth_token is None:
45
  return ""
46
+ try:
47
+ org_names = [org["name"] for org in whoami(oauth_token.token)["orgs"]]
48
+ if org_names:
49
+ return f"You belong to: {', '.join(org_names)}"
50
+ return "You don't belong to any organizations."
51
+ except Exception:
52
+ return ""
53
 
54
 
55
  def get_device():
 
80
  def download_video(url: str, output_dir: str) -> list[dict]:
81
  """Download video from YouTube URL (video or playlist)."""
82
  ydl_opts = {
83
+ "format": "best[height<=720]/best",
84
  "outtmpl": os.path.join(output_dir, "%(title)s.%(ext)s"),
85
  "quiet": True,
86
  "no_warnings": True,
87
+ "ignoreerrors": True,
88
+ "retries": 3,
89
  }
90
 
91
  downloaded = []
92
+ try:
93
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
94
+ info = ydl.extract_info(url, download=True)
95
+ if info is None:
96
+ raise ValueError("Could not extract video information")
97
+ if "entries" in info:
98
+ for entry in info["entries"]:
99
+ if entry:
100
+ ext = entry.get("ext", "mp4")
101
+ downloaded.append({
102
+ "title": entry.get("title", "Unknown"),
103
+ "path": os.path.join(output_dir, f"{entry['title']}.{ext}"),
104
+ "duration": entry.get("duration", 0),
105
+ })
106
+ else:
107
+ ext = info.get("ext", "mp4")
108
+ downloaded.append({
109
+ "title": info.get("title", "Unknown"),
110
+ "path": os.path.join(output_dir, f"{info['title']}.{ext}"),
111
+ "duration": info.get("duration", 0),
112
+ })
113
+ except Exception as e:
114
+ raise RuntimeError(f"Failed to download video: {e!s}") from e
115
  return downloaded
116
 
117
 
118
  def extract_audio(video_path: str, output_dir: str) -> str:
119
  """Extract audio from video file."""
120
  audio_path = os.path.join(output_dir, "audio.mp3")
121
+ try:
122
+ result = subprocess.run(
123
+ ["ffmpeg", "-i", video_path, "-vn", "-acodec", "libmp3lame",
124
+ "-q:a", "2", audio_path, "-y"],
125
+ capture_output=True,
126
+ timeout=300,
127
+ )
128
+ if result.returncode != 0 and not os.path.exists(audio_path):
129
+ raise RuntimeError(f"FFmpeg failed: {result.stderr.decode()}")
130
+ except subprocess.TimeoutExpired as e:
131
+ raise RuntimeError("Audio extraction timed out") from e
132
+ except FileNotFoundError as e:
133
+ raise RuntimeError("FFmpeg not found. Please install FFmpeg.") from e
134
  return audio_path
135
 
136
 
 
449
 
450
  with gr.TabItem("Chat with Videos"):
451
  kb_stats = gr.Markdown()
452
+ chatbot = gr.Chatbot(label="Video Chat")
453
  chat_input = gr.Textbox(
454
  label="Ask a question about your videos",
455
  placeholder="What did the video say about...?",