lukmanaj commited on
Commit
a242cf4
·
verified ·
1 Parent(s): 861f28d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -164
app.py CHANGED
@@ -3,174 +3,14 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- from google import genai
7
- from google.genai import types
8
  import time
9
- from smolagents import CodeAgent, DuckDuckGoSearchTool, WikipediaSearchTool, PythonInterpreterTool, FinalAnswerTool, LiteLLMModel, tool
10
 
11
 
12
  # (Keep Constants as is)
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
- # --- Basic Agent Definition ---
17
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
18
- # class BasicAgent:
19
- # def __init__(self):
20
- # print("BasicAgent initialized.")
21
- # def __call__(self, question: str) -> str:
22
- # print(f"Agent received question (first 50 chars): {question[:50]}...")
23
- # fixed_answer = "This is a default answer."
24
- # print(f"Agent returning fixed answer: {fixed_answer}")
25
- # return fixed_answer
26
-
27
- # class GeminiModel:
28
- # def __init__(self, model_name="gemini-2.0-flash-exp"):
29
- # api_key = os.getenv("GEMINI_API_KEY")
30
- # if not api_key:
31
- # raise ValueError("GEMINI_API_KEY is missing.")
32
-
33
- # os.environ["GOOGLE_API_KEY"] = api_key
34
- # self.client = genai.Client()
35
- # self.model_id = model_name
36
-
37
- # self.generation_config = types.GenerateContentConfig(
38
- # temperature=0.4,
39
- # top_p=0.9,
40
- # top_k=40,
41
- # candidate_count=1,
42
- # seed=42,
43
- # presence_penalty=0.0,
44
- # frequency_penalty=0.0,
45
- # )
46
-
47
- # def __call__(self, prompt: str, **kwargs) -> str:
48
- # """Send prompt to Gemini."""
49
- # try:
50
- # response = self.client.generate_content(
51
- # model=self.model_id,
52
- # contents=[{"role": "user", "parts": [{"text": prompt}]}],
53
- # generation_config=self.generation_config
54
- # )
55
- # # Return a dictionary that CodeAgent expects
56
- # return {"content": response.candidates[0].content.parts[0].text.strip()}
57
- # except Exception as e:
58
- # return {"content": f"Error during Gemini call: {str(e)}"}
59
-
60
- # # Define BasicAgent properly
61
- # class BasicAgent:
62
- # def __init__(self):
63
- # print("Initializing CodeAgent with Gemini + tools.")
64
-
65
- # # Load tools
66
- # self.search_tool = DuckDuckGoSearchTool()
67
-
68
- # # Build the agent
69
- # self.agent = CodeAgent(
70
- # tools=[self.search_tool],
71
- # model=GeminiModel(), # Our simple Gemini wrapper
72
- # planning_interval=3 # Activate planning
73
- # )
74
-
75
- # def __call__(self, question: str) -> str:
76
- # """Call the CodeAgent."""
77
- # print(f"Running agent for task: {question[:50]}...")
78
- # try:
79
- # result = self.agent.run(question)
80
-
81
- # # Sleep to respect rate limits
82
- # time.sleep(7)
83
- # return result
84
- # except Exception as e:
85
- # return f"Error running agent: {str(e)}"
86
-
87
- # class BasicAgent(ReActAgent):
88
- # def __init__(self):
89
- # print("BasicAgent using local LLM initialized.")
90
-
91
- # # Load a small model from Hugging Face
92
- # model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
93
- # self.tokenizer = AutoTokenizer.from_pretrained(model_name)
94
- # self.model = AutoModelForCausalLM.from_pretrained(
95
- # model_name,
96
- # torch_dtype=torch.float16,
97
- # device_map="auto" # Automatically choose GPU/CPU
98
- # )
99
-
100
- # super().__init__(tools=[]) # No tools for now
101
-
102
- # def call(self, task: str) -> str:
103
- # """Core method for answering a task."""
104
- # prompt = f"Answer the following question concisely:\n\n{task}\n\nAnswer:"
105
- # inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
106
-
107
- # with torch.no_grad():
108
- # outputs = self.model.generate(
109
- # **inputs,
110
- # max_new_tokens=200,
111
- # do_sample=True,
112
- # temperature=0.7,
113
- # top_p=0.95,
114
- # top_k=50,
115
- # )
116
- # answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
117
-
118
- # # Extract only the answer part
119
- # return answer.split("Answer:")[-1].strip()
120
-
121
- # class BasicAgent:
122
- # def __init__(self):
123
- # print("BasicAgent using local LLM initialized.")
124
-
125
- # # Load a small Hugging Face model
126
- # model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" # Change if you want
127
- # self.tokenizer = AutoTokenizer.from_pretrained(model_name)
128
- # self.model = AutoModelForCausalLM.from_pretrained(
129
- # model_name,
130
- # torch_dtype=torch.float16,
131
- # device_map="auto" # Use GPU if available
132
- # )
133
-
134
- # def __call__(self, task: str) -> str:
135
- # """Answer a question."""
136
- # prompt = f"Answer the following question clearly and concisely:\n\n{task}\n\nAnswer:"
137
- # inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
138
-
139
- # with torch.no_grad():
140
- # outputs = self.model.generate(
141
- # **inputs,
142
- # max_new_tokens=256,
143
- # do_sample=True,
144
- # temperature=0.7,
145
- # top_p=0.9,
146
- # top_k=50,
147
- # )
148
- # decoded = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
149
-
150
- # # Extract the answer part
151
- # if "Answer:" in decoded:
152
- # return decoded.split("Answer:")[-1].strip()
153
- # return decoded.strip()
154
-
155
-
156
-
157
- # # Setup Gemini Client
158
- # api_key = os.getenv("GEMINI_API_KEY")
159
- # if not api_key:
160
- # raise ValueError("GEMINI_API_KEY is missing.")
161
- # os.environ["GOOGLE_API_KEY"] = api_key
162
- # client = genai.Client()
163
- # model_id = "gemini-2.0-flash-exp"
164
-
165
- # generation_config = types.GenerateContentConfig(
166
- # temperature=0.4,
167
- # top_p=0.9,
168
- # top_k=40,
169
- # candidate_count=1,
170
- # seed=42,
171
- # presence_penalty=0.0,
172
- # frequency_penalty=0.0,
173
- # )
174
 
175
 
176
  # Define the real agent
@@ -178,7 +18,7 @@ class BasicAgent:
178
  def __init__(self):
179
  print("Improved BasicAgent initialized with Gemini and enhanced tools.")
180
 
181
- # Load Gemini through LiteLLM (ensure GEMINI_API_TOKEN is set)
182
  self.model = LiteLLMModel(
183
  model_id="gemini/gemini-2.0-flash-lite",
184
  api_key=os.getenv("GEMINI_API_TOKEN"),
@@ -219,8 +59,8 @@ class BasicAgent:
219
  result = f"Error: {str(e)}"
220
 
221
  # Rate limiting to avoid 429 errors or API limits
222
- print("Waiting 3 seconds to respect rate limits...")
223
- time.sleep(3)
224
 
225
  return result
226
 
 
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
6
  import time
7
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, WikipediaSearchTool, PythonInterpreterTool, FinalAnswerTool, LiteLLMModel
8
 
9
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
 
16
  # Define the real agent
 
18
  def __init__(self):
19
  print("Improved BasicAgent initialized with Gemini and enhanced tools.")
20
 
21
+ # Load Gemini through LiteLLM
22
  self.model = LiteLLMModel(
23
  model_id="gemini/gemini-2.0-flash-lite",
24
  api_key=os.getenv("GEMINI_API_TOKEN"),
 
59
  result = f"Error: {str(e)}"
60
 
61
  # Rate limiting to avoid 429 errors or API limits
62
+ print("Waiting 5 seconds to respect rate limits...")
63
+ time.sleep(5)
64
 
65
  return result
66