lovepreetsingh1996 commited on
Commit
260122f
·
1 Parent(s): 11beb43

fix: couple of issues

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -31,11 +31,10 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
31
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
32
  class BasicAgent:
33
  def __init__(self):
 
 
34
  # self.llm = Ollama(model="qwen2.5:7b", request_timeout=500)
35
  self.llm = Gemini(model_name="models/gemini-2.0-flash")
36
- print("BasicAgent initialized.")
37
- def __call__(self, question: str) -> str:
38
- print(f"Agent received question (first 50 chars): {question[:50]}...")
39
  def get_answers_from_youtube_transcript(youtube_link: str) -> str:
40
  """Fetches transcript of the given youtube_link and returns matching answers based on query.
41
  Args:
@@ -70,7 +69,7 @@ class BasicAgent:
70
  nodes = splitter.get_nodes_from_documents(documents)
71
 
72
  retriever = BM25Retriever(nodes=nodes, similarity_top_k=10)
73
- synthesizer = get_response_synthesizer(response_mode="refine", llm=self.llm)
74
  query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=synthesizer)
75
 
76
  response = query_engine.query(query)
@@ -95,7 +94,7 @@ class BasicAgent:
95
  nodes = splitter.get_nodes_from_documents(documents)
96
 
97
  retriever = BM25Retriever(nodes=nodes, similarity_top_k=1)
98
- synthesizer = get_response_synthesizer(response_mode="refine", llm=llm)
99
  query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=synthesizer)
100
 
101
  response = query_engine.query(query)
@@ -111,17 +110,22 @@ class BasicAgent:
111
  description="Searches wikipedia and converts results into a high quality answer."
112
  )
113
 
114
- agent = AgentWorkflow.from_tools_or_functions([duckduckgo_search_tool, youtube_transcript_answer_tool, wikipedia_search_tool], llm=self.llm)
115
- async def run_agent():
116
- return await agent.run(question)
 
 
 
 
 
117
 
118
- response = asyncio.run(run_agent())
119
 
120
  final_answer = response.response.blocks[0].text
121
  print(f"Agent returning fixed answer: {final_answer}")
122
  return final_answer
123
 
124
- def run_and_submit_all( profile: gr.OAuthProfile | None):
125
  """
126
  Fetches all questions, runs the BasicAgent on them, submits all answers,
127
  and displays the results.
@@ -176,7 +180,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
176
  answers_payload = []
177
  print(f"Running agent on {len(questions_data)} questions...")
178
  for item in questions_data:
179
- time.sleep(60)
180
  task_id = item.get("task_id")
181
  question_text = item.get("question")
182
  if not task_id or question_text is None:
 
31
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
32
  class BasicAgent:
33
  def __init__(self):
34
+ # TODO inspect messages exchanged between the llm and the agent
35
+
36
  # self.llm = Ollama(model="qwen2.5:7b", request_timeout=500)
37
  self.llm = Gemini(model_name="models/gemini-2.0-flash")
 
 
 
38
  def get_answers_from_youtube_transcript(youtube_link: str) -> str:
39
  """Fetches transcript of the given youtube_link and returns matching answers based on query.
40
  Args:
 
69
  nodes = splitter.get_nodes_from_documents(documents)
70
 
71
  retriever = BM25Retriever(nodes=nodes, similarity_top_k=10)
72
+ synthesizer = get_response_synthesizer(response_mode="tree_summarize", llm=self.llm)
73
  query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=synthesizer)
74
 
75
  response = query_engine.query(query)
 
94
  nodes = splitter.get_nodes_from_documents(documents)
95
 
96
  retriever = BM25Retriever(nodes=nodes, similarity_top_k=1)
97
+ synthesizer = get_response_synthesizer(response_mode="tree_summarize", llm=self.llm)
98
  query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=synthesizer)
99
 
100
  response = query_engine.query(query)
 
110
  description="Searches wikipedia and converts results into a high quality answer."
111
  )
112
 
113
+ self.agent = AgentWorkflow.from_tools_or_functions([duckduckgo_search_tool, youtube_transcript_answer_tool, wikipedia_search_tool], llm=self.llm)
114
+ print("BasicAgent initialized.")
115
+
116
+ async def run_agent(self, question: str):
117
+ return await self.agent.run(question)
118
+
119
+ def __call__(self, question: str) -> str:
120
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
121
 
122
+ response = asyncio.run(self.run_agent(question=question))
123
 
124
  final_answer = response.response.blocks[0].text
125
  print(f"Agent returning fixed answer: {final_answer}")
126
  return final_answer
127
 
128
+ async def run_and_submit_all( profile: gr.OAuthProfile | None):
129
  """
130
  Fetches all questions, runs the BasicAgent on them, submits all answers,
131
  and displays the results.
 
180
  answers_payload = []
181
  print(f"Running agent on {len(questions_data)} questions...")
182
  for item in questions_data:
183
+ await asyncio.sleep(60)
184
  task_id = item.get("task_id")
185
  question_text = item.get("question")
186
  if not task_id or question_text is None: