avi080704 commited on
Commit
f9dc849
·
verified ·
1 Parent(s): 837dabf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -260
app.py CHANGED
@@ -1,46 +1,17 @@
1
- import os
2
- import re
3
- import gradio as gr
4
- import requests
5
- import pandas as pd
6
-
7
- from smolagents import CodeAgent, DuckDuckGoSearchTool
8
- from smolagents.models import OpenAIServerModel
9
-
10
- # ---------------------------------------------------
11
- # Constants
12
- # ---------------------------------------------------
13
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
-
15
- # ---------------------------------------------------
16
- # Agent
17
- # ---------------------------------------------------
18
  class BasicAgent:
19
 
20
  def __init__(self):
21
 
22
- print("Initializing Groq Agent...")
23
 
24
  self.model = OpenAIServerModel(
25
- model_id="llama-3.3-70b-versatile",
26
  api_base="https://api.groq.com/openai/v1",
27
  api_key=os.getenv("GROQ_API_KEY")
28
  )
29
 
30
- self.agent = CodeAgent(
31
- tools=[
32
- DuckDuckGoSearchTool()
33
- ],
34
- model=self.model,
35
- max_steps=3,
36
- verbosity_level=0
37
- )
38
-
39
- print("Groq Agent initialized.")
40
 
41
- # ---------------------------------------------------
42
- # Clean outputs for exact-match grading
43
- # ---------------------------------------------------
44
  def clean_answer(self, text):
45
 
46
  if text is None:
@@ -48,38 +19,24 @@ class BasicAgent:
48
 
49
  text = str(text)
50
 
51
- # remove markdown/code
52
  text = text.replace("```", "")
53
  text = text.replace("FINAL ANSWER:", "")
54
  text = text.replace("Answer:", "")
55
 
56
- # remove excessive whitespace
57
- text = re.sub(r"\s+", " ", text)
58
 
59
- # first line only
60
- text = text.split("\n")[0]
61
 
62
- # concise
63
- text = text.strip()
64
-
65
- return text[:300]
66
-
67
- # ---------------------------------------------------
68
- # Run agent
69
- # ---------------------------------------------------
70
  def __call__(self, question: str) -> str:
71
 
72
  prompt = f"""
73
- You are solving a GAIA benchmark question.
74
 
75
  IMPORTANT:
76
  - Return ONLY the final answer.
77
  - No reasoning.
78
- - No explanations.
79
- - No markdown.
80
- - No bullet points.
81
- - Be concise and accurate.
82
- - Use web search if needed.
83
 
84
  Question:
85
  {question}
@@ -87,220 +44,19 @@ Question:
87
 
88
  try:
89
 
90
- result = self.agent.run(prompt)
 
 
 
91
 
92
- cleaned = self.clean_answer(result)
93
 
94
- print(f"\nQUESTION:\n{question}")
95
- print(f"\nANSWER:\n{cleaned}")
96
 
97
  return cleaned
98
 
99
  except Exception as e:
100
 
101
- print(f"Agent error: {e}")
102
-
103
- return ""
104
-
105
-
106
- # ---------------------------------------------------
107
- # Evaluation + Submission
108
- # ---------------------------------------------------
109
- def run_and_submit_all(profile: gr.OAuthProfile | None):
110
-
111
- space_id = os.getenv("SPACE_ID")
112
-
113
- # Login check
114
- if profile:
115
- username = profile.username
116
- print(f"Logged in as: {username}")
117
- else:
118
- return "Please login with Hugging Face.", None
119
-
120
- api_url = DEFAULT_API_URL
121
- questions_url = f"{api_url}/questions"
122
- submit_url = f"{api_url}/submit"
123
-
124
- # ---------------------------------------------------
125
- # Initialize Agent
126
- # ---------------------------------------------------
127
- try:
128
-
129
- agent = BasicAgent()
130
-
131
- except Exception as e:
132
-
133
- print(f"Initialization error: {e}")
134
-
135
- return f"Error initializing agent: {e}", None
136
-
137
- # Space repo link
138
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
139
-
140
- # ---------------------------------------------------
141
- # Fetch Questions
142
- # ---------------------------------------------------
143
- try:
144
-
145
- print("Fetching questions...")
146
-
147
- response = requests.get(
148
- questions_url,
149
- timeout=30
150
- )
151
-
152
- response.raise_for_status()
153
-
154
- questions_data = response.json()
155
-
156
- print(f"Fetched {len(questions_data)} questions.")
157
-
158
- except Exception as e:
159
-
160
- print(f"Question fetch error: {e}")
161
-
162
- return f"Error fetching questions: {e}", None
163
-
164
- # ---------------------------------------------------
165
- # Run Agent
166
- # ---------------------------------------------------
167
- answers_payload = []
168
- results_log = []
169
-
170
- for item in questions_data:
171
-
172
- task_id = item.get("task_id")
173
- question_text = item.get("question")
174
-
175
- if not task_id or question_text is None:
176
- continue
177
-
178
- print(f"\nRunning task {task_id}")
179
-
180
- try:
181
-
182
- submitted_answer = agent(question_text)
183
-
184
- answers_payload.append({
185
- "task_id": task_id,
186
- "submitted_answer": submitted_answer
187
- })
188
-
189
- results_log.append({
190
- "Task ID": task_id,
191
- "Question": question_text,
192
- "Submitted Answer": submitted_answer
193
- })
194
-
195
- except Exception as e:
196
-
197
- print(f"Task error: {e}")
198
-
199
- results_log.append({
200
- "Task ID": task_id,
201
- "Question": question_text,
202
- "Submitted Answer": f"ERROR: {e}"
203
- })
204
-
205
- # ---------------------------------------------------
206
- # Submit
207
- # ---------------------------------------------------
208
- submission_data = {
209
- "username": username.strip(),
210
- "agent_code": agent_code,
211
- "answers": answers_payload
212
- }
213
-
214
- try:
215
-
216
- print("Submitting answers...")
217
-
218
- response = requests.post(
219
- submit_url,
220
- json=submission_data,
221
- timeout=120
222
- )
223
-
224
- response.raise_for_status()
225
-
226
- result_data = response.json()
227
-
228
- final_status = (
229
- f"Submission Successful!\n\n"
230
- f"User: {result_data.get('username')}\n"
231
- f"Score: {result_data.get('score', 'N/A')}%\n"
232
- f"Correct: "
233
- f"{result_data.get('correct_count', '?')}/"
234
- f"{result_data.get('total_attempted', '?')}\n\n"
235
- f"Message: {result_data.get('message', '')}"
236
- )
237
-
238
- print(final_status)
239
-
240
- return final_status, pd.DataFrame(results_log)
241
-
242
- except Exception as e:
243
-
244
- error_message = f"Submission failed: {e}"
245
-
246
- print(error_message)
247
-
248
- return error_message, pd.DataFrame(results_log)
249
-
250
-
251
- # ---------------------------------------------------
252
- # UI
253
- # ---------------------------------------------------
254
- with gr.Blocks() as demo:
255
-
256
- gr.Markdown("# Hugging Face Agents Course - Final Assignment")
257
-
258
- gr.Markdown(
259
- """
260
- This agent uses:
261
-
262
- - Groq API
263
- - Llama 3.3 70B
264
- - smolagents
265
- - DuckDuckGo Search
266
- """
267
- )
268
-
269
- gr.LoginButton()
270
-
271
- run_button = gr.Button(
272
- "Run Evaluation & Submit"
273
- )
274
-
275
- status_output = gr.Textbox(
276
- label="Submission Result",
277
- lines=8,
278
- interactive=False
279
- )
280
-
281
- results_table = gr.DataFrame(
282
- label="Agent Answers",
283
- wrap=True
284
- )
285
-
286
- run_button.click(
287
- fn=run_and_submit_all,
288
- outputs=[
289
- status_output,
290
- results_table
291
- ]
292
- )
293
-
294
- # ---------------------------------------------------
295
- # Launch
296
- # ---------------------------------------------------
297
- if __name__ == "__main__":
298
-
299
- print("\n==============================")
300
- print("Starting Groq Agent...")
301
- print("==============================\n")
302
 
303
- demo.launch(
304
- debug=True,
305
- share=False
306
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  class BasicAgent:
2
 
3
  def __init__(self):
4
 
5
+ print("Initializing Fast Groq Agent...")
6
 
7
  self.model = OpenAIServerModel(
8
+ model_id="llama-3.1-8b-instant",
9
  api_base="https://api.groq.com/openai/v1",
10
  api_key=os.getenv("GROQ_API_KEY")
11
  )
12
 
13
+ print("Fast agent ready.")
 
 
 
 
 
 
 
 
 
14
 
 
 
 
15
  def clean_answer(self, text):
16
 
17
  if text is None:
 
19
 
20
  text = str(text)
21
 
 
22
  text = text.replace("```", "")
23
  text = text.replace("FINAL ANSWER:", "")
24
  text = text.replace("Answer:", "")
25
 
26
+ text = text.strip().split("\n")[0]
 
27
 
28
+ return text[:200]
 
29
 
 
 
 
 
 
 
 
 
30
  def __call__(self, question: str) -> str:
31
 
32
  prompt = f"""
33
+ Answer the question.
34
 
35
  IMPORTANT:
36
  - Return ONLY the final answer.
37
  - No reasoning.
38
+ - No explanation.
39
+ - Keep answers concise.
 
 
 
40
 
41
  Question:
42
  {question}
 
44
 
45
  try:
46
 
47
+ response = self.model(
48
+ prompt,
49
+ max_tokens=80
50
+ )
51
 
52
+ cleaned = self.clean_answer(response)
53
 
54
+ print(cleaned)
 
55
 
56
  return cleaned
57
 
58
  except Exception as e:
59
 
60
+ print(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ return ""