wlchee commited on
Commit
66cde51
·
verified ·
1 Parent(s): 0e5f4ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -47
app.py CHANGED
@@ -3,74 +3,83 @@ import gradio as gr
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"
11
 
12
- # --- Custom Tools ---
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:
24
- return "Error: Could not evaluate the expression"
25
 
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:
53
  print(f"Processing: {question[:100]}...")
54
  question_lower = question.lower()
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,7 +91,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
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,20 +137,20 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
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()
138
- run_btn = gr.Button("Start Evaluation")
139
  output = gr.Textbox(label="Results")
140
- results = gr.DataFrame(label="Details")
141
 
142
  run_btn.click(
143
  fn=run_and_submit_all,
144
- outputs=[output, results]
145
  )
146
 
147
  if __name__ == "__main__":
 
3
  import requests
4
  import pandas as pd
5
  from datetime import datetime
6
+ from smolagents import Tool, ToolCallingAgent
7
+ from smolagents.models import InferenceClientModel
8
 
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
+ # --- Custom Tools with Proper Input/Output Specifications ---
13
  class CalculatorTool(Tool):
14
  name = "calculator"
15
  description = "Performs mathematical calculations"
16
+ input_schema = {
17
+ "expression": {
18
+ "type": "string",
19
+ "description": "Mathematical expression to evaluate (e.g., '2+2')"
20
+ }
21
+ }
22
+ output_schema = {
23
+ "result": {
24
+ "type": "string",
25
+ "description": "The calculated result of the expression"
26
+ }
27
+ }
28
+
29
+ def use(self, expression: str) -> dict:
30
  try:
31
+ return {"result": str(eval(expression))}
32
+ except Exception as e:
33
+ return {"result": f"Error: {str(e)}"}
34
 
35
  class TimeTool(Tool):
36
  name = "current_time"
37
  description = "Gets current UTC time"
38
+ input_schema = {} # No inputs needed
39
+ output_schema = {
40
+ "time": {
41
+ "type": "string",
42
+ "description": "Current time in UTC (YYYY-MM-DD HH:MM:SS)"
43
+ }
44
+ }
45
+
46
+ def use(self) -> dict:
47
+ return {"time": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")}
48
 
49
  # --- Enhanced Agent ---
50
+ class LocalAgent:
51
  def __init__(self):
52
+ print("Initializing agent with smolagents...")
53
+ self.tools = [CalculatorTool(), TimeTool()]
 
 
 
54
 
55
+ # Using a free Hugging Face model
56
+ self.agent = ToolCallingAgent(
57
+ tools=self.tools,
58
+ model=InferenceClientModel(
59
+ model_id="HuggingFaceH4/zephyr-7b-beta",
60
+ api_base="https://api-inference.huggingface.co/models"
61
+ )
62
  )
63
 
64
  def __call__(self, question: str) -> str:
65
  print(f"Processing: {question[:100]}...")
66
  question_lower = question.lower()
67
 
68
+ # Direct tool usage for simple queries
69
  if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
70
+ result = CalculatorTool().use(question.replace("?", ""))
71
+ return result["result"]
72
 
 
73
  if any(word in question_lower for word in ["time", "current time"]):
74
+ result = TimeTool().use()
75
+ return result["time"]
76
 
77
+ # Use full agent for complex questions
78
  try:
79
+ response = self.agent.run(question)
80
+ return str(response)
 
 
 
 
81
  except Exception as e:
82
+ print(f"Agent error: {e}")
83
  return "I couldn't process this question."
84
 
85
  # --- Evaluation Runner ---
 
91
  api_url = os.getenv("API_URL", DEFAULT_API_URL)
92
 
93
  try:
94
+ agent = LocalAgent()
95
  response = requests.get(f"{api_url}/questions", timeout=15)
96
  response.raise_for_status()
97
  questions = response.json()
 
137
  return f"Evaluation failed: {str(e)}", pd.DataFrame(results if 'results' in locals() else [])
138
 
139
  # --- Gradio Interface ---
140
+ with gr.Blocks(title="Agent Evaluation Runner") as app:
141
  gr.Markdown("""
142
+ ## Advanced Agent Evaluation
143
+ Uses smolagents with proper tool schemas
144
  """)
145
 
146
  gr.LoginButton()
147
+ run_btn = gr.Button("Run Evaluation")
148
  output = gr.Textbox(label="Results")
149
+ results_table = gr.DataFrame(label="Question Log")
150
 
151
  run_btn.click(
152
  fn=run_and_submit_all,
153
+ outputs=[output, results_table]
154
  )
155
 
156
  if __name__ == "__main__":