Anil commited on
Commit
e074e7e
·
1 Parent(s): 81917a3

Update app.py with latest changes

Browse files
Files changed (1) hide show
  1. app.py +115 -5
app.py CHANGED
@@ -7,17 +7,127 @@ import pandas as pd
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("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
+ OPENROUTER_API_KEY=os.getenv("OPENROUTER_API_KEY") # Get OpenRouter API key from environment variable
11
 
12
  # --- Basic Agent Definition ---
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
+ from smolagents import (
15
+ CodeAgent,
16
+ LiteLLMModel,
17
+ tool,
18
+ DuckDuckGoSearchTool,
19
+ VisitWebpageTool,
20
+ PythonInterpreterTool
21
+ )
22
+
23
+ import wikipedia
24
+
25
+
26
+ # ----------------------------
27
+ # Custom Tool 1 - Wikipedia
28
+ # ----------------------------
29
+ @tool
30
+ def wikipedia_search(query: str) -> str:
31
+ """
32
+ Search Wikipedia and return a concise summary.
33
+ """
34
+ try:
35
+ return wikipedia.summary(query, sentences=5)
36
+ except Exception as e:
37
+ return f"Wikipedia error: {e}"
38
+
39
+
40
+ # ----------------------------
41
+ # Custom Tool 2 - Current Date
42
+ # ----------------------------
43
+ @tool
44
+ def current_date() -> str:
45
+ """
46
+ Returns the current date and time.
47
+ """
48
+ from datetime import datetime
49
+
50
+ return datetime.utcnow().isoformat()
51
+
52
+
53
+ # ----------------------------
54
+ # Custom Tool 3 - Calculator
55
+ # ----------------------------
56
+ @tool
57
+ def calculator(expression: str) -> str:
58
+ """
59
+ Evaluate a mathematical expression.
60
+ Example:
61
+ 2+2
62
+ (5*7)/3
63
+ """
64
+ try:
65
+ return str(eval(expression))
66
+ except Exception as e:
67
+ return f"Calculation error: {e}"
68
+
69
+
70
+ # ----------------------------
71
+ # Basic Agent
72
+ # ----------------------------
73
  class BasicAgent:
74
+
75
  def __init__(self):
76
+
77
+ print("Initializing SmolAgent...")
78
+
79
+ self.model = LiteLLMModel(
80
+ model_id="openrouter/anthropic/claude-sonnet-4"
81
+ # alternatively:
82
+ # model_id="openrouter/openai/gpt-4.1"
83
+ )
84
+
85
+ self.agent = CodeAgent(
86
+ tools=[
87
+ DuckDuckGoSearchTool(),
88
+ VisitWebpageTool(),
89
+ PythonInterpreterTool(),
90
+ wikipedia_search,
91
+ current_date,
92
+ calculator,
93
+ ],
94
+ model=self.model,
95
+ add_base_tools=True,
96
+ planning_interval=3,
97
+ max_steps=15
98
+ )
99
+
100
+ print("SmolAgent ready.")
101
+
102
  def __call__(self, question: str) -> str:
103
+
104
+ print(f"Question: {question[:100]}")
105
+
106
+ try:
107
+
108
+ answer = self.agent.run(
109
+ f"""
110
+ You are solving a benchmark task.
111
+
112
+ Use tools whenever necessary.
113
+ Search before guessing.
114
+ Use Python for calculations.
115
+ Verify facts using search.
116
+
117
+ Question:
118
+ {question}
119
+
120
+ Return only the final answer.
121
+ """
122
+ )
123
+
124
+ return str(answer)
125
+
126
+ except Exception as e:
127
+
128
+ print(f"Agent error: {e}")
129
+
130
+ return f"Agent Error: {e}"
131
 
132
  def run_and_submit_all( profile: gr.OAuthProfile | None):
133
  """