selim-ba commited on
Commit
3d4fa63
·
verified ·
1 Parent(s): b000df6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -33,6 +33,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
33
 
34
  class SuperSmartAgent:
35
  def __init__(self):
 
36
  self.graph = self._build_graph()
37
 
38
  def _build_graph(self):
@@ -129,7 +130,6 @@ class SuperSmartAgent:
129
  return state
130
 
131
  ###################
132
- model = YOLO("yolov8n.pt") # lightweight object detector
133
 
134
  def contains_youtube_url(text):
135
  return re.search(r"(https?://)?(www\.)?(youtube\.com|youtu\.be)/", text) is not None
@@ -166,7 +166,7 @@ class SuperSmartAgent:
166
 
167
 
168
  def use_video_analysis(state):
169
- url_match = re.search(r"(https?://[^\s]+)", state["question"])
170
  url = url_match.group(1).strip() if url_match else None
171
 
172
  if not url:
@@ -181,8 +181,9 @@ class SuperSmartAgent:
181
  if not stream:
182
  state["response"] = "No downloadable video stream found."
183
  return state
184
- temp_file = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
185
- stream.download(filename=temp_file.name)
 
186
 
187
  cap = cv2.VideoCapture(temp_file.name)
188
  frames = []
@@ -199,7 +200,7 @@ class SuperSmartAgent:
199
 
200
  object_counts = []
201
  for frame in frames:
202
- results = model(frame)
203
  count = len(results[0].boxes)
204
  object_counts.append(count)
205
 
 
33
 
34
  class SuperSmartAgent:
35
  def __init__(self):
36
+ self.model = YOLO("yolov8n.pt")
37
  self.graph = self._build_graph()
38
 
39
  def _build_graph(self):
 
130
  return state
131
 
132
  ###################
 
133
 
134
  def contains_youtube_url(text):
135
  return re.search(r"(https?://)?(www\.)?(youtube\.com|youtu\.be)/", text) is not None
 
166
 
167
 
168
  def use_video_analysis(state):
169
+ url_match = re.search(r"(https?://(?:www\.)?(?:youtube\.com/watch\?v=|youtu\.be/)[\w\-]+)", state["question"])
170
  url = url_match.group(1).strip() if url_match else None
171
 
172
  if not url:
 
181
  if not stream:
182
  state["response"] = "No downloadable video stream found."
183
  return state
184
+ with tempfile.TemporaryDirectory() as tmpdirname:
185
+ temp_path = os.path.join(tmpdirname, "video.mp4")
186
+ stream.download(filename=temp_path)
187
 
188
  cap = cv2.VideoCapture(temp_file.name)
189
  frames = []
 
200
 
201
  object_counts = []
202
  for frame in frames:
203
+ results = self.model(frame)
204
  count = len(results[0].boxes)
205
  object_counts.append(count)
206