Spaces:
Build error
Build error
CB commited on
Update streamlit_app.py
Browse files- streamlit_app.py +3 -31
streamlit_app.py
CHANGED
|
@@ -10,10 +10,11 @@ import yt_dlp
|
|
| 10 |
import ffmpeg
|
| 11 |
import streamlit as st
|
| 12 |
from dotenv import load_dotenv
|
|
|
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
-
#
|
| 17 |
HAS_GENAI = False
|
| 18 |
GENAI = None
|
| 19 |
try:
|
|
@@ -25,22 +26,11 @@ except Exception:
|
|
| 25 |
upload_file = None
|
| 26 |
get_file = None
|
| 27 |
|
| 28 |
-
# Optional phi Agent (useful if phi is installed)
|
| 29 |
-
HAS_PHI_AGENT = False
|
| 30 |
-
try:
|
| 31 |
-
from phi.agent import Agent
|
| 32 |
-
from phi.model.google import Gemini
|
| 33 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
| 34 |
-
HAS_PHI_AGENT = True
|
| 35 |
-
except Exception:
|
| 36 |
-
Agent = Gemini = DuckDuckGo = None
|
| 37 |
-
|
| 38 |
st.set_page_config(page_title="Generate the story of videos:", layout="wide")
|
| 39 |
|
| 40 |
DATA_DIR = Path("./data")
|
| 41 |
DATA_DIR.mkdir(exist_ok=True)
|
| 42 |
|
| 43 |
-
# Session defaults
|
| 44 |
st.session_state.setdefault("videos", "")
|
| 45 |
st.session_state.setdefault("loop_video", False)
|
| 46 |
|
|
@@ -61,7 +51,6 @@ settings_exp.text_input("Video Password", key="video-password", placeholder="Ent
|
|
| 61 |
if not API_KEY:
|
| 62 |
settings_exp.warning("No Google API key provided. Uploading/processing will not work.", icon="⚠️")
|
| 63 |
|
| 64 |
-
# Safety settings (kept OFF by default)
|
| 65 |
safety_settings = [
|
| 66 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "OFF"},
|
| 67 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "OFF"},
|
|
@@ -69,7 +58,6 @@ safety_settings = [
|
|
| 69 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "OFF"},
|
| 70 |
]
|
| 71 |
|
| 72 |
-
# Helpers
|
| 73 |
def sanitize_filename(path_str: str):
|
| 74 |
name = Path(path_str).name
|
| 75 |
name = name.lower().translate(str.maketrans("", "", string.punctuation)).replace(" ", "_")
|
|
@@ -89,7 +77,6 @@ def convert_video_to_mp4(video_path: str) -> str:
|
|
| 89 |
def download_video_ytdlp(url: str, save_dir: str, video_password: str = None) -> str:
|
| 90 |
if not url:
|
| 91 |
raise ValueError("No URL provided")
|
| 92 |
-
# local file
|
| 93 |
if os.path.exists(url) and os.path.isfile(url):
|
| 94 |
return convert_video_to_mp4(url)
|
| 95 |
outtmpl = str(Path(save_dir) / "%(id)s.%(ext)s")
|
|
@@ -106,18 +93,12 @@ def download_video_ytdlp(url: str, save_dir: str, video_password: str = None) ->
|
|
| 106 |
raise FileNotFoundError("Downloaded video not found")
|
| 107 |
return convert_video_to_mp4(matches[0])
|
| 108 |
|
| 109 |
-
# Initialize SDK if available and API key present
|
| 110 |
if API_KEY and HAS_GENAI:
|
| 111 |
try:
|
| 112 |
GENAI.configure(api_key=API_KEY)
|
| 113 |
except Exception:
|
| 114 |
-
|
| 115 |
-
try:
|
| 116 |
-
GENAI.configure(api_key=API_KEY)
|
| 117 |
-
except Exception:
|
| 118 |
-
pass
|
| 119 |
|
| 120 |
-
# Download action
|
| 121 |
if st.sidebar.button("Load Video", use_container_width=True):
|
| 122 |
try:
|
| 123 |
video_password = st.session_state.get("video-password", "")
|
|
@@ -126,7 +107,6 @@ if st.sidebar.button("Load Video", use_container_width=True):
|
|
| 126 |
except Exception as e:
|
| 127 |
st.sidebar.error(f"Failed to load video: {e}")
|
| 128 |
|
| 129 |
-
# Sidebar preview & options
|
| 130 |
if st.session_state["videos"]:
|
| 131 |
try:
|
| 132 |
st.sidebar.video(st.session_state["videos"], loop=st.session_state.get("loop_video", False))
|
|
@@ -159,13 +139,11 @@ if st.session_state["videos"]:
|
|
| 159 |
|
| 160 |
st.sidebar.write("Title:", Path(st.session_state["videos"]).name)
|
| 161 |
|
| 162 |
-
# Upload and polling using SDK if available, otherwise fallback to REST
|
| 163 |
def upload_video(filepath: str):
|
| 164 |
if not API_KEY:
|
| 165 |
raise RuntimeError("No API key provided")
|
| 166 |
if HAS_GENAI and callable(upload_file):
|
| 167 |
return upload_file(filepath)
|
| 168 |
-
# REST fallback (v1beta2 files endpoint)
|
| 169 |
url = "https://generative.googleapis.com/v1beta2/files?uploadType=multipart"
|
| 170 |
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 171 |
metadata = {"mimeType": "video/mp4", "displayName": Path(filepath).name}
|
|
@@ -203,7 +181,6 @@ def poll_file_processed(file_obj, timeout=180):
|
|
| 203 |
raise TimeoutError("File processing timed out")
|
| 204 |
time.sleep(2)
|
| 205 |
|
| 206 |
-
# Use SDK generate when available (rest fallback also present but SDK preferred)
|
| 207 |
def call_model_with_file(prompt_text: str, uploaded_file_obj):
|
| 208 |
if not API_KEY:
|
| 209 |
raise RuntimeError("No API key provided")
|
|
@@ -214,7 +191,6 @@ def call_model_with_file(prompt_text: str, uploaded_file_obj):
|
|
| 214 |
else uploaded_file_obj.get("name") or uploaded_file_obj.get("id")
|
| 215 |
)
|
| 216 |
|
| 217 |
-
# Prefer SDK
|
| 218 |
if HAS_GENAI and hasattr(GENAI, "responses") and hasattr(GENAI.responses, "generate"):
|
| 219 |
request = {
|
| 220 |
"model": f"models/{model_id}",
|
|
@@ -224,7 +200,6 @@ def call_model_with_file(prompt_text: str, uploaded_file_obj):
|
|
| 224 |
}
|
| 225 |
return GENAI.responses.generate(**request)
|
| 226 |
|
| 227 |
-
# REST v1 fallback (use short model id in URL)
|
| 228 |
model_short = model_id.split("/")[-1]
|
| 229 |
endpoint = f"https://generativeapis.googleapis.com/v1/models/{model_short}:generate"
|
| 230 |
body = {
|
|
@@ -233,12 +208,10 @@ def call_model_with_file(prompt_text: str, uploaded_file_obj):
|
|
| 233 |
"maxOutputTokens": 1000,
|
| 234 |
}
|
| 235 |
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
|
| 236 |
-
import requests
|
| 237 |
r = requests.post(endpoint, headers=headers, json=body, timeout=180)
|
| 238 |
r.raise_for_status()
|
| 239 |
return r.json()
|
| 240 |
|
| 241 |
-
# Main generate action
|
| 242 |
if st.button("Generate the story", type="primary"):
|
| 243 |
if not st.session_state.get("videos"):
|
| 244 |
st.error("No video loaded. Use 'Load Video' in the sidebar.")
|
|
@@ -255,7 +228,6 @@ if st.button("Generate the story", type="primary"):
|
|
| 255 |
with st.spinner("Generating description..."):
|
| 256 |
result = call_model_with_file(prompt_text, processed)
|
| 257 |
|
| 258 |
-
# Extract text from common response shapes
|
| 259 |
text_out = ""
|
| 260 |
if isinstance(result, dict):
|
| 261 |
if "output" in result:
|
|
|
|
| 10 |
import ffmpeg
|
| 11 |
import streamlit as st
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
+
import requests
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
+
# Try to import the Google SDK
|
| 18 |
HAS_GENAI = False
|
| 19 |
GENAI = None
|
| 20 |
try:
|
|
|
|
| 26 |
upload_file = None
|
| 27 |
get_file = None
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
st.set_page_config(page_title="Generate the story of videos:", layout="wide")
|
| 30 |
|
| 31 |
DATA_DIR = Path("./data")
|
| 32 |
DATA_DIR.mkdir(exist_ok=True)
|
| 33 |
|
|
|
|
| 34 |
st.session_state.setdefault("videos", "")
|
| 35 |
st.session_state.setdefault("loop_video", False)
|
| 36 |
|
|
|
|
| 51 |
if not API_KEY:
|
| 52 |
settings_exp.warning("No Google API key provided. Uploading/processing will not work.", icon="⚠️")
|
| 53 |
|
|
|
|
| 54 |
safety_settings = [
|
| 55 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "OFF"},
|
| 56 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "OFF"},
|
|
|
|
| 58 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "OFF"},
|
| 59 |
]
|
| 60 |
|
|
|
|
| 61 |
def sanitize_filename(path_str: str):
|
| 62 |
name = Path(path_str).name
|
| 63 |
name = name.lower().translate(str.maketrans("", "", string.punctuation)).replace(" ", "_")
|
|
|
|
| 77 |
def download_video_ytdlp(url: str, save_dir: str, video_password: str = None) -> str:
|
| 78 |
if not url:
|
| 79 |
raise ValueError("No URL provided")
|
|
|
|
| 80 |
if os.path.exists(url) and os.path.isfile(url):
|
| 81 |
return convert_video_to_mp4(url)
|
| 82 |
outtmpl = str(Path(save_dir) / "%(id)s.%(ext)s")
|
|
|
|
| 93 |
raise FileNotFoundError("Downloaded video not found")
|
| 94 |
return convert_video_to_mp4(matches[0])
|
| 95 |
|
|
|
|
| 96 |
if API_KEY and HAS_GENAI:
|
| 97 |
try:
|
| 98 |
GENAI.configure(api_key=API_KEY)
|
| 99 |
except Exception:
|
| 100 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
|
|
|
| 102 |
if st.sidebar.button("Load Video", use_container_width=True):
|
| 103 |
try:
|
| 104 |
video_password = st.session_state.get("video-password", "")
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
st.sidebar.error(f"Failed to load video: {e}")
|
| 109 |
|
|
|
|
| 110 |
if st.session_state["videos"]:
|
| 111 |
try:
|
| 112 |
st.sidebar.video(st.session_state["videos"], loop=st.session_state.get("loop_video", False))
|
|
|
|
| 139 |
|
| 140 |
st.sidebar.write("Title:", Path(st.session_state["videos"]).name)
|
| 141 |
|
|
|
|
| 142 |
def upload_video(filepath: str):
|
| 143 |
if not API_KEY:
|
| 144 |
raise RuntimeError("No API key provided")
|
| 145 |
if HAS_GENAI and callable(upload_file):
|
| 146 |
return upload_file(filepath)
|
|
|
|
| 147 |
url = "https://generative.googleapis.com/v1beta2/files?uploadType=multipart"
|
| 148 |
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 149 |
metadata = {"mimeType": "video/mp4", "displayName": Path(filepath).name}
|
|
|
|
| 181 |
raise TimeoutError("File processing timed out")
|
| 182 |
time.sleep(2)
|
| 183 |
|
|
|
|
| 184 |
def call_model_with_file(prompt_text: str, uploaded_file_obj):
|
| 185 |
if not API_KEY:
|
| 186 |
raise RuntimeError("No API key provided")
|
|
|
|
| 191 |
else uploaded_file_obj.get("name") or uploaded_file_obj.get("id")
|
| 192 |
)
|
| 193 |
|
|
|
|
| 194 |
if HAS_GENAI and hasattr(GENAI, "responses") and hasattr(GENAI.responses, "generate"):
|
| 195 |
request = {
|
| 196 |
"model": f"models/{model_id}",
|
|
|
|
| 200 |
}
|
| 201 |
return GENAI.responses.generate(**request)
|
| 202 |
|
|
|
|
| 203 |
model_short = model_id.split("/")[-1]
|
| 204 |
endpoint = f"https://generativeapis.googleapis.com/v1/models/{model_short}:generate"
|
| 205 |
body = {
|
|
|
|
| 208 |
"maxOutputTokens": 1000,
|
| 209 |
}
|
| 210 |
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
|
|
|
|
| 211 |
r = requests.post(endpoint, headers=headers, json=body, timeout=180)
|
| 212 |
r.raise_for_status()
|
| 213 |
return r.json()
|
| 214 |
|
|
|
|
| 215 |
if st.button("Generate the story", type="primary"):
|
| 216 |
if not st.session_state.get("videos"):
|
| 217 |
st.error("No video loaded. Use 'Load Video' in the sidebar.")
|
|
|
|
| 228 |
with st.spinner("Generating description..."):
|
| 229 |
result = call_model_with_file(prompt_text, processed)
|
| 230 |
|
|
|
|
| 231 |
text_out = ""
|
| 232 |
if isinstance(result, dict):
|
| 233 |
if "output" in result:
|