Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import pandas as pd
|
|
6 |
from google import genai
|
7 |
from google.genai import types
|
8 |
import time
|
|
|
9 |
|
10 |
# (Keep Constants as is)
|
11 |
# --- Constants ---
|
@@ -22,19 +23,15 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
22 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
23 |
# return fixed_answer
|
24 |
|
25 |
-
class
|
26 |
-
def __init__(self):
|
27 |
-
|
28 |
-
|
29 |
-
# Set up Gemini API
|
30 |
-
api_key = os.environ.get("GEMINI_API_KEY")
|
31 |
if not api_key:
|
32 |
-
raise ValueError("GEMINI_API_KEY
|
33 |
-
|
34 |
-
os.environ["GOOGLE_API_KEY"] = api_key # Required by google-genai
|
35 |
|
|
|
36 |
self.client = genai.Client()
|
37 |
-
self.model_id =
|
38 |
|
39 |
self.generation_config = types.GenerateContentConfig(
|
40 |
temperature=0.4,
|
@@ -46,26 +43,45 @@ class BasicAgent:
|
|
46 |
frequency_penalty=0.0,
|
47 |
)
|
48 |
|
49 |
-
def
|
50 |
-
|
51 |
-
|
52 |
try:
|
53 |
-
response = self.client.
|
54 |
model=self.model_id,
|
55 |
-
contents=
|
56 |
-
|
57 |
)
|
58 |
-
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# class BasicAgent(ReActAgent):
|
70 |
# def __init__(self):
|
71 |
# print("BasicAgent using local LLM initialized.")
|
|
|
6 |
from google import genai
|
7 |
from google.genai import types
|
8 |
import time
|
9 |
+
from smolagents import CodeAgent, InferenceClientModel, load_tool, DuckDuckGoSearchTool
|
10 |
|
11 |
# (Keep Constants as is)
|
12 |
# --- Constants ---
|
|
|
23 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
24 |
# return fixed_answer
|
25 |
|
26 |
+
class GeminiModel:
|
27 |
+
def __init__(self, model_name="gemini-2.0-flash-exp"):
|
28 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
|
|
|
|
|
|
29 |
if not api_key:
|
30 |
+
raise ValueError("GEMINI_API_KEY is missing.")
|
|
|
|
|
31 |
|
32 |
+
os.environ["GOOGLE_API_KEY"] = api_key
|
33 |
self.client = genai.Client()
|
34 |
+
self.model_id = model_name
|
35 |
|
36 |
self.generation_config = types.GenerateContentConfig(
|
37 |
temperature=0.4,
|
|
|
43 |
frequency_penalty=0.0,
|
44 |
)
|
45 |
|
46 |
+
def run(self, prompt: str) -> str:
|
47 |
+
"""Send prompt to Gemini."""
|
|
|
48 |
try:
|
49 |
+
response = self.client.generate_content(
|
50 |
model=self.model_id,
|
51 |
+
contents=[{"role": "user", "parts": [{"text": prompt}]}],
|
52 |
+
generation_config=self.generation_config
|
53 |
)
|
54 |
+
return response.candidates[0].content.parts[0].text.strip()
|
55 |
+
except Exception as e:
|
56 |
+
return f"Error during Gemini call: {str(e)}"
|
57 |
|
58 |
+
# Define BasicAgent properly
|
59 |
+
class BasicAgent:
|
60 |
+
def __init__(self):
|
61 |
+
print("Initializing CodeAgent with Gemini + tools.")
|
62 |
|
63 |
+
# Load tools
|
64 |
+
self.search_tool = DuckDuckGoSearchTool()
|
65 |
+
|
66 |
+
# Build the agent
|
67 |
+
self.agent = CodeAgent(
|
68 |
+
tools=[self.search_tool],
|
69 |
+
model=GeminiModel(), # Our simple Gemini wrapper
|
70 |
+
planning_interval=3 # Activate planning
|
71 |
+
)
|
72 |
|
73 |
+
def __call__(self, question: str) -> str:
|
74 |
+
"""Call the CodeAgent."""
|
75 |
+
print(f"Running agent for task: {question[:50]}...")
|
76 |
+
try:
|
77 |
+
result = self.agent.run(question)
|
78 |
+
|
79 |
+
# Sleep to respect rate limits
|
80 |
+
time.sleep(7)
|
81 |
+
return result
|
82 |
+
except Exception as e:
|
83 |
+
return f"Error running agent: {str(e)}"
|
84 |
+
|
85 |
# class BasicAgent(ReActAgent):
|
86 |
# def __init__(self):
|
87 |
# print("BasicAgent using local LLM initialized.")
|