Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
from datetime import datetime
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -13,8 +13,11 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
class CalculatorTool(Tool):
|
| 14 |
name = "calculator"
|
| 15 |
description = "Performs mathematical calculations"
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
def
|
| 18 |
try:
|
| 19 |
return str(eval(expression))
|
| 20 |
except:
|
|
@@ -23,26 +26,27 @@ class CalculatorTool(Tool):
|
|
| 23 |
class TimeTool(Tool):
|
| 24 |
name = "current_time"
|
| 25 |
description = "Gets current UTC time"
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
def
|
| 28 |
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 29 |
|
| 30 |
# --- Enhanced Agent ---
|
| 31 |
-
class
|
| 32 |
def __init__(self):
|
| 33 |
-
print("Initializing local
|
| 34 |
self.tools = {
|
| 35 |
"calculator": CalculatorTool(),
|
| 36 |
"time": TimeTool()
|
| 37 |
}
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
self.
|
| 41 |
-
|
| 42 |
-
model=
|
| 43 |
-
|
| 44 |
-
api_base="https://api-inference.huggingface.co/models"
|
| 45 |
-
)
|
| 46 |
)
|
| 47 |
|
| 48 |
def __call__(self, question: str) -> str:
|
|
@@ -51,17 +55,22 @@ class LocalAgent:
|
|
| 51 |
|
| 52 |
# Math questions
|
| 53 |
if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
|
| 54 |
-
return self.tools["calculator"]
|
| 55 |
|
| 56 |
# Time questions
|
| 57 |
if any(word in question_lower for word in ["time", "current time"]):
|
| 58 |
-
return self.tools["time"]
|
| 59 |
|
| 60 |
-
# Fallback to
|
| 61 |
try:
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
-
print(f"
|
| 65 |
return "I couldn't process this question."
|
| 66 |
|
| 67 |
# --- Evaluation Runner ---
|
|
@@ -73,7 +82,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 73 |
api_url = os.getenv("API_URL", DEFAULT_API_URL)
|
| 74 |
|
| 75 |
try:
|
| 76 |
-
agent =
|
| 77 |
response = requests.get(f"{api_url}/questions", timeout=15)
|
| 78 |
response.raise_for_status()
|
| 79 |
questions = response.json()
|
|
@@ -119,10 +128,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 119 |
return f"Evaluation failed: {str(e)}", pd.DataFrame(results if 'results' in locals() else [])
|
| 120 |
|
| 121 |
# --- Gradio Interface ---
|
| 122 |
-
with gr.Blocks(title="Local Agent Evaluator") as app:
|
| 123 |
gr.Markdown("""
|
| 124 |
-
## Local Agent Evaluation
|
| 125 |
-
Uses
|
| 126 |
""")
|
| 127 |
|
| 128 |
gr.LoginButton()
|
|
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
from datetime import datetime
|
| 6 |
+
from transformers import pipeline, Tool
|
| 7 |
+
from transformers.agents import Agent
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 13 |
class CalculatorTool(Tool):
|
| 14 |
name = "calculator"
|
| 15 |
description = "Performs mathematical calculations"
|
| 16 |
+
inputs = {"expression": {"type": "text"}}
|
| 17 |
+
outputs = {"result": {"type": "text"}}
|
| 18 |
+
output_type = "text" # Added required attribute
|
| 19 |
|
| 20 |
+
def __call__(self, expression: str) -> str:
|
| 21 |
try:
|
| 22 |
return str(eval(expression))
|
| 23 |
except:
|
|
|
|
| 26 |
class TimeTool(Tool):
|
| 27 |
name = "current_time"
|
| 28 |
description = "Gets current UTC time"
|
| 29 |
+
inputs = {}
|
| 30 |
+
outputs = {"time": {"type": "text"}}
|
| 31 |
+
output_type = "text" # Added required attribute
|
| 32 |
|
| 33 |
+
def __call__(self) -> str:
|
| 34 |
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 35 |
|
| 36 |
# --- Enhanced Agent ---
|
| 37 |
+
class HFLocalAgent:
|
| 38 |
def __init__(self):
|
| 39 |
+
print("Initializing local Hugging Face agent...")
|
| 40 |
self.tools = {
|
| 41 |
"calculator": CalculatorTool(),
|
| 42 |
"time": TimeTool()
|
| 43 |
}
|
| 44 |
|
| 45 |
+
# Load local model (small but efficient)
|
| 46 |
+
self.llm = pipeline(
|
| 47 |
+
"text-generation",
|
| 48 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 49 |
+
device="cpu" # Change to "cuda" if GPU available
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
def __call__(self, question: str) -> str:
|
|
|
|
| 55 |
|
| 56 |
# Math questions
|
| 57 |
if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
|
| 58 |
+
return self.tools["calculator"](question.replace("?", ""))
|
| 59 |
|
| 60 |
# Time questions
|
| 61 |
if any(word in question_lower for word in ["time", "current time"]):
|
| 62 |
+
return self.tools["time"]()
|
| 63 |
|
| 64 |
+
# Fallback to local LLM
|
| 65 |
try:
|
| 66 |
+
response = self.llm(
|
| 67 |
+
f"Answer concisely: {question}",
|
| 68 |
+
max_new_tokens=100,
|
| 69 |
+
temperature=0.7
|
| 70 |
+
)
|
| 71 |
+
return response[0]['generated_text'].split(":")[-1].strip()
|
| 72 |
except Exception as e:
|
| 73 |
+
print(f"LLM error: {e}")
|
| 74 |
return "I couldn't process this question."
|
| 75 |
|
| 76 |
# --- Evaluation Runner ---
|
|
|
|
| 82 |
api_url = os.getenv("API_URL", DEFAULT_API_URL)
|
| 83 |
|
| 84 |
try:
|
| 85 |
+
agent = HFLocalAgent()
|
| 86 |
response = requests.get(f"{api_url}/questions", timeout=15)
|
| 87 |
response.raise_for_status()
|
| 88 |
questions = response.json()
|
|
|
|
| 128 |
return f"Evaluation failed: {str(e)}", pd.DataFrame(results if 'results' in locals() else [])
|
| 129 |
|
| 130 |
# --- Gradio Interface ---
|
| 131 |
+
with gr.Blocks(title="Local HF Agent Evaluator") as app:
|
| 132 |
gr.Markdown("""
|
| 133 |
+
## Local Hugging Face Agent Evaluation
|
| 134 |
+
Uses completely free/local models - no API keys required
|
| 135 |
""")
|
| 136 |
|
| 137 |
gr.LoginButton()
|