lukmanaj commited on
Commit
0a59064
·
verified ·
1 Parent(s): 1541de7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -24
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 BasicAgent:
26
- def __init__(self):
27
- print("CustomAgent using Gemini 2.0 initialized.")
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 not found in environment variables.")
33
-
34
- os.environ["GOOGLE_API_KEY"] = api_key # Required by google-genai
35
 
 
36
  self.client = genai.Client()
37
- self.model_id = "gemini-2.0-flash-exp" # Or "gemini-1.5-flash-002" if you want faster
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 __call__(self, question: str) -> str:
50
- print(f"Agent received question (first 50 chars): {question[:50]}...")
51
-
52
  try:
53
- response = self.client.models.generate_content(
54
  model=self.model_id,
55
- contents=f"""You are a smart, factual assistant. Answer clearly and concisely:\n\n{question}\n\nProvide only the final answer without extra commentary.""",
56
- config=self.generation_config,
57
  )
58
- answer = response.text.strip()
 
 
59
 
60
- # Add a short sleep to avoid hitting rate limits
61
- time.sleep(7) # Wait 7 seconds after each question
62
- print(f"Returning answer (first 100 chars): {answer[:100]}")
63
- return answer
64
 
65
- except Exception as e:
66
- print(f"Error during Gemini call: {str(e)}")
67
- return f"Error: {str(e)}"
 
 
 
 
 
 
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.")