Update app.py
#361
by unworthyslaveTOCHRIST - opened
app.py
CHANGED
|
@@ -3,21 +3,117 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import re
|
| 7 |
+
import wikipedia
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
from sympy import sympify
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
# --- Basic Agent Definition ---
|
| 16 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------c
|
| 17 |
class BasicAgent:
|
| 18 |
+
|
| 19 |
def __init__(self):
|
| 20 |
+
print("Initializing production agent")
|
| 21 |
+
|
| 22 |
+
self.llm = pipeline(
|
| 23 |
+
"text2text-generation",
|
| 24 |
+
model="google/flan-t5-base",
|
| 25 |
+
max_new_tokens=200
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
print("Agent ready")
|
| 29 |
+
|
| 30 |
+
# TOOL 1 - Wikipedia Search
|
| 31 |
+
def search_wikipedia(self, query):
|
| 32 |
+
try:
|
| 33 |
+
summary = wikipedia.summary(
|
| 34 |
+
query,
|
| 35 |
+
sentences=2
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
return summary
|
| 39 |
+
except Exception:
|
| 40 |
+
return "No information found"
|
| 41 |
+
|
| 42 |
+
# TOOL 2 - Math Solver
|
| 43 |
+
def solve_math(self, expression):
|
| 44 |
+
try:
|
| 45 |
+
result = sympify(expression)
|
| 46 |
+
return str(result)
|
| 47 |
+
except Exception:
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# TOOL 3 - Detector
|
| 52 |
+
def detect_math(self, question):
|
| 53 |
+
math_pattern = r"[0-9\+\-\*\/\(\)\.]"
|
| 54 |
+
return bool(re.search(math_pattern, question))
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# MAIN AGENT LOGIC
|
| 58 |
+
def __call__(self, question:str) -> str:
|
| 59 |
+
print(f"Question: {question}")
|
| 60 |
+
|
| 61 |
+
question_lower = question.lower()
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
# Using math tool
|
| 65 |
+
if self.detect_math(question):
|
| 66 |
+
cleaned = re.sub(
|
| 67 |
+
r"[^0-9\+\-\*\/\(\)\.]",
|
| 68 |
+
"",
|
| 69 |
+
question
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
result = self.solve_math(cleaned)
|
| 73 |
+
if result:
|
| 74 |
+
print("Math tool used")
|
| 75 |
+
return result
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
if any(
|
| 79 |
+
keyword in question_lower
|
| 80 |
+
for keyword in [
|
| 81 |
+
"who",
|
| 82 |
+
"what",
|
| 83 |
+
"when",
|
| 84 |
+
"where"
|
| 85 |
+
]
|
| 86 |
+
):
|
| 87 |
+
topic = question
|
| 88 |
+
|
| 89 |
+
info = self.search_wikipedia(topic)
|
| 90 |
+
|
| 91 |
+
if info != "No information found.":
|
| 92 |
+
print("Wikipedia tool used")
|
| 93 |
+
return info
|
| 94 |
+
|
| 95 |
+
prompt = f"""
|
| 96 |
+
Answer the question clearly and concisely
|
| 97 |
+
|
| 98 |
+
Question:
|
| 99 |
+
{question}
|
| 100 |
+
|
| 101 |
+
Answer:
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
response = self.llm(prompt)[0]["generated_text"]
|
| 105 |
+
answer = response.strip()
|
| 106 |
+
print("LLM used")
|
| 107 |
+
return answer
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print("Agent error:", e)
|
| 111 |
+
return "Unable to answer the question."
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
|
| 118 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 119 |
"""
|