Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,94 @@ class BasicAgent:
|
|
| 19 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
return fixed_answer
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -40,7 +128,19 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 19 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
return fixed_answer
|
| 21 |
|
| 22 |
+
from smolagents import ToolCallingAgent, InferenceClientModel()
|
| 23 |
+
from smolagents.tools import DuckDuckGoSearchResults
|
| 24 |
+
|
| 25 |
+
from smolagents import Tool
|
| 26 |
+
from smolagents.tools import DuckDuckGoSearchResults
|
| 27 |
+
from smolagents.models import InferenceClientModel
|
| 28 |
+
from smolagents import CodeAgent
|
| 29 |
+
|
| 30 |
+
class WebSearchTool(Tool):
|
| 31 |
+
def __init__(self):
|
| 32 |
+
self.agent = CodeAgent(
|
| 33 |
+
tools=[DuckDuckGoSearchResults()],
|
| 34 |
+
model=InferenceClientModel("deepseek-ai/DeepSeek-R1"),
|
| 35 |
+
name="WebSearcher",
|
| 36 |
+
description="Uses DuckDuckGo to answer queries with live web results.",
|
| 37 |
+
max_steps=5
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def __call__(self, query: str) -> str:
|
| 41 |
+
try:
|
| 42 |
+
result = self.agent(query)
|
| 43 |
+
return result.get("output", "No response.")
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Web search failed: {e}"
|
| 46 |
+
|
| 47 |
+
class VideoAnalyzerTool(Tool):
|
| 48 |
+
def __init__(self):
|
| 49 |
+
self.image_classifier = ImageClassifierTool()
|
| 50 |
+
|
| 51 |
+
def __call__(self, video_path: str) -> str:
|
| 52 |
+
cap = cv2.VideoCapture(video_path)
|
| 53 |
+
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 54 |
+
labels = set()
|
| 55 |
+
|
| 56 |
+
for i in range(0, frame_count, max(1, frame_count // 5)):
|
| 57 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
| 58 |
+
ret, frame = cap.read()
|
| 59 |
+
if not ret:
|
| 60 |
+
continue
|
| 61 |
+
frame_path = f"temp_frame.jpg"
|
| 62 |
+
cv2.imwrite(frame_path, frame)
|
| 63 |
+
try:
|
| 64 |
+
label = self.image_classifier(frame_path)
|
| 65 |
+
labels.add(label)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
labels.add(f"Error processing frame: {e}")
|
| 68 |
+
os.remove(frame_path)
|
| 69 |
+
|
| 70 |
+
cap.release()
|
| 71 |
+
return f"Video contains: {', '.join(labels)}"
|
| 72 |
+
|
| 73 |
+
from smolagents import CodeAgent, Tool
|
| 74 |
+
from PIL import Image
|
| 75 |
+
import torch
|
| 76 |
+
import torchvision.transforms as transforms
|
| 77 |
+
from transformers import ViTForImageClassification, ViTFeatureExtractor
|
| 78 |
+
import cv2
|
| 79 |
+
import os
|
| 80 |
+
|
| 81 |
+
class ImageClassifierTool(Tool):
|
| 82 |
+
def __init__(self):
|
| 83 |
+
self.model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224")
|
| 84 |
+
self.feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
| 85 |
+
self.transform = transforms.Compose([
|
| 86 |
+
transforms.Resize((224, 224)),
|
| 87 |
+
transforms.ToTensor()
|
| 88 |
+
])
|
| 89 |
+
self.id2label = self.model.config.id2label
|
| 90 |
+
|
| 91 |
+
def __call__(self, image_path: str) -> str:
|
| 92 |
+
image = Image.open(image_path).convert("RGB")
|
| 93 |
+
inputs = self.feature_extractor(images=image, return_tensors="pt")
|
| 94 |
+
with torch.no_grad():
|
| 95 |
+
outputs = self.model(**inputs)
|
| 96 |
+
logits = outputs.logits
|
| 97 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 98 |
+
return f"Predicted label: {self.id2label[predicted_class_idx]}"
|
| 99 |
+
|
| 100 |
+
class TimezoneTool(Tool):
|
| 101 |
+
name = "timezone_tool"
|
| 102 |
+
description = "Returns the current time for a given city."
|
| 103 |
+
|
| 104 |
+
def __call__(self, city: str) -> str:
|
| 105 |
+
url = f"http://worldtimeapi.org/api/timezone"
|
| 106 |
+
response = requests.get(url).json()
|
| 107 |
+
# You'd want to match city to a timezone
|
| 108 |
+
return "It's 9:45 AM in Tokyo."
|
| 109 |
+
|
| 110 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 111 |
"""
|
| 112 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 128 |
|
| 129 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 130 |
try:
|
| 131 |
+
agent = CodeAgent(
|
| 132 |
+
tools=[
|
| 133 |
+
ImageClassifierTool(),
|
| 134 |
+
VideoAnalyzerTool(),
|
| 135 |
+
TimezoneTool(),
|
| 136 |
+
WebSearchTool(), # Now a Tool, so it can be integrated!
|
| 137 |
+
],
|
| 138 |
+
model=InferenceClientModel("HuggingFaceH4/zephyr-7b-beta"),
|
| 139 |
+
max_steps=5,
|
| 140 |
+
name="web/media-agent",
|
| 141 |
+
description="An intelligent assistant that can classify images, summarize videos, check timezones, and search the web in real time."
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
except Exception as e:
|
| 145 |
print(f"Error instantiating agent: {e}")
|
| 146 |
return f"Error initializing agent: {e}", None
|