Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
#from smolagents import Agent, tool
|
| 6 |
from duckduckgo_search import DDGS
|
| 7 |
from transformers import pipeline
|
| 8 |
from smolagents import tool
|
| 9 |
|
| 10 |
-
# TOOL 1: Wikipedia-based Search Tool
|
| 11 |
-
from duckduckgo_search import DDGS
|
| 12 |
-
|
| 13 |
@tool
|
| 14 |
def web_search(query: str) -> str:
|
| 15 |
"""
|
|
@@ -31,7 +28,6 @@ def web_search(query: str) -> str:
|
|
| 31 |
return f"{snippet}\n\nSource: [{r['href']}]({r['href']})"
|
| 32 |
return "Could not find a direct answer from Wikipedia."
|
| 33 |
|
| 34 |
-
# TOOL 2: Citation Tool
|
| 35 |
@tool
|
| 36 |
def cite(input: str) -> str:
|
| 37 |
"""
|
|
@@ -49,7 +45,6 @@ def cite(input: str) -> str:
|
|
| 49 |
except:
|
| 50 |
return "Could not format citation."
|
| 51 |
|
| 52 |
-
# TOOL 3: Math Eval
|
| 53 |
@tool
|
| 54 |
def python(code: str) -> str:
|
| 55 |
"""
|
|
@@ -67,7 +62,6 @@ def python(code: str) -> str:
|
|
| 67 |
except Exception as e:
|
| 68 |
return f"Error: {str(e)}"
|
| 69 |
|
| 70 |
-
# TOOL 4: Fallback
|
| 71 |
@tool
|
| 72 |
def fallback(_: str) -> str:
|
| 73 |
"""
|
|
@@ -81,27 +75,18 @@ def fallback(_: str) -> str:
|
|
| 81 |
"""
|
| 82 |
return "Sorry, I couldn't confidently answer that. Could you rephrase?"
|
| 83 |
|
| 84 |
-
# MAIN AGENT
|
| 85 |
class BasicAgent:
|
| 86 |
def __call__(self, question: str) -> str:
|
| 87 |
q = question.lower()
|
| 88 |
|
| 89 |
try:
|
| 90 |
-
|
| 91 |
-
if any(x in q for x in ["who", "what", "when", "where", "how many", "how much", "did", "which", "name"]):
|
| 92 |
-
return web_search(question)
|
| 93 |
-
|
| 94 |
-
# Inline citation formatting
|
| 95 |
-
elif "|||" in q:
|
| 96 |
return cite(question)
|
| 97 |
-
|
| 98 |
-
# Math evaluation
|
| 99 |
-
elif any(op in q for op in ["+", "-", "*", "/"]):
|
| 100 |
return python(question)
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
return
|
| 104 |
-
|
| 105 |
except Exception as e:
|
| 106 |
return f"Agent error: {str(e)}"
|
| 107 |
|
|
|
|
| 1 |
+
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
from duckduckgo_search import DDGS
|
| 7 |
from transformers import pipeline
|
| 8 |
from smolagents import tool
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
@tool
|
| 11 |
def web_search(query: str) -> str:
|
| 12 |
"""
|
|
|
|
| 28 |
return f"{snippet}\n\nSource: [{r['href']}]({r['href']})"
|
| 29 |
return "Could not find a direct answer from Wikipedia."
|
| 30 |
|
|
|
|
| 31 |
@tool
|
| 32 |
def cite(input: str) -> str:
|
| 33 |
"""
|
|
|
|
| 45 |
except:
|
| 46 |
return "Could not format citation."
|
| 47 |
|
|
|
|
| 48 |
@tool
|
| 49 |
def python(code: str) -> str:
|
| 50 |
"""
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
return f"Error: {str(e)}"
|
| 64 |
|
|
|
|
| 65 |
@tool
|
| 66 |
def fallback(_: str) -> str:
|
| 67 |
"""
|
|
|
|
| 75 |
"""
|
| 76 |
return "Sorry, I couldn't confidently answer that. Could you rephrase?"
|
| 77 |
|
|
|
|
| 78 |
class BasicAgent:
|
| 79 |
def __call__(self, question: str) -> str:
|
| 80 |
q = question.lower()
|
| 81 |
|
| 82 |
try:
|
| 83 |
+
if "|||" in question:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
return cite(question)
|
| 85 |
+
if any(op in q for op in ["+", "-", "*", "/"]) and any(c.isdigit() for c in q):
|
|
|
|
|
|
|
| 86 |
return python(question)
|
| 87 |
+
if len(q.split()) < 3:
|
| 88 |
+
return fallback(question)
|
| 89 |
+
return web_search(question)
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
return f"Agent error: {str(e)}"
|
| 92 |
|