lovepreetsingh1996 commited on
Commit
c8febd3
·
1 Parent(s): a276d30

fix: try again

Browse files
Files changed (1) hide show
  1. app.py +44 -11
app.py CHANGED
@@ -2,12 +2,20 @@ import os
2
  import gradio as gr
3
  import requests
4
  import inspect
 
5
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
6
  from llama_index.readers.youtube_transcript import YoutubeTranscriptReader
7
  from llama_index.core.tools import FunctionTool
8
  from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
9
  from llama_index.core.agent.workflow import AgentWorkflow
10
  from llama_index.llms.gemini import Gemini
 
 
 
 
 
 
 
11
  import pandas as pd
12
  import asyncio
13
 
@@ -19,15 +27,15 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
20
  class BasicAgent:
21
  def __init__(self):
22
- self.llm = Gemini(model_name="models/gemini-2.0-flash-thinking-exp-01-21")
 
23
  print("BasicAgent initialized.")
24
  def __call__(self, question: str) -> str:
25
  print(f"Agent received question (first 50 chars): {question[:50]}...")
26
- def get_answers_from_youtube_transcript(youtube_link: str, query: str) -> str:
27
  """Fetches transcript of the given youtube_link and returns matching answers based on query.
28
  Args:
29
  youtube_link (str): youtube video link for which we need to answer questions on.
30
- query (str): question to answer from video transcript.
31
  """
32
  loader = YoutubeTranscriptReader()
33
  documents = loader.load_data(
@@ -39,21 +47,46 @@ class BasicAgent:
39
  return "Here's the transcript from the video, examine and formulate answer based on what is said in the transcript: \n" + text
40
 
41
  youtube_transcript_answer_tool = FunctionTool.from_defaults(
42
- get_answers_from_youtube_transcript,
43
- name="get_answers_from_youtube_transcript",
44
- description="Fetches transcript of the given youtube_link and returns matching answers based on query.",
45
  )
46
 
47
- duckduckgoSearchTools = DuckDuckGoSearchToolSpec().to_tool_list()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- agent = AgentWorkflow.from_tools_or_functions([*duckduckgoSearchTools, youtube_transcript_answer_tool], llm=self.llm)
50
 
51
  async def run_agent():
52
  return await agent.run(question)
53
 
54
- fixed_answer = asyncio.run(run_agent())
55
- print(f"Agent returning fixed answer: {fixed_answer}")
56
- return fixed_answer
 
57
 
58
  def run_and_submit_all( profile: gr.OAuthProfile | None):
59
  """
 
2
  import gradio as gr
3
  import requests
4
  import inspect
5
+ from llama_index.llms.ollama import Ollama
6
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
7
  from llama_index.readers.youtube_transcript import YoutubeTranscriptReader
8
  from llama_index.core.tools import FunctionTool
9
  from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
10
  from llama_index.core.agent.workflow import AgentWorkflow
11
  from llama_index.llms.gemini import Gemini
12
+ from llama_index.retrievers.bm25 import BM25Retriever
13
+ from llama_index.core.query_engine import RetrieverQueryEngine
14
+ from llama_index.core.tools import QueryEngineTool
15
+ from llama_index.core.node_parser import SentenceSplitter
16
+ from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
17
+ from llama_index.core.schema import Document
18
+ from llama_index.core import get_response_synthesizer
19
  import pandas as pd
20
  import asyncio
21
 
 
27
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
28
  class BasicAgent:
29
  def __init__(self):
30
+ # self.llm = Ollama(model="qwen2.5:7b", request_timeout=500)
31
+ self.llm = Gemini(model_name="models/gemini-2.0-flash-lite")
32
  print("BasicAgent initialized.")
33
  def __call__(self, question: str) -> str:
34
  print(f"Agent received question (first 50 chars): {question[:50]}...")
35
+ def get_youtube_transcript(youtube_link: str) -> str:
36
  """Fetches transcript of the given youtube_link and returns matching answers based on query.
37
  Args:
38
  youtube_link (str): youtube video link for which we need to answer questions on.
 
39
  """
40
  loader = YoutubeTranscriptReader()
41
  documents = loader.load_data(
 
47
  return "Here's the transcript from the video, examine and formulate answer based on what is said in the transcript: \n" + text
48
 
49
  youtube_transcript_answer_tool = FunctionTool.from_defaults(
50
+ get_youtube_transcript,
51
+ name="get_youtube_transcript",
52
+ description="Fetches transcript of the given video using youtube_link.",
53
  )
54
 
55
+ def duck_duck_go_search_tool(query: str) -> str:
56
+ try:
57
+ raw_results = DuckDuckGoSearchToolSpec().duckduckgo_full_search(query)
58
+ texts = [res['body'] for res in raw_results]
59
+ documents = [Document(text=body) for body in texts]
60
+
61
+ splitter = SentenceSplitter(chunk_size=256)
62
+ nodes = splitter.get_nodes_from_documents(documents)
63
+
64
+ retriever = BM25Retriever(nodes=nodes, similarity_top_k=10)
65
+ synthesizer = get_response_synthesizer(response_mode="refine", llm=self.llm)
66
+ query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=synthesizer)
67
+
68
+ response = query_engine.query(query)
69
+ return response.response
70
+
71
+ except Exception as e:
72
+ return f"An error occurred: {e}"
73
+
74
+
75
+ duckduckgo_search_tool = FunctionTool.from_defaults(
76
+ duck_duck_go_search_tool,
77
+ name="duck_duck_go_search_tool",
78
+ description="Searches the web and refines the result into a high-quality answer."
79
+ )
80
 
81
+ agent = AgentWorkflow.from_tools_or_functions([duckduckgo_search_tool, youtube_transcript_answer_tool], llm=self.llm)
82
 
83
  async def run_agent():
84
  return await agent.run(question)
85
 
86
+ response = asyncio.run(run_agent())
87
+ final_answer = response.response.blocks[0].text
88
+ print(f"Agent returning fixed answer: {final_answer}")
89
+ return final_answer
90
 
91
  def run_and_submit_all( profile: gr.OAuthProfile | None):
92
  """