lovepreetsingh1996 commited on
Commit
3cf70e0
·
1 Parent(s): 4946de1

fix: remove wikipedia

Browse files
Files changed (1) hide show
  1. app.py +34 -31
app.py CHANGED
@@ -36,24 +36,28 @@ class BasicAgent:
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_youtube_transcript(youtube_link: str) -> str:
40
  """Fetches transcript of the given youtube_link and returns matching answers based on query.
41
  Args:
42
  youtube_link (str): youtube video link for which we need to answer questions on.
 
43
  """
44
- loader = YoutubeTranscriptReader()
45
- documents = loader.load_data(
46
- ytlinks=[youtube_link]
47
- )
 
48
 
49
- text = documents[0].text_resource.text
50
 
51
- return "Here's the transcript from the video, examine and formulate answer based on what is said in the transcript: \n" + text
 
 
52
 
53
  youtube_transcript_answer_tool = FunctionTool.from_defaults(
54
- get_youtube_transcript,
55
- name="get_youtube_transcript",
56
- description="Fetches transcript of the given youtube video and returns matching answers based on query. To be called when video link is given.",
57
  )
58
 
59
  def duck_duck_go_search_tool(query: str) -> str:
@@ -82,33 +86,32 @@ class BasicAgent:
82
  description="Searches the web and refines the result into a high-quality answer. Use when other tools don't seem suitable"
83
  )
84
 
85
- def wikipedia_search(query: str) -> str:
86
- try:
87
- text = WikipediaToolSpec().search_data(query)
88
- documents = [Document(text)]
89
 
90
- splitter = SentenceSplitter(chunk_size=256)
91
- nodes = splitter.get_nodes_from_documents(documents)
92
 
93
- retriever = BM25Retriever(nodes=nodes, similarity_top_k=10)
94
- synthesizer = get_response_synthesizer(response_mode="refine", llm=self.llm)
95
- query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=synthesizer)
96
 
97
- response = query_engine.query(query)
98
- return response.response
99
-
100
- except Exception as e:
101
- return f"An error occurred: {e}"
102
 
 
 
103
 
104
- wikipedia_search_tool = FunctionTool.from_defaults(
105
- wikipedia_search,
106
- name="wikipedia_search",
107
- description="Searches wikipedia and converts results into a high quality answer."
108
- )
109
 
110
- agent = AgentWorkflow.from_tools_or_functions([duckduckgo_search_tool, youtube_transcript_answer_tool, wikipedia_search_tool], llm=self.llm)
 
 
 
 
111
 
 
112
  async def run_agent():
113
  return await agent.run(question)
114
 
@@ -173,7 +176,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
173
  answers_payload = []
174
  print(f"Running agent on {len(questions_data)} questions...")
175
  for item in questions_data:
176
- time.sleep(45)
177
  task_id = item.get("task_id")
178
  question_text = item.get("question")
179
  if not task_id or question_text is None:
 
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:
42
  youtube_link (str): youtube video link for which we need to answer questions on.
43
+ query (str): question to answer from video transcript.
44
  """
45
+ try:
46
+ loader = YoutubeTranscriptReader()
47
+ documents = loader.load_data(
48
+ ytlinks=[youtube_link]
49
+ )
50
 
51
+ text = documents[0].text_resource.text
52
 
53
+ return "Here's the transcript from the video, examine and formulate answer based on what is said in the transcript: \n" + text
54
+ except Exception as e:
55
+ print("error", e)
56
 
57
  youtube_transcript_answer_tool = FunctionTool.from_defaults(
58
+ get_answers_from_youtube_transcript,
59
+ name="get_answers_from_youtube_transcript",
60
+ description="Fetches transcript of the given youtube_link and returns matching answers based on query. To be called when video link is given.",
61
  )
62
 
63
  def duck_duck_go_search_tool(query: str) -> str:
 
86
  description="Searches the web and refines the result into a high-quality answer. Use when other tools don't seem suitable"
87
  )
88
 
89
+ # def wikipedia_search(query: str) -> str:
90
+ # try:
91
+ # text = WikipediaToolSpec().search_data(query)
92
+ # documents = [Document(text=text)]
93
 
94
+ # splitter = SentenceSplitter(chunk_size=256)
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)
102
+ # return response.response
 
 
 
103
 
104
+ # except Exception as e:
105
+ # return f"An error occurred: {e}"
106
 
 
 
 
 
 
107
 
108
+ # wikipedia_search_tool = FunctionTool.from_defaults(
109
+ # wikipedia_search,
110
+ # name="wikipedia_search",
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], llm=self.llm)
115
  async def run_agent():
116
  return await agent.run(question)
117
 
 
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: