Sonu313131 commited on
Commit
f5d3707
·
verified ·
1 Parent(s): a655e08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -119
app.py CHANGED
@@ -27,131 +27,14 @@ from smolagents import DuckDuckGoSearchTool
27
  from smolagents.models import InferenceClientModel
28
  from smolagents import CodeAgent
29
 
30
- class CustomDuckDuckGoTool(DuckDuckGoSearchTool):
31
- name = "duckduckgo_search"
32
- description = "Searches the web using DuckDuckGo."
33
-
34
- class WebSearchTool(Tool):
35
- name = "web_search"
36
- description = "Searches the web using DuckDuckGo and retrieves relevant answers."
37
-
38
- def __init__(self):
39
- self.agent = CodeAgent(
40
- tools=[DuckDuckGoSearchTool()],
41
- model=InferenceClientModel("deepseek-ai/DeepSeek-R1"),
42
- name="WebSearcher",
43
- description="Uses DuckDuckGo to answer queries with live web results.",
44
- max_steps=5
45
- )
46
-
47
- def __call__(self, query: str) -> str:
48
- try:
49
- result = self.agent(query)
50
- return result.get("output", "No response.")
51
- except Exception as e:
52
- return f"Web search failed: {e}"
53
-
54
- class VideoAnalyzerTool(Tool):
55
- name = "video_analyzer"
56
- description = "Analyzes frames from a video using image classification."
57
-
58
- def __init__(self):
59
- self.image_classifier = ImageClassifierTool()
60
-
61
- def __call__(self, video_path: str) -> str:
62
- cap = cv2.VideoCapture(video_path)
63
- frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
64
- labels = set()
65
-
66
- for i in range(0, frame_count, max(1, frame_count // 5)):
67
- cap.set(cv2.CAP_PROP_POS_FRAMES, i)
68
- ret, frame = cap.read()
69
- if not ret:
70
- continue
71
- frame_path = f"temp_frame.jpg"
72
- cv2.imwrite(frame_path, frame)
73
- try:
74
- label = self.image_classifier(frame_path)
75
- labels.add(label)
76
- except Exception as e:
77
- labels.add(f"Error processing frame: {e}")
78
- os.remove(frame_path)
79
-
80
- cap.release()
81
- return f"Video contains: {', '.join(labels)}"
82
-
83
- from smolagents import CodeAgent, Tool
84
- from PIL import Image
85
- import torch
86
- import torchvision.transforms as transforms
87
- from transformers import ViTForImageClassification, ViTFeatureExtractor
88
- import cv2
89
- import os
90
-
91
- class ImageClassifierTool(Tool):
92
- name = "image_classifier"
93
- description = "Classifies images using ViT."
94
-
95
- def __init__(self):
96
- self.model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224")
97
- self.feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
98
- self.transform = transforms.Compose([
99
- transforms.Resize((224, 224)),
100
- transforms.ToTensor()
101
- ])
102
- self.id2label = self.model.config.id2label
103
-
104
- def __call__(self, image_path: str) -> str:
105
- image = Image.open(image_path).convert("RGB")
106
- inputs = self.feature_extractor(images=image, return_tensors="pt")
107
- with torch.no_grad():
108
- outputs = self.model(**inputs)
109
- logits = outputs.logits
110
- predicted_class_idx = logits.argmax(-1).item()
111
- return f"Predicted label: {self.id2label[predicted_class_idx]}"
112
-
113
- class TimezoneTool(Tool):
114
- name = "timezone_tool"
115
- description = "Returns the current time for a given city."
116
-
117
- def __call__(self, city: str) -> str:
118
- url = f"http://worldtimeapi.org/api/timezone"
119
- response = requests.get(url).json()
120
- # You'd want to match city to a timezone
121
- return "It's 9:45 AM in Tokyo."
122
-
123
- def run_and_submit_all( profile: gr.OAuthProfile | None):
124
- """
125
- Fetches all questions, runs the BasicAgent on them, submits all answers,
126
- and displays the results.
127
- """
128
- # --- Determine HF Space Runtime URL and Repo URL ---
129
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
130
-
131
- if profile:
132
- username= f"{profile.username}"
133
- print(f"User logged in: {username}")
134
- else:
135
- print("User not logged in.")
136
- return "Please Login to Hugging Face with the button.", None
137
-
138
- api_url = DEFAULT_API_URL
139
- questions_url = f"{api_url}/questions"
140
- submit_url = f"{api_url}/submit"
141
 
142
  # 1. Instantiate Agent ( modify this part to create your agent)
143
  try:
144
  agent = CodeAgent(
145
- tools=[
146
- ImageClassifierTool(),
147
- VideoAnalyzerTool(),
148
- TimezoneTool(),
149
- WebSearchTool(), # Now a Tool, so it can be integrated!
150
- ],
151
  model=InferenceClientModel("HuggingFaceH4/zephyr-7b-beta"),
152
  max_steps=5,
153
- name="web/media-agent",
154
- description="An intelligent assistant that can classify images, summarize videos, check timezones, and search the web in real time."
155
  )
156
 
157
  except Exception as e:
 
27
  from smolagents.models import InferenceClientModel
28
  from smolagents import CodeAgent
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # 1. Instantiate Agent ( modify this part to create your agent)
32
  try:
33
  agent = CodeAgent(
34
+ tools=[DuckDuckGoSearchTool()],
 
 
 
 
 
35
  model=InferenceClientModel("HuggingFaceH4/zephyr-7b-beta"),
36
  max_steps=5,
37
+ verbosity_level=2
 
38
  )
39
 
40
  except Exception as e: