CB commited on
Commit
c5e883b
·
verified ·
1 Parent(s): b6bbcb0

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +10 -13
streamlit_app.py CHANGED
@@ -25,7 +25,7 @@ except Exception:
25
 
26
  try:
27
  import google.generativeai as genai
28
- from google.generativeai import upload_file, get_file
29
  GENAI = genai
30
  HAS_GENAI = True
31
  except Exception:
@@ -76,7 +76,6 @@ def convert_video_to_mp4(video_path: str) -> str:
76
  try:
77
  ffmpeg.input(video_path).output(target_path).run(overwrite_output=True, quiet=True)
78
  except Exception as e:
79
- # If ffmpeg binary missing or conversion fails, re-raise with hint
80
  raise RuntimeError(f"ffmpeg conversion failed: {e}")
81
  try:
82
  os.remove(video_path)
@@ -95,7 +94,6 @@ def download_video_ytdlp(url: str, save_dir: str, video_password: str = None) ->
95
  ydl_opts["videopassword"] = video_password
96
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
97
  info = ydl.extract_info(url, download=True)
98
- # Prefer returned id/filename
99
  video_id = info.get("id") if isinstance(info, dict) else None
100
  if video_id:
101
  matches = glob(os.path.join(save_dir, f"{video_id}.*"))
@@ -105,13 +103,11 @@ def download_video_ytdlp(url: str, save_dir: str, video_password: str = None) ->
105
  raise FileNotFoundError("Downloaded video not found")
106
  return convert_video_to_mp4(matches[0])
107
 
108
- # Helper: robustly extract name/id from SDK file object or dict
109
  def file_name_or_id(file_obj):
110
  if file_obj is None:
111
  return None
112
  if isinstance(file_obj, dict):
113
  return file_obj.get("name") or file_obj.get("id")
114
- # some SDKs return an object with .name or .id attributes
115
  return getattr(file_obj, "name", None) or getattr(file_obj, "id", None) or getattr(file_obj, "fileId", None)
116
 
117
  # configure GENAI if available
@@ -208,24 +204,25 @@ if st.button("Generate the story", type="primary"):
208
  response = _agent.run(prompt_text, videos=[processed], safety_settings=safety_settings)
209
  out = getattr(response, "content", None) or getattr(response, "outputText", None) or str(response)
210
  else:
211
- # fallback: call GENAI.responses.generate directly
212
- with st.spinner("Generating description via SDK..."):
213
  fname = file_name_or_id(processed)
214
  if not fname:
215
  raise RuntimeError("Uploaded file missing name/id")
216
 
 
 
217
  request = {
218
- "model": f"models/{model_id}",
219
  "input": [{"text": prompt_text, "files": [{"name": fname}]}],
220
  "safetySettings": safety_settings,
221
  "maxOutputTokens": 1000,
222
  }
223
- res = GENAI.responses.generate(**request)
 
 
224
  out = ""
225
- # basic extraction
226
- for item in (getattr(res, "output", None) or res.get("output", [])):
227
- contents = getattr(item, "content", None) or item.get("content", [])
228
- for c in contents:
229
  ctype = getattr(c, "type", None) or c.get("type")
230
  if ctype in ("output_text", "text"):
231
  out += getattr(c, "text", "") or c.get("text", "")
 
25
 
26
  try:
27
  import google.generativeai as genai
28
+ from google.generativeai import upload_file, get_file, ResponsesClient
29
  GENAI = genai
30
  HAS_GENAI = True
31
  except Exception:
 
76
  try:
77
  ffmpeg.input(video_path).output(target_path).run(overwrite_output=True, quiet=True)
78
  except Exception as e:
 
79
  raise RuntimeError(f"ffmpeg conversion failed: {e}")
80
  try:
81
  os.remove(video_path)
 
94
  ydl_opts["videopassword"] = video_password
95
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
96
  info = ydl.extract_info(url, download=True)
 
97
  video_id = info.get("id") if isinstance(info, dict) else None
98
  if video_id:
99
  matches = glob(os.path.join(save_dir, f"{video_id}.*"))
 
103
  raise FileNotFoundError("Downloaded video not found")
104
  return convert_video_to_mp4(matches[0])
105
 
 
106
  def file_name_or_id(file_obj):
107
  if file_obj is None:
108
  return None
109
  if isinstance(file_obj, dict):
110
  return file_obj.get("name") or file_obj.get("id")
 
111
  return getattr(file_obj, "name", None) or getattr(file_obj, "id", None) or getattr(file_obj, "fileId", None)
112
 
113
  # configure GENAI if available
 
204
  response = _agent.run(prompt_text, videos=[processed], safety_settings=safety_settings)
205
  out = getattr(response, "content", None) or getattr(response, "outputText", None) or str(response)
206
  else:
207
+ with st.spinner("Generating description via Responses client..."):
 
208
  fname = file_name_or_id(processed)
209
  if not fname:
210
  raise RuntimeError("Uploaded file missing name/id")
211
 
212
+ client = ResponsesClient()
213
+ # ResponsesClient.generate expects a request dict; adapt to SDK
214
  request = {
215
+ "model": model_id if not model_id.startswith("models/") else model_id.split("/", 1)[1],
216
  "input": [{"text": prompt_text, "files": [{"name": fname}]}],
217
  "safetySettings": safety_settings,
218
  "maxOutputTokens": 1000,
219
  }
220
+ res = client.generate(**request)
221
+
222
+ # extract text from response
223
  out = ""
224
+ for item in getattr(res, "output", []) or res.get("output", []):
225
+ for c in getattr(item, "content", []) or item.get("content", []):
 
 
226
  ctype = getattr(c, "type", None) or c.get("type")
227
  if ctype in ("output_text", "text"):
228
  out += getattr(c, "text", "") or c.get("text", "")