dygoo commited on
Commit
c1ec90a
·
verified ·
1 Parent(s): 12c5c93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -4
app.py CHANGED
@@ -11,6 +11,7 @@ from functools import lru_cache # Added: For caching search results
11
  from youtube_transcript_api import YouTubeTranscriptApi
12
  import re
13
 
 
14
  class YouTubeVideoTool:
15
  def __init__(self):
16
  self.name = "youtube_video_tool"
@@ -41,6 +42,8 @@ class YouTubeVideoTool:
41
  except Exception as e:
42
  return f"Error processing YouTube video: {str(e)}"
43
 
 
 
44
  def _extract_video_id(self, url_or_id):
45
  """Extract YouTube video ID from various URL formats or return the ID if already provided."""
46
  # Handle direct video ID
@@ -78,13 +81,18 @@ def cached_search(query):
78
 
79
  # --- Basic Agent Definition ---
80
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
81
  class BasicAgent:
 
 
82
  def __init__(self, model=None, tools=None):
83
  self.model = model
84
  self.tools = tools if tools is not None else []
85
  self.history = []
86
- print("BasicAgent initialized.")
87
 
 
88
  def __call__(self, question: str) -> str:
89
  print(f"Agent received question (first 50 chars): {question[:50]}...")
90
  # Implement your agent logic here using self.model and self.tools
@@ -92,6 +100,8 @@ class BasicAgent:
92
  print(f"Agent returning answer: {final_answer[:50]}...")
93
  return final_answer
94
 
 
 
95
  def process_question(self, question:str) -> str:
96
  try:
97
  # Check if this is a request about a YouTube video
@@ -111,7 +121,7 @@ class BasicAgent:
111
  return self._formulate_direct_answer(relevant_info, question)
112
  else:
113
  # Use regular search
114
- search_results = cached_search(question) if self.tools and search_tool in self.tools else "No search results available."
115
  relevant_info = self._extract_key_info(search_results, question)
116
  return self._formulate_direct_answer(relevant_info, question)
117
  except Exception as e:
@@ -125,6 +135,8 @@ class BasicAgent:
125
  return self._get_fallback_answer(question)
126
  return self._get_fallback_answer(question)
127
 
 
 
128
  def _extract_key_info(self, search_results, question):
129
  # Split results into sentences and find most relevant
130
  sentences = search_results.split('. ')
@@ -141,10 +153,15 @@ class BasicAgent:
141
  # Fallback to first few sentences
142
  return '. '.join(sentences[:2])
143
 
 
 
144
  def _formulate_direct_answer(self, relevant_info, question):
145
- # Format as direct, concise answer
 
146
  return relevant_info
147
 
 
 
148
  def _get_fallback_answer(self, question):
149
  return f"Based on the information available, I cannot provide a specific answer to your question about {question.split()[0:3]}..."
150
 
@@ -169,6 +186,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
169
  questions_url = f"{api_url}/questions"
170
  submit_url = f"{api_url}/submit"
171
 
 
 
172
  # 1. Instantiate Agent ( modify this part to create your agent)
173
  try:
174
  agent = BasicAgent(model= 'gemini/gemini-2.0-flash-exp', tools=[search_tool, visit_webpage, youtube_tool])
@@ -179,6 +198,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
179
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
180
  print(agent_code)
181
 
 
 
182
  # 2. Fetch Questions
183
  print(f"Fetching questions from: {questions_url}")
184
  try:
@@ -200,11 +221,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
200
  print(f"An unexpected error occurred fetching questions: {e}")
201
  return f"An unexpected error occurred fetching questions: {e}", None
202
 
 
 
203
  # 3. Run your Agent
204
  results_log = []
205
  answers_payload = []
206
  print(f"Running agent on {len(questions_data)} questions...")
207
- for item in questions_data:
208
  task_id = item.get("task_id")
209
  question_text = item.get("question")
210
  if not task_id or question_text is None:
@@ -224,11 +247,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
224
  print("Agent did not produce any answers to submit.")
225
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
226
 
 
 
227
  # 4. Prepare Submission
228
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
229
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
230
  print(status_update)
231
 
 
 
232
  # 5. Submit
233
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
234
  try:
@@ -273,6 +300,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
273
  return status_message, results_df
274
 
275
 
 
276
  # --- Build Gradio Interface using Blocks ---
277
  with gr.Blocks() as demo:
278
  gr.Markdown("# Basic Agent Evaluation Runner")
 
11
  from youtube_transcript_api import YouTubeTranscriptApi
12
  import re
13
 
14
+
15
  class YouTubeVideoTool:
16
  def __init__(self):
17
  self.name = "youtube_video_tool"
 
42
  except Exception as e:
43
  return f"Error processing YouTube video: {str(e)}"
44
 
45
+
46
+
47
  def _extract_video_id(self, url_or_id):
48
  """Extract YouTube video ID from various URL formats or return the ID if already provided."""
49
  # Handle direct video ID
 
81
 
82
  # --- Basic Agent Definition ---
83
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
84
+
85
+
86
  class BasicAgent:
87
+
88
+
89
  def __init__(self, model=None, tools=None):
90
  self.model = model
91
  self.tools = tools if tools is not None else []
92
  self.history = []
93
+ print(f"BasicAgent initialized with model: {model} and {len(self.tools)} tools.")
94
 
95
+
96
  def __call__(self, question: str) -> str:
97
  print(f"Agent received question (first 50 chars): {question[:50]}...")
98
  # Implement your agent logic here using self.model and self.tools
 
100
  print(f"Agent returning answer: {final_answer[:50]}...")
101
  return final_answer
102
 
103
+
104
+
105
  def process_question(self, question:str) -> str:
106
  try:
107
  # Check if this is a request about a YouTube video
 
121
  return self._formulate_direct_answer(relevant_info, question)
122
  else:
123
  # Use regular search
124
+ search_results = cached_search(question) if any(isinstance(tool, DuckDuckGoSearchTool) for tool in self.tools) else "No search results available."
125
  relevant_info = self._extract_key_info(search_results, question)
126
  return self._formulate_direct_answer(relevant_info, question)
127
  except Exception as e:
 
135
  return self._get_fallback_answer(question)
136
  return self._get_fallback_answer(question)
137
 
138
+
139
+
140
  def _extract_key_info(self, search_results, question):
141
  # Split results into sentences and find most relevant
142
  sentences = search_results.split('. ')
 
153
  # Fallback to first few sentences
154
  return '. '.join(sentences[:2])
155
 
156
+
157
+
158
  def _formulate_direct_answer(self, relevant_info, question):
159
+ if self.model:
160
+ return f"Based on the search: {relevant_info}"
161
  return relevant_info
162
 
163
+
164
+
165
  def _get_fallback_answer(self, question):
166
  return f"Based on the information available, I cannot provide a specific answer to your question about {question.split()[0:3]}..."
167
 
 
186
  questions_url = f"{api_url}/questions"
187
  submit_url = f"{api_url}/submit"
188
 
189
+
190
+
191
  # 1. Instantiate Agent ( modify this part to create your agent)
192
  try:
193
  agent = BasicAgent(model= 'gemini/gemini-2.0-flash-exp', tools=[search_tool, visit_webpage, youtube_tool])
 
198
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
199
  print(agent_code)
200
 
201
+
202
+
203
  # 2. Fetch Questions
204
  print(f"Fetching questions from: {questions_url}")
205
  try:
 
221
  print(f"An unexpected error occurred fetching questions: {e}")
222
  return f"An unexpected error occurred fetching questions: {e}", None
223
 
224
+
225
+
226
  # 3. Run your Agent
227
  results_log = []
228
  answers_payload = []
229
  print(f"Running agent on {len(questions_data)} questions...")
230
+ for idx, item in enumerate(questions_data):
231
  task_id = item.get("task_id")
232
  question_text = item.get("question")
233
  if not task_id or question_text is None:
 
247
  print("Agent did not produce any answers to submit.")
248
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
249
 
250
+
251
+
252
  # 4. Prepare Submission
253
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
254
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
255
  print(status_update)
256
 
257
+
258
+
259
  # 5. Submit
260
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
261
  try:
 
300
  return status_message, results_df
301
 
302
 
303
+
304
  # --- Build Gradio Interface using Blocks ---
305
  with gr.Blocks() as demo:
306
  gr.Markdown("# Basic Agent Evaluation Runner")