Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,32 +31,34 @@ class GeminiLLM(LLM):
|
|
| 31 |
return "google-gemini-llm"
|
| 32 |
|
| 33 |
def _make_request(self, api_key: str, prompt: str) -> requests.Response:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
],
|
| 54 |
-
"generationConfig": {
|
| 55 |
-
"temperature": self.temperature
|
| 56 |
}
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
|
@@ -238,59 +240,6 @@ def square_root(a: float) -> float:
|
|
| 238 |
return a ** 0.5
|
| 239 |
return sqrt(a)
|
| 240 |
|
| 241 |
-
@tool
|
| 242 |
-
def ask_youtube_video(url: str, objeto: str = "bird") -> str:
|
| 243 |
-
"""
|
| 244 |
-
Analyzes a YouTube video and estimates how many objects of the requested type appear in it.
|
| 245 |
-
Uses a YOLOv8 object detection model to count appearances in selected frames.
|
| 246 |
-
|
| 247 |
-
Args:
|
| 248 |
-
url (str): The YouTube video URL.
|
| 249 |
-
objeto (str): Object to search for (default is "bird").
|
| 250 |
-
|
| 251 |
-
Returns:
|
| 252 |
-
str: Approximate number of detected objects in the video or an error message.
|
| 253 |
-
"""
|
| 254 |
-
import tempfile
|
| 255 |
-
import cv2
|
| 256 |
-
from ultralytics import YOLO
|
| 257 |
-
from pytube import YouTube
|
| 258 |
-
|
| 259 |
-
try:
|
| 260 |
-
# Download video temporarily
|
| 261 |
-
yt = YouTube(url)
|
| 262 |
-
video_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 263 |
-
yt.streams.filter(file_extension="mp4", progressive=True).first().download(filename=video_path)
|
| 264 |
-
|
| 265 |
-
# Load pre-trained YOLO model (lightweight for faster analysis)
|
| 266 |
-
model = YOLO("yolov8s.pt")
|
| 267 |
-
|
| 268 |
-
# Process video
|
| 269 |
-
cap = cv2.VideoCapture(video_path)
|
| 270 |
-
frame_count, total_detected = 0, 0
|
| 271 |
-
|
| 272 |
-
while True:
|
| 273 |
-
ret, frame = cap.read()
|
| 274 |
-
if not ret:
|
| 275 |
-
break
|
| 276 |
-
frame_count += 1
|
| 277 |
-
|
| 278 |
-
# Analyze 1 out of every 30 frames
|
| 279 |
-
if frame_count % 30 == 0:
|
| 280 |
-
results = model(frame, verbose=False)
|
| 281 |
-
for r in results:
|
| 282 |
-
for c in r.boxes.cls:
|
| 283 |
-
label = model.names[int(c)]
|
| 284 |
-
if objeto.lower() in label.lower():
|
| 285 |
-
total_detected += 1
|
| 286 |
-
|
| 287 |
-
cap.release()
|
| 288 |
-
return f"Approximately {total_detected} {objeto}(s) detected in the video."
|
| 289 |
-
|
| 290 |
-
except Exception as e:
|
| 291 |
-
return f"Error analyzing video: {str(e)}"
|
| 292 |
-
|
| 293 |
-
|
| 294 |
# --- Basic Agent Definition ---
|
| 295 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 296 |
from langchain_core.prompts import PromptTemplate
|
|
|
|
| 31 |
return "google-gemini-llm"
|
| 32 |
|
| 33 |
def _make_request(self, api_key: str, prompt: str) -> requests.Response:
|
| 34 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/{self.model_name}:generateContent"
|
| 35 |
+
headers = {
|
| 36 |
+
"Content-Type": "application/json",
|
| 37 |
+
"X-goog-api-key": api_key
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
full_prompt = (
|
| 41 |
+
"You are an agent. Please respond concisely only with the answer, "
|
| 42 |
+
"no extra explanations.\n\n"
|
| 43 |
+
f"Question: {prompt}"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
data = {
|
| 47 |
+
"contents": [
|
| 48 |
+
{
|
| 49 |
+
"role": "user",
|
| 50 |
+
"parts": [
|
| 51 |
+
{"text": full_prompt}
|
| 52 |
+
]
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
+
],
|
| 55 |
+
"generationConfig": {
|
| 56 |
+
"temperature": self.temperature
|
| 57 |
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
return requests.post(url, headers=headers, json=data)
|
| 61 |
+
|
| 62 |
|
| 63 |
|
| 64 |
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
|
|
|
| 240 |
return a ** 0.5
|
| 241 |
return sqrt(a)
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
# --- Basic Agent Definition ---
|
| 244 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 245 |
from langchain_core.prompts import PromptTemplate
|