Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +49 -9
streamlit_app.py
CHANGED
|
@@ -194,11 +194,15 @@ def generate_report(
|
|
| 194 |
model_id: str,
|
| 195 |
timeout: int = 300,
|
| 196 |
) -> str:
|
| 197 |
-
#
|
|
|
|
|
|
|
| 198 |
b64 = _encode_video_b64(video_path)
|
| 199 |
video_part = {"inline_data": {"mime_type": "video/mp4", "data": b64}}
|
| 200 |
|
| 201 |
-
#
|
|
|
|
|
|
|
| 202 |
safety_settings = [
|
| 203 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 204 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
|
@@ -206,22 +210,58 @@ def generate_report(
|
|
| 206 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 207 |
]
|
| 208 |
|
| 209 |
-
#
|
|
|
|
|
|
|
| 210 |
model = genai.GenerativeModel(
|
| 211 |
model_name=model_id,
|
| 212 |
safety_settings=safety_settings,
|
| 213 |
)
|
| 214 |
|
| 215 |
-
#
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
# Send the request
|
| 219 |
resp = model.generate_content(
|
| 220 |
[prompt, video_part],
|
| 221 |
-
generation_config=
|
| 222 |
request_options={"timeout": timeout},
|
| 223 |
)
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
|
| 227 |
def _strip_prompt_echo(prompt: str, text: str, threshold: float = 0.68) -> str:
|
|
|
|
| 194 |
model_id: str,
|
| 195 |
timeout: int = 300,
|
| 196 |
) -> str:
|
| 197 |
+
# --------------------------------------------------------------
|
| 198 |
+
# 1️⃣ Encode the video as base‑64
|
| 199 |
+
# --------------------------------------------------------------
|
| 200 |
b64 = _encode_video_b64(video_path)
|
| 201 |
video_part = {"inline_data": {"mime_type": "video/mp4", "data": b64}}
|
| 202 |
|
| 203 |
+
# --------------------------------------------------------------
|
| 204 |
+
# 2️⃣ Safety settings – keep BLOCK_NONE (as you requested)
|
| 205 |
+
# --------------------------------------------------------------
|
| 206 |
safety_settings = [
|
| 207 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 208 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
|
|
|
| 210 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 211 |
]
|
| 212 |
|
| 213 |
+
# --------------------------------------------------------------
|
| 214 |
+
# 3️⃣ Build the model with those safety settings
|
| 215 |
+
# --------------------------------------------------------------
|
| 216 |
model = genai.GenerativeModel(
|
| 217 |
model_name=model_id,
|
| 218 |
safety_settings=safety_settings,
|
| 219 |
)
|
| 220 |
|
| 221 |
+
# --------------------------------------------------------------
|
| 222 |
+
# 4️⃣ Send the request (generation_config now contains only token limits)
|
| 223 |
+
# --------------------------------------------------------------
|
|
|
|
| 224 |
resp = model.generate_content(
|
| 225 |
[prompt, video_part],
|
| 226 |
+
generation_config={"max_output_tokens": 1024},
|
| 227 |
request_options={"timeout": timeout},
|
| 228 |
)
|
| 229 |
+
|
| 230 |
+
# --------------------------------------------------------------
|
| 231 |
+
# 5️⃣ Pull the safety feedback (always safe to access)
|
| 232 |
+
# --------------------------------------------------------------
|
| 233 |
+
feedback = resp.prompt_feedback
|
| 234 |
+
block_reason = getattr(feedback, "block_reason", None)
|
| 235 |
+
safety_ratings = getattr(feedback, "safety_ratings", [])
|
| 236 |
+
|
| 237 |
+
# --------------------------------------------------------------
|
| 238 |
+
# 6️⃣ Assemble the output we will return
|
| 239 |
+
# --------------------------------------------------------------
|
| 240 |
+
parts = []
|
| 241 |
+
|
| 242 |
+
# 6a – Normal text (if any candidate was returned)
|
| 243 |
+
if resp.candidates:
|
| 244 |
+
parts.append(resp.text) # quick accessor works here
|
| 245 |
+
else:
|
| 246 |
+
parts.append("[No candidate output – request blocked]")
|
| 247 |
+
|
| 248 |
+
# 6b – Full safety feedback
|
| 249 |
+
if block_reason:
|
| 250 |
+
parts.append(f"\n**Block reason:** {block_reason}")
|
| 251 |
+
|
| 252 |
+
if safety_ratings:
|
| 253 |
+
rating_lines = []
|
| 254 |
+
for rating in safety_ratings:
|
| 255 |
+
cat = rating.category.name
|
| 256 |
+
sev = rating.probability.name
|
| 257 |
+
rating_lines.append(f"- {cat}: {sev}")
|
| 258 |
+
parts.append("\n**Safety ratings**:\n" + "\n".join(rating_lines))
|
| 259 |
+
|
| 260 |
+
# 6c – Any additional message the API may include
|
| 261 |
+
if getattr(resp, "message", None):
|
| 262 |
+
parts.append(f"\n**Message:** {resp.message}")
|
| 263 |
+
|
| 264 |
+
return "\n".join(parts)
|
| 265 |
|
| 266 |
|
| 267 |
def _strip_prompt_echo(prompt: str, text: str, threshold: float = 0.68) -> str:
|