selim-ba commited on
Commit
e1f889f
·
verified ·
1 Parent(s): e322b50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py CHANGED
@@ -8,6 +8,12 @@ from langgraph.graph import StateGraph, END
8
  from typing import TypedDict
9
  import re
10
  import string
 
 
 
 
 
 
11
 
12
 
13
  # (Keep Constants as is)
@@ -121,6 +127,76 @@ class SuperSmartAgent:
121
  print(f"Fallback triggered. Final question state: {state['question']}")
122
  state["response"] = "This question doesn't require Python or is unclear."
123
  return state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  class AgentState(TypedDict, total=False):
126
  question: str
@@ -141,6 +217,7 @@ class SuperSmartAgent:
141
  builder.add_node("fallback", fallback)
142
 
143
  # Edges
 
144
  builder.set_entry_point("check_reversed")
145
  builder.add_edge("check_reversed", "fix_question")
146
 
@@ -157,6 +234,21 @@ class SuperSmartAgent:
157
  )
158
  builder.add_edge("generate_code", END)
159
  builder.add_edge("fallback", END)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  graph = builder.compile()
162
  return graph
 
8
  from typing import TypedDict
9
  import re
10
  import string
11
+ from duckduckgo_search import ddg
12
+ from pytube import YouTube
13
+ import cv2
14
+ from ultralytics import YOLO
15
+ import tempfile
16
+ import os
17
 
18
 
19
  # (Keep Constants as is)
 
127
  print(f"Fallback triggered. Final question state: {state['question']}")
128
  state["response"] = "This question doesn't require Python or is unclear."
129
  return state
130
+
131
+ ###################
132
+ model = YOLO("yolov8n.pt") # lightweight object detector
133
+
134
+ def check_special_tools(state):
135
+ q = state["question"].lower()
136
+ if "search the web" in q or "look up" in q:
137
+ state["use_tool"] = "web"
138
+ elif "how many objects" in q and "video" in q:
139
+ state["use_tool"] = "video"
140
+ else:
141
+ state["use_tool"] = None
142
+ return state
143
+
144
+ def use_web_search(state):
145
+ query = state["question"].replace("search the web for", "").replace("look up", "").strip()
146
+ print(f"Searching DuckDuckGo for: {query}")
147
+ results = ddg(query, max_results=3)
148
+ if results:
149
+ snippets = [r['title'] + ": " + r['body'] for r in results]
150
+ state["response"] = "\n".join(snippets)
151
+ else:
152
+ state["response"] = "No results found."
153
+ return state
154
+
155
+ def use_video_analysis(state):
156
+ import re
157
+
158
+ url_match = re.search(r"(https?://[^\s]+)", state["question"])
159
+ url = url_match.group(1) if url_match else None
160
+
161
+ if not url:
162
+ state["response"] = "Could not find a video URL in the question."
163
+ return state
164
+
165
+ print(f"Analyzing video from: {url}")
166
+
167
+ try:
168
+ yt = YouTube(url)
169
+ stream = yt.streams.filter(file_extension="mp4", progressive=True).order_by('resolution').desc().first()
170
+ temp_file = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
171
+ stream.download(filename=temp_file.name)
172
+
173
+ cap = cv2.VideoCapture(temp_file.name)
174
+ frames = []
175
+ frame_count = 0
176
+
177
+ while True:
178
+ ret, frame = cap.read()
179
+ if not ret or frame_count >= 5: # analyze only first 5 frames
180
+ break
181
+ frames.append(frame)
182
+ frame_count += 1
183
+ cap.release()
184
+ os.remove(temp_file.name)
185
+
186
+ object_counts = []
187
+ for frame in frames:
188
+ results = model(frame)
189
+ count = len(results[0].boxes)
190
+ object_counts.append(count)
191
+
192
+ state["response"] = f"Objects per frame (first {len(object_counts)} frames): {object_counts}"
193
+ except Exception as e:
194
+ state["response"] = f"Error processing video: {e}"
195
+ return state
196
+
197
+
198
+
199
+ ##################
200
 
201
  class AgentState(TypedDict, total=False):
202
  question: str
 
217
  builder.add_node("fallback", fallback)
218
 
219
  # Edges
220
+ ####################################################
221
  builder.set_entry_point("check_reversed")
222
  builder.add_edge("check_reversed", "fix_question")
223
 
 
234
  )
235
  builder.add_edge("generate_code", END)
236
  builder.add_edge("fallback", END)
237
+ #####################################################
238
+ builder.add_node("check_special_tools", check_special_tools)
239
+ builder.add_node("use_web_search", use_web_search)
240
+ builder.add_node("use_video_analysis", use_video_analysis)
241
+
242
+ builder.set_entry_point("check_special_tools")
243
+ builder.add_conditional_edges(
244
+ "check_special_tools",
245
+ lambda s: "use_web_search" if s.get("use_tool") == "web"
246
+ else "use_video_analysis" if s.get("use_tool") == "video"
247
+ else "check_reversed"
248
+ )
249
+
250
+ builder.add_edge("use_web_search", END)
251
+ builder.add_edge("use_video_analysis", END)
252
 
253
  graph = builder.compile()
254
  return graph