Spaces:
Sleeping
Sleeping
CB commited on
Update streamlit_app.py
Browse files- streamlit_app.py +3 -13
streamlit_app.py
CHANGED
|
@@ -13,7 +13,7 @@ from dotenv import load_dotenv
|
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
-
#
|
| 17 |
try:
|
| 18 |
from phi.agent import Agent
|
| 19 |
from phi.model.google import Gemini
|
|
@@ -26,11 +26,10 @@ except Exception:
|
|
| 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:
|
|
|
|
| 32 |
upload_file = get_file = ResponsesClient = None
|
| 33 |
-
GENAI = None
|
| 34 |
HAS_GENAI = False
|
| 35 |
|
| 36 |
st.set_page_config(page_title="Generate the story of videos:", layout="wide")
|
|
@@ -110,16 +109,10 @@ 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 |
-
# 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 |
-
|
| 123 |
except Exception:
|
| 124 |
st.sidebar.warning("Failed to configure google.generativeai with provided API key.")
|
| 125 |
else:
|
|
@@ -184,7 +177,6 @@ def upload_video_sdk(filepath: str):
|
|
| 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)
|
|
@@ -229,7 +221,6 @@ if st.button("Generate the story", type="primary"):
|
|
| 229 |
raise RuntimeError("Uploaded file missing name/id")
|
| 230 |
|
| 231 |
client = ResponsesClient()
|
| 232 |
-
# ResponsesClient.generate expects a request dict; adapt to SDK
|
| 233 |
request = {
|
| 234 |
"model": model_id if not model_id.startswith("models/") else model_id.split("/", 1)[1],
|
| 235 |
"input": [{"text": prompt_text, "files": [{"name": fname}]}],
|
|
@@ -238,7 +229,6 @@ if st.button("Generate the story", type="primary"):
|
|
| 238 |
}
|
| 239 |
res = client.generate(**request)
|
| 240 |
|
| 241 |
-
# extract text from response
|
| 242 |
out = ""
|
| 243 |
for item in getattr(res, "output", []) or res.get("output", []):
|
| 244 |
for c in getattr(item, "content", []) or item.get("content", []):
|
|
|
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
+
# Optional SDKs
|
| 17 |
try:
|
| 18 |
from phi.agent import Agent
|
| 19 |
from phi.model.google import Gemini
|
|
|
|
| 26 |
try:
|
| 27 |
import google.generativeai as genai
|
| 28 |
from google.generativeai import upload_file, get_file, ResponsesClient
|
|
|
|
| 29 |
HAS_GENAI = True
|
| 30 |
except Exception:
|
| 31 |
+
genai = None
|
| 32 |
upload_file = get_file = ResponsesClient = None
|
|
|
|
| 33 |
HAS_GENAI = False
|
| 34 |
|
| 35 |
st.set_page_config(page_title="Generate the story of videos:", layout="wide")
|
|
|
|
| 109 |
return file_obj.get("name") or file_obj.get("id")
|
| 110 |
return getattr(file_obj, "name", None) or getattr(file_obj, "id", None) or getattr(file_obj, "fileId", None)
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
# configure GENAI if available
|
| 113 |
if API_KEY and HAS_GENAI:
|
| 114 |
try:
|
| 115 |
+
genai.configure(api_key=API_KEY)
|
| 116 |
except Exception:
|
| 117 |
st.sidebar.warning("Failed to configure google.generativeai with provided API key.")
|
| 118 |
else:
|
|
|
|
| 177 |
|
| 178 |
def wait_for_processed(file_obj, timeout=180):
|
| 179 |
if not HAS_GENAI or get_file is None:
|
|
|
|
| 180 |
return file_obj
|
| 181 |
start = time.time()
|
| 182 |
name = file_name_or_id(file_obj)
|
|
|
|
| 221 |
raise RuntimeError("Uploaded file missing name/id")
|
| 222 |
|
| 223 |
client = ResponsesClient()
|
|
|
|
| 224 |
request = {
|
| 225 |
"model": model_id if not model_id.startswith("models/") else model_id.split("/", 1)[1],
|
| 226 |
"input": [{"text": prompt_text, "files": [{"name": fname}]}],
|
|
|
|
| 229 |
}
|
| 230 |
res = client.generate(**request)
|
| 231 |
|
|
|
|
| 232 |
out = ""
|
| 233 |
for item in getattr(res, "output", []) or res.get("output", []):
|
| 234 |
for c in getattr(item, "content", []) or item.get("content", []):
|