Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +5 -9
streamlit_app.py
CHANGED
|
@@ -21,7 +21,8 @@ import os
|
|
| 21 |
import string
|
| 22 |
import traceback
|
| 23 |
from pathlib import Path
|
| 24 |
-
from typing import
|
|
|
|
| 25 |
|
| 26 |
# ----------------------------------------------------------------------
|
| 27 |
# Third‑party libraries
|
|
@@ -71,7 +72,7 @@ DEFAULT_STATE = {
|
|
| 71 |
"video_path": "",
|
| 72 |
"model_input": DEFAULT_MODEL,
|
| 73 |
"prompt": DEFAULT_PROMPT,
|
| 74 |
-
"api_key": os.getenv("GOOGLE_API_KEY", ""),
|
| 75 |
"video_password": "",
|
| 76 |
"compress_mb": 200,
|
| 77 |
"busy": False,
|
|
@@ -117,7 +118,6 @@ def _convert_to_mp4(src: Path) -> Path:
|
|
| 117 |
except ffmpeg.Error as e:
|
| 118 |
raise RuntimeError(f"ffmpeg conversion failed: {e.stderr.decode()}") from e
|
| 119 |
|
| 120 |
-
# Remove original if conversion succeeded
|
| 121 |
if dst.exists() and dst.stat().st_size > 0:
|
| 122 |
src.unlink()
|
| 123 |
return dst
|
|
@@ -168,13 +168,11 @@ def _download_with_yt_dlp(url: str, dst: Path, password: str = "") -> Path:
|
|
| 168 |
except Exception as e:
|
| 169 |
raise RuntimeError(f"yt‑dlp could not download the URL: {e}") from e
|
| 170 |
|
| 171 |
-
# Predictable filename from yt‑dlp info dict
|
| 172 |
if isinstance(info, dict) and "id" in info:
|
| 173 |
candidate = dst / f"{info['id']}.{info.get('ext', 'mp4')}"
|
| 174 |
if candidate.exists():
|
| 175 |
return _convert_to_mp4(candidate)
|
| 176 |
|
| 177 |
-
# Fallback: newest file in the folder
|
| 178 |
files = list(dst.iterdir())
|
| 179 |
if not files:
|
| 180 |
raise RuntimeError("yt‑dlp did not produce any files.")
|
|
@@ -193,11 +191,9 @@ def download_video(url: str, dst: Path, password: str = "") -> Path:
|
|
| 193 |
"""
|
| 194 |
video_exts = (".mp4", ".mov", ".webm", ".mkv", ".avi", ".flv")
|
| 195 |
|
| 196 |
-
# 1️⃣ Direct file
|
| 197 |
if url.lower().endswith(video_exts):
|
| 198 |
return _download_direct(url, dst)
|
| 199 |
|
| 200 |
-
# 2️⃣ Twitter
|
| 201 |
if "twitter.com" in url and "/status/" in url:
|
| 202 |
tweet_id = url.split("/")[-1].split("?")[0]
|
| 203 |
for tweet in sntwitter.TwitterTweetScraper(tweet_id).get_items():
|
|
@@ -209,7 +205,7 @@ def download_video(url: str, dst: Path, password: str = "") -> Path:
|
|
| 209 |
return download_video(u.expandedUrl, dst)
|
| 210 |
raise RuntimeError("No video found in the tweet.")
|
| 211 |
|
| 212 |
-
#
|
| 213 |
return _download_with_yt_dlp(url, dst, password)
|
| 214 |
|
| 215 |
|
|
@@ -237,7 +233,7 @@ def generate_report(
|
|
| 237 |
return getattr(resp, "text", str(resp))
|
| 238 |
|
| 239 |
|
| 240 |
-
def _strip_prompt_echo(prompt: str, text: str, threshold: float = 0
|
| 241 |
"""Remove the prompt if the model repeats it at the start of *text*."""
|
| 242 |
if not prompt or not text:
|
| 243 |
return text
|
|
|
|
| 21 |
import string
|
| 22 |
import traceback
|
| 23 |
from pathlib import Path
|
| 24 |
+
from typing import Tuple, Optional
|
| 25 |
+
from difflib import SequenceMatcher # <-- needed for prompt‑echo stripping
|
| 26 |
|
| 27 |
# ----------------------------------------------------------------------
|
| 28 |
# Third‑party libraries
|
|
|
|
| 72 |
"video_path": "",
|
| 73 |
"model_input": DEFAULT_MODEL,
|
| 74 |
"prompt": DEFAULT_PROMPT,
|
| 75 |
+
"api_key": os.getenv("GOOGLE_API_KEY", "AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs"),
|
| 76 |
"video_password": "",
|
| 77 |
"compress_mb": 200,
|
| 78 |
"busy": False,
|
|
|
|
| 118 |
except ffmpeg.Error as e:
|
| 119 |
raise RuntimeError(f"ffmpeg conversion failed: {e.stderr.decode()}") from e
|
| 120 |
|
|
|
|
| 121 |
if dst.exists() and dst.stat().st_size > 0:
|
| 122 |
src.unlink()
|
| 123 |
return dst
|
|
|
|
| 168 |
except Exception as e:
|
| 169 |
raise RuntimeError(f"yt‑dlp could not download the URL: {e}") from e
|
| 170 |
|
|
|
|
| 171 |
if isinstance(info, dict) and "id" in info:
|
| 172 |
candidate = dst / f"{info['id']}.{info.get('ext', 'mp4')}"
|
| 173 |
if candidate.exists():
|
| 174 |
return _convert_to_mp4(candidate)
|
| 175 |
|
|
|
|
| 176 |
files = list(dst.iterdir())
|
| 177 |
if not files:
|
| 178 |
raise RuntimeError("yt‑dlp did not produce any files.")
|
|
|
|
| 191 |
"""
|
| 192 |
video_exts = (".mp4", ".mov", ".webm", ".mkv", ".avi", ".flv")
|
| 193 |
|
|
|
|
| 194 |
if url.lower().endswith(video_exts):
|
| 195 |
return _download_direct(url, dst)
|
| 196 |
|
|
|
|
| 197 |
if "twitter.com" in url and "/status/" in url:
|
| 198 |
tweet_id = url.split("/")[-1].split("?")[0]
|
| 199 |
for tweet in sntwitter.TwitterTweetScraper(tweet_id).get_items():
|
|
|
|
| 205 |
return download_video(u.expandedUrl, dst)
|
| 206 |
raise RuntimeError("No video found in the tweet.")
|
| 207 |
|
| 208 |
+
# Fallback to yt‑dlp for any other URL
|
| 209 |
return _download_with_yt_dlp(url, dst, password)
|
| 210 |
|
| 211 |
|
|
|
|
| 233 |
return getattr(resp, "text", str(resp))
|
| 234 |
|
| 235 |
|
| 236 |
+
def _strip_prompt_echo(prompt: str, text: str, threshold: float = 0.68) -> str:
|
| 237 |
"""Remove the prompt if the model repeats it at the start of *text*."""
|
| 238 |
if not prompt or not text:
|
| 239 |
return text
|