kpbotla commited on
Commit
c93f6ca
·
verified ·
1 Parent(s): 2473595

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -62
app.py CHANGED
@@ -7,72 +7,39 @@ from duckduckgo_search import DDGS
7
  from transformers import pipeline
8
 
9
  # --- Tool Definitions ---
10
- @tool
11
- class WebSearchTool:
12
- name = "web_search"
13
- description = "Search the web for up-to-date factual information."
14
- def use(self, query: str) -> str:
15
- with DDGS() as ddgs:
16
- results = ddgs.text(query)
17
- output = [f"{r['title']} - {r['href']}" for r in results[:3]]
18
- return "\n".join(output) if output else "No relevant results found."
19
-
20
- @tool
21
- class CiteTool:
22
- name = "cite"
23
- description = "Add citation to a given answer with a valid URL."
24
- def use(self, input: str) -> str:
25
- try:
26
- answer, url = input.split("|||")
27
- return f"{answer.strip()}\n\nSource: [{url.strip()}]({url.strip()})"
28
- except:
29
- return "Could not format citation correctly."
30
-
31
- summarizer = pipeline("summarization")
32
- @tool
33
- class SummarizerTool:
34
- name = "summarize"
35
- description = "Summarize a long text into a short paragraph."
36
- def use(self, input: str) -> str:
37
- if len(input) < 50:
38
- return input
39
- result = summarizer(input, max_length=100, min_length=25, do_sample=False)
40
- return result[0]['summary_text']
41
-
42
- @tool
43
- class PythonTool:
44
- name = "python"
45
- description = "Execute Python code to solve math problems."
46
- def use(self, code: str) -> str:
47
- try:
48
- result = str(eval(code, {"__builtins__": {}}))
49
- return f"Answer: {result}"
50
- except Exception as e:
51
- return f"Error: {str(e)}"
52
 
53
- @tool
54
- class FallbackTool:
55
- name = "fallback"
56
- description = "Handle unanswerable or unclear queries."
57
- def use(self, _: str) -> str:
58
- return "I'm sorry, I couldn't find the answer to your question. Could you rephrase or try something else?"
59
-
60
- # --- Basic Agent Definition ---
61
  class BasicAgent:
62
  def __init__(self):
63
- tools = [WebSearchTool(), CiteTool(), SummarizerTool(), PythonTool(), FallbackTool()]
64
- self.agent = Agent(
65
- tools=tools,
66
- system_prompt="""
67
- You are Smart Answering Agent v3.
68
- Answer questions factually, concisely, and cite sources when available.
69
- Route to the correct tool for factual, math, or summarization queries.
70
- If you don’t know the answer, respond gracefully using the fallback tool.
71
- Ensure output format is friendly for the GAIA evaluation.
72
- """
73
- )
74
  def __call__(self, question: str) -> str:
75
- return self.agent.run(question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  # --- Evaluation Logic ---
78
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
7
  from transformers import pipeline
8
 
9
  # --- Tool Definitions ---
10
+ from tools.search_tool import WebSearchTool
11
+ from tools.citation_tool import CiteTool
12
+ from tools.summarizer_tool import SummarizerTool
13
+ from tools.math_tool import PythonTool
14
+ from tools.fallback_tool import FallbackTool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
 
 
 
 
 
 
 
 
16
  class BasicAgent:
17
  def __init__(self):
18
+ self.search = WebSearchTool()
19
+ self.cite = CiteTool()
20
+ self.summarizer = SummarizerTool()
21
+ self.math = PythonTool()
22
+ self.fallback = FallbackTool()
23
+
 
 
 
 
 
24
  def __call__(self, question: str) -> str:
25
+ question_lower = question.lower()
26
+
27
+ try:
28
+ if any(keyword in question_lower for keyword in ["latest", "news", "who", "what", "where", "when"]):
29
+ return self.search.use(question)
30
+ elif any(keyword in question_lower for keyword in ["summarize", "summary", "explain"]):
31
+ return self.summarizer.use(question)
32
+ elif any(char.isdigit() for char in question) and any(op in question for op in ["+", "-", "*", "/"]):
33
+ return self.math.use(question)
34
+ elif "|||" in question:
35
+ return self.cite.use(question)
36
+ else:
37
+ return self.fallback.use(question)
38
+ except Exception as e:
39
+ return f"Error during processing: {str(e)}"
40
+
41
+
42
+
43
 
44
  # --- Evaluation Logic ---
45
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"