Spaces:
Build error
Build error
CB commited on
Update streamlit_app.py
Browse files- streamlit_app.py +21 -2
streamlit_app.py
CHANGED
|
@@ -29,7 +29,7 @@ try:
|
|
| 29 |
GENAI = genai
|
| 30 |
HAS_GENAI = True
|
| 31 |
except Exception:
|
| 32 |
-
upload_file = get_file = None
|
| 33 |
GENAI = None
|
| 34 |
HAS_GENAI = False
|
| 35 |
|
|
@@ -110,12 +110,21 @@ def file_name_or_id(file_obj):
|
|
| 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
|
| 114 |
if API_KEY and HAS_GENAI:
|
| 115 |
try:
|
| 116 |
GENAI.configure(api_key=API_KEY)
|
| 117 |
except Exception:
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# initialize phi Agent if available (uses Gemini wrapper)
|
| 121 |
_agent = None
|
|
@@ -169,9 +178,14 @@ if st.session_state["videos"]:
|
|
| 169 |
def upload_video_sdk(filepath: str):
|
| 170 |
if not API_KEY:
|
| 171 |
raise RuntimeError("No API key provided")
|
|
|
|
|
|
|
| 172 |
return upload_file(filepath)
|
| 173 |
|
| 174 |
def wait_for_processed(file_obj, timeout=180):
|
|
|
|
|
|
|
|
|
|
| 175 |
start = time.time()
|
| 176 |
name = file_name_or_id(file_obj)
|
| 177 |
if not name:
|
|
@@ -193,6 +207,9 @@ if st.button("Generate the story", type="primary"):
|
|
| 193 |
st.error("Google API key not set.")
|
| 194 |
else:
|
| 195 |
try:
|
|
|
|
|
|
|
|
|
|
| 196 |
with st.spinner("Uploading video..."):
|
| 197 |
uploaded = upload_video_sdk(st.session_state["videos"])
|
| 198 |
processed = wait_for_processed(uploaded)
|
|
@@ -204,6 +221,8 @@ if st.button("Generate the story", type="primary"):
|
|
| 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:
|
|
|
|
| 29 |
GENAI = genai
|
| 30 |
HAS_GENAI = True
|
| 31 |
except Exception:
|
| 32 |
+
upload_file = get_file = ResponsesClient = None
|
| 33 |
GENAI = None
|
| 34 |
HAS_GENAI = False
|
| 35 |
|
|
|
|
| 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 |
+
# Ensure SDK symbols are defined consistently
|
| 114 |
+
if not HAS_GENAI:
|
| 115 |
+
upload_file = None
|
| 116 |
+
get_file = None
|
| 117 |
+
ResponsesClient = None
|
| 118 |
+
|
| 119 |
# configure GENAI if available
|
| 120 |
if API_KEY and HAS_GENAI:
|
| 121 |
try:
|
| 122 |
GENAI.configure(api_key=API_KEY)
|
| 123 |
except Exception:
|
| 124 |
+
st.sidebar.warning("Failed to configure google.generativeai with provided API key.")
|
| 125 |
+
else:
|
| 126 |
+
if API_KEY and not HAS_GENAI:
|
| 127 |
+
st.sidebar.warning("google.generativeai SDK not available in environment.")
|
| 128 |
|
| 129 |
# initialize phi Agent if available (uses Gemini wrapper)
|
| 130 |
_agent = None
|
|
|
|
| 178 |
def upload_video_sdk(filepath: str):
|
| 179 |
if not API_KEY:
|
| 180 |
raise RuntimeError("No API key provided")
|
| 181 |
+
if not HAS_GENAI or upload_file is None:
|
| 182 |
+
raise RuntimeError("google.generativeai SDK not available; cannot upload")
|
| 183 |
return upload_file(filepath)
|
| 184 |
|
| 185 |
def wait_for_processed(file_obj, timeout=180):
|
| 186 |
+
if not HAS_GENAI or get_file is None:
|
| 187 |
+
# If SDK not available, return the original object and let later code handle it.
|
| 188 |
+
return file_obj
|
| 189 |
start = time.time()
|
| 190 |
name = file_name_or_id(file_obj)
|
| 191 |
if not name:
|
|
|
|
| 207 |
st.error("Google API key not set.")
|
| 208 |
else:
|
| 209 |
try:
|
| 210 |
+
if not HAS_GENAI:
|
| 211 |
+
raise RuntimeError("google.generativeai SDK not available; install it and restart the app.")
|
| 212 |
+
|
| 213 |
with st.spinner("Uploading video..."):
|
| 214 |
uploaded = upload_video_sdk(st.session_state["videos"])
|
| 215 |
processed = wait_for_processed(uploaded)
|
|
|
|
| 221 |
response = _agent.run(prompt_text, videos=[processed], safety_settings=safety_settings)
|
| 222 |
out = getattr(response, "content", None) or getattr(response, "outputText", None) or str(response)
|
| 223 |
else:
|
| 224 |
+
if ResponsesClient is None:
|
| 225 |
+
raise RuntimeError("ResponsesClient not available; google.generativeai SDK incomplete.")
|
| 226 |
with st.spinner("Generating description via Responses client..."):
|
| 227 |
fname = file_name_or_id(processed)
|
| 228 |
if not fname:
|