yoshizen commited on
Commit
eafab09
·
verified ·
1 Parent(s): 0ac6b59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -425
app.py CHANGED
@@ -1,479 +1,186 @@
1
  """
2
- GAIA Test Application for Hugging Face Agents Course Certification
3
 
4
- This application provides a simulated GAIA benchmark test environment with 20 questions
5
- from Level 1 of the GAIA benchmark. It allows users to practice for the certification
6
- test and automatically calculates their score.
7
  """
8
 
9
  import os
 
10
  import json
11
- import gradio as gr
12
  import requests
13
- from typing import List, Dict, Any, Tuple
14
 
15
- # Sample GAIA Level 1 questions (20 questions for certification practice)
16
- GAIA_SAMPLE_QUESTIONS = [
17
- {
18
- "id": "gaia_l1_001",
19
- "question": "What is the capital city of Japan?",
20
- "answer": "Tokyo",
21
- "category": "factual",
22
- "difficulty": "easy"
23
- },
24
- {
25
- "id": "gaia_l1_002",
26
- "question": "Calculate the area of a circle with radius 5 cm. Express your answer in square centimeters with one decimal place.",
27
- "answer": "78.5",
28
- "category": "mathematical",
29
- "difficulty": "easy"
30
- },
31
- {
32
- "id": "gaia_l1_003",
33
- "question": "Who wrote the novel '1984'?",
34
- "answer": "George Orwell",
35
- "category": "factual",
36
- "difficulty": "easy"
37
- },
38
- {
39
- "id": "gaia_l1_004",
40
- "question": "What is the chemical symbol for gold?",
41
- "answer": "Au",
42
- "category": "factual",
43
- "difficulty": "easy"
44
- },
45
- {
46
- "id": "gaia_l1_005",
47
- "question": "If a train travels at 60 miles per hour, how far will it travel in 2.5 hours?",
48
- "answer": "150 miles",
49
- "category": "mathematical",
50
- "difficulty": "easy"
51
- },
52
- {
53
- "id": "gaia_l1_006",
54
- "question": "What is the largest planet in our solar system?",
55
- "answer": "Jupiter",
56
- "category": "factual",
57
- "difficulty": "easy"
58
- },
59
- {
60
- "id": "gaia_l1_007",
61
- "question": "Calculate 15% of 240.",
62
- "answer": "36",
63
- "category": "mathematical",
64
- "difficulty": "easy"
65
- },
66
- {
67
- "id": "gaia_l1_008",
68
- "question": "Who painted the Mona Lisa?",
69
- "answer": "Leonardo da Vinci",
70
- "category": "factual",
71
- "difficulty": "easy"
72
- },
73
- {
74
- "id": "gaia_l1_009",
75
- "question": "What is the boiling point of water in degrees Celsius at standard atmospheric pressure?",
76
- "answer": "100",
77
- "category": "factual",
78
- "difficulty": "easy"
79
- },
80
- {
81
- "id": "gaia_l1_010",
82
- "question": "If x + 3 = 8, what is the value of x?",
83
- "answer": "5",
84
- "category": "mathematical",
85
- "difficulty": "easy"
86
- },
87
- {
88
- "id": "gaia_l1_011",
89
- "question": "What is the main ingredient in guacamole?",
90
- "answer": "Avocado",
91
- "category": "factual",
92
- "difficulty": "medium"
93
- },
94
- {
95
- "id": "gaia_l1_012",
96
- "question": "If a rectangle has a length of 12 cm and a width of 8 cm, what is its perimeter?",
97
- "answer": "40 cm",
98
- "category": "mathematical",
99
- "difficulty": "medium"
100
- },
101
- {
102
- "id": "gaia_l1_013",
103
- "question": "Who was the first person to walk on the moon?",
104
- "answer": "Neil Armstrong",
105
- "category": "factual",
106
- "difficulty": "medium"
107
- },
108
- {
109
- "id": "gaia_l1_014",
110
- "question": "What is the square root of 144?",
111
- "answer": "12",
112
- "category": "mathematical",
113
- "difficulty": "medium"
114
- },
115
- {
116
- "id": "gaia_l1_015",
117
- "question": "Which element has the chemical symbol 'O'?",
118
- "answer": "Oxygen",
119
- "category": "factual",
120
- "difficulty": "medium"
121
- },
122
- {
123
- "id": "gaia_l1_016",
124
- "question": "A store is having a 30% off sale. If an item originally costs $85, what is the sale price?",
125
- "answer": "$59.50",
126
- "category": "mathematical",
127
- "difficulty": "medium"
128
- },
129
- {
130
- "id": "gaia_l1_017",
131
- "question": "What is the largest ocean on Earth?",
132
- "answer": "Pacific Ocean",
133
- "category": "factual",
134
- "difficulty": "medium"
135
- },
136
- {
137
- "id": "gaia_l1_018",
138
- "question": "If 3x - 7 = 14, what is the value of x?",
139
- "answer": "7",
140
- "category": "mathematical",
141
- "difficulty": "medium"
142
- },
143
- {
144
- "id": "gaia_l1_019",
145
- "question": "Who wrote 'Romeo and Juliet'?",
146
- "answer": "William Shakespeare",
147
- "category": "factual",
148
- "difficulty": "medium"
149
- },
150
- {
151
- "id": "gaia_l1_020",
152
- "question": "What is the capital of Australia?",
153
- "answer": "Canberra",
154
- "category": "factual",
155
- "difficulty": "medium"
156
- }
157
- ]
158
 
159
- class GAIATestSimulator:
160
  """
161
- Simulates the GAIA benchmark test for certification practice
162
  """
163
- def __init__(self, questions: List[Dict[str, Any]] = None):
164
  """
165
- Initialize the GAIA test simulator
166
 
167
  Args:
168
- questions: List of question dictionaries (optional)
169
- """
170
- self.questions = questions or GAIA_SAMPLE_QUESTIONS
171
- self.user_answers = {}
172
- self.current_question_idx = 0
173
- self.total_questions = len(self.questions)
174
- self.results = None
175
-
176
- def get_current_question(self) -> Dict[str, Any]:
177
  """
178
- Get the current question
179
-
180
- Returns:
181
- Current question dictionary
182
- """
183
- if self.current_question_idx < self.total_questions:
184
- return self.questions[self.current_question_idx]
185
- return None
186
-
187
- def submit_answer(self, answer: str) -> Tuple[str, int, int]:
 
 
 
188
  """
189
- Submit an answer for the current question
190
 
191
  Args:
192
- answer: User's answer
193
 
194
  Returns:
195
- Tuple of (status message, current question index, total questions)
196
  """
197
- if self.current_question_idx >= self.total_questions:
198
- return "All questions have been answered.", self.current_question_idx, self.total_questions
199
-
200
- current_question = self.questions[self.current_question_idx]
201
- self.user_answers[current_question["id"]] = answer
202
- self.current_question_idx += 1
203
-
204
- if self.current_question_idx < self.total_questions:
205
- next_question = self.questions[self.current_question_idx]
206
- return f"Answer submitted. Moving to question {self.current_question_idx + 1}/{self.total_questions}.", self.current_question_idx, self.total_questions
207
- else:
208
- return "All questions have been answered. You can now calculate your score.", self.current_question_idx, self.total_questions
209
-
210
- def calculate_score(self) -> Dict[str, Any]:
211
- """
212
- Calculate the score based on submitted answers
213
-
214
- Returns:
215
- Dictionary with score results
216
- """
217
- if not self.user_answers:
218
- return {
219
- "status": "error",
220
- "message": "No answers have been submitted yet."
221
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
 
223
- correct_answers = 0
224
- results = []
225
-
226
- for question in self.questions:
227
- question_id = question["id"]
228
- if question_id in self.user_answers:
229
- user_answer = self.user_answers[question_id].strip()
230
- correct_answer = question["answer"].strip()
231
-
232
- # Simple string matching (case-insensitive)
233
- is_correct = user_answer.lower() == correct_answer.lower()
234
-
235
- results.append({
236
- "question_id": question_id,
237
- "question": question["question"],
238
- "user_answer": user_answer,
239
- "correct_answer": correct_answer,
240
- "is_correct": is_correct
241
- })
242
-
243
- if is_correct:
244
- correct_answers += 1
245
-
246
- answered_questions = len(self.user_answers)
247
- score_percentage = (correct_answers / self.total_questions) * 100
248
- passed = score_percentage >= 30
249
-
250
- self.results = {
251
- "status": "success",
252
- "total_questions": self.total_questions,
253
- "answered_questions": answered_questions,
254
- "correct_answers": correct_answers,
255
- "score_percentage": score_percentage,
256
- "passed": passed,
257
- "results": results
258
- }
259
-
260
- return self.results
261
-
262
- def reset_test(self) -> str:
263
- """
264
- Reset the test to start over
265
-
266
- Returns:
267
- Status message
268
- """
269
- self.user_answers = {}
270
- self.current_question_idx = 0
271
- self.results = None
272
- return "Test has been reset. You can start again."
273
 
274
 
275
- def create_gaia_test_interface():
276
  """
277
- Create a Gradio interface for the GAIA test simulator
278
 
279
  Returns:
280
  Gradio interface
281
  """
282
- # Initialize the simulator
283
- simulator = GAIATestSimulator()
284
 
285
- def get_question_text():
286
- """Get the current question text"""
287
- question = simulator.get_current_question()
288
- if question:
289
- return f"Question {simulator.current_question_idx + 1}/{simulator.total_questions}: {question['question']}"
290
- return "All questions have been answered. You can now calculate your score."
291
-
292
- def submit_answer(answer):
293
- """Submit an answer and get the next question"""
294
- message, current_idx, total = simulator.submit_answer(answer)
295
-
296
- # Get the next question if available
297
- next_question = ""
298
- if current_idx < total:
299
- question = simulator.get_current_question()
300
- next_question = f"Question {current_idx + 1}/{total}: {question['question']}"
301
- else:
302
- next_question = "All questions have been answered. You can now calculate your score."
303
-
304
- return message, next_question, "" # Clear the answer input
305
-
306
- def calculate_score():
307
- """Calculate and display the score"""
308
- results = simulator.calculate_score()
309
-
310
- if results["status"] == "error":
311
- return results["message"]
312
 
313
- score_message = f"""
314
- ## GAIA Test Results
315
-
316
- - **Total Questions**: {results['total_questions']}
317
- - **Answered Questions**: {results['answered_questions']}
318
- - **Correct Answers**: {results['correct_answers']}
319
- - **Score**: {results['score_percentage']:.1f}%
320
- - **Certification Status**: {"PASSED ✅" if results['passed'] else "FAILED ❌"}
321
-
322
- {"🎉 Congratulations! You have passed the GAIA certification practice test. You can now take the official test to get your certificate." if results['passed'] else "You did not pass the certification threshold (30%). Please review your answers and try again."}
323
-
324
- ### Detailed Results:
325
-
326
- | Question | Your Answer | Correct Answer | Result |
327
- |----------|-------------|----------------|--------|
328
- """
329
 
330
- for result in results["results"]:
331
- score_message += f"| {result['question']} | {result['user_answer']} | {result['correct_answer']} | {'✓' if result['is_correct'] else '✗'} |\n"
 
332
 
333
- return score_message
334
-
335
- def reset_test():
336
- """Reset the test"""
337
- message = simulator.reset_test()
338
- question = simulator.get_current_question()
339
- question_text = f"Question 1/{simulator.total_questions}: {question['question']}"
340
- return message, question_text, ""
341
 
342
  # Create the interface
343
- with gr.Blocks(title="GAIA Certification Practice Test") as interface:
344
- gr.Markdown("# GAIA Certification Practice Test")
345
  gr.Markdown("""
346
- This application simulates the GAIA benchmark test for Hugging Face Agents Course certification.
 
 
347
 
348
- - The test consists of 20 questions from Level 1 of the GAIA benchmark
349
- - You need to score at least 30% (6 correct answers) to pass
350
- - Answer each question and submit to move to the next one
351
- - After answering all questions, calculate your score to see if you passed
 
352
 
353
- Good luck!
354
- """)
 
 
 
355
 
356
- with gr.Row():
357
- with gr.Column():
358
- question_text = gr.Textbox(
359
- label="Current Question",
360
- value=get_question_text(),
361
- lines=3,
362
- interactive=False
363
- )
364
-
365
- answer_input = gr.Textbox(
366
- label="Your Answer",
367
- placeholder="Type your answer here...",
368
- lines=2
369
- )
370
-
371
- submit_button = gr.Button("Submit Answer")
372
-
373
- status_text = gr.Textbox(
374
- label="Status",
375
- value="",
376
- lines=1,
377
- interactive=False
378
- )
379
 
380
- with gr.Row():
381
- calculate_button = gr.Button("Calculate Score")
382
- reset_button = gr.Button("Reset Test")
 
383
 
384
- score_output = gr.Markdown(label="Results")
 
 
 
 
 
 
 
 
 
 
 
 
 
385
 
386
  # Set up event handlers
387
  submit_button.click(
388
- fn=submit_answer,
389
- inputs=[answer_input],
390
- outputs=[status_text, question_text, answer_input]
391
  )
392
 
393
- calculate_button.click(
394
- fn=calculate_score,
395
- inputs=[],
396
- outputs=[score_output]
397
  )
398
-
399
- reset_button.click(
400
- fn=reset_test,
401
- inputs=[],
402
- outputs=[status_text, question_text, answer_input]
403
- )
404
-
405
- return interface
406
-
407
-
408
- def create_certification_guide_interface():
409
- """
410
- Create a Gradio interface for the certification guide
411
-
412
- Returns:
413
- Gradio interface
414
- """
415
- def get_certification_instructions():
416
- """Get the certification instructions"""
417
- return """
418
- # How to Get Your Hugging Face Agents Course Certificate
419
-
420
- After practicing with this simulator and achieving at least 30% correct answers, follow these steps to get your official certificate:
421
-
422
- ## Step 1: Complete the Required Course Units
423
- - Complete Unit 1 (Introduction to Agents)
424
- - Complete at least one use case from Unit 3
425
- - Complete the final project in Unit 4
426
-
427
- ## Step 2: Take the Official GAIA Test
428
- 1. Visit the [GAIA test page](https://huggingface.co/learn/agents-course/en/unit4/the-final-hands-on) on the Hugging Face Agents Course website
429
- 2. Sign in with your Hugging Face account
430
- 3. Follow the instructions to take the test
431
- 4. Answer the 20 questions to the best of your ability
432
- 5. Submit your answers
433
-
434
- ## Step 3: Claim Your Certificate
435
- If you scored above 30% on the official test:
436
- 1. Visit the [certificate page](https://huggingface.co/learn/agents-course/en/unit4/get-your-certificate)
437
- 2. Sign in with your Hugging Face account
438
- 3. Enter your full name (this will appear on your certificate)
439
- 4. Click "Get My Certificate" to download your certificate
440
-
441
- ## Step 4: Share Your Achievement
442
- - Add your certificate to your LinkedIn profile
443
- - Share it on social media (tag @huggingface)
444
- - Add it to your resume or portfolio
445
-
446
- Congratulations on completing the Hugging Face Agents Course!
447
- """
448
-
449
- with gr.Blocks(title="GAIA Certification Guide") as interface:
450
- gr.Markdown(get_certification_instructions())
451
-
452
- return interface
453
-
454
-
455
- def create_main_interface():
456
- """
457
- Create the main application interface with tabs
458
-
459
- Returns:
460
- Gradio interface
461
- """
462
- with gr.Blocks(title="GAIA Certification Practice") as interface:
463
- gr.Markdown("# GAIA Certification Practice for Hugging Face Agents Course")
464
-
465
- with gr.Tabs():
466
- with gr.TabItem("Practice Test"):
467
- test_interface = create_gaia_test_interface()
468
-
469
- with gr.TabItem("Certification Guide"):
470
- guide_interface = create_certification_guide_interface()
471
 
472
  return interface
473
 
474
 
475
- # Create and launch the application
476
- app = create_main_interface()
477
 
 
478
  if __name__ == "__main__":
479
- app.launch()
 
1
  """
2
+ Minimal Gradio interface for a simple AI assistant without smolagents
3
 
4
+ This is a standalone version that uses only Hugging Face Inference API directly.
5
+ It creates a simple Gradio interface for text generation.
 
6
  """
7
 
8
  import os
9
+ import sys
10
  import json
 
11
  import requests
12
+ import gradio as gr
13
 
14
+ # Check if running in Hugging Face Spaces
15
+ IS_HF_SPACES = os.environ.get("SPACE_ID") is not None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ class MinimalAIAssistant:
18
  """
19
+ Minimal AI Assistant using Hugging Face Inference API directly
20
  """
21
+ def __init__(self, api_key=None, model_id="mistralai/Mixtral-8x7B-Instruct-v0.1"):
22
  """
23
+ Initialize the minimal AI assistant
24
 
25
  Args:
26
+ api_key: Hugging Face API key
27
+ model_id: Model ID to use
 
 
 
 
 
 
 
28
  """
29
+ self.api_key = api_key or os.environ.get("HF_API_KEY", "")
30
+ self.model_id = model_id
31
+ self.api_url = f"https://api-inference.huggingface.co/models/{model_id}"
32
+ self.headers = {"Authorization": f"Bearer {self.api_key}"}
33
+
34
+ # System prompt
35
+ self.system_prompt = """
36
+ You are an advanced AI assistant designed to help with various tasks.
37
+ You can answer questions, provide information, and assist with problem-solving.
38
+ Always be helpful, accurate, and concise in your responses.
39
+ """
40
+
41
+ def query(self, prompt):
42
  """
43
+ Query the model with a prompt
44
 
45
  Args:
46
+ prompt: User prompt
47
 
48
  Returns:
49
+ Model response
50
  """
51
+ try:
52
+ # Format the prompt with system message
53
+ formatted_prompt = f"{self.system_prompt}\n\nUser: {prompt}\n\nAssistant:"
54
+
55
+ # Prepare the payload
56
+ payload = {
57
+ "inputs": formatted_prompt,
58
+ "parameters": {
59
+ "max_new_tokens": 1024,
60
+ "temperature": 0.7,
61
+ "top_p": 0.95,
62
+ "do_sample": True
63
+ }
 
 
 
 
 
 
 
 
 
 
 
64
  }
65
+
66
+ # Make the API request
67
+ response = requests.post(self.api_url, headers=self.headers, json=payload)
68
+
69
+ # Check for errors
70
+ if response.status_code != 200:
71
+ return f"Error: API returned status code {response.status_code}. {response.text}"
72
+
73
+ # Parse the response
74
+ result = response.json()
75
+
76
+ # Extract the generated text
77
+ if isinstance(result, list) and len(result) > 0:
78
+ generated_text = result[0].get("generated_text", "")
79
+ # Remove the prompt from the response
80
+ if generated_text.startswith(formatted_prompt):
81
+ generated_text = generated_text[len(formatted_prompt):].strip()
82
+ return generated_text
83
+ else:
84
+ return "Error: Unexpected response format from API"
85
 
86
+ except Exception as e:
87
+ return f"Error querying model: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
 
90
+ def create_gradio_interface():
91
  """
92
+ Create a Gradio interface for the minimal AI assistant
93
 
94
  Returns:
95
  Gradio interface
96
  """
97
+ # Initialize the assistant
98
+ assistant = MinimalAIAssistant()
99
 
100
+ def process_query(query, api_key=""):
101
+ """
102
+ Process a user query
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
+ Args:
105
+ query: User query
106
+ api_key: Hugging Face API key (optional)
107
+
108
+ Returns:
109
+ Assistant's response
110
+ """
111
+ # Update API key if provided
112
+ if api_key:
113
+ assistant.api_key = api_key
114
+ assistant.headers = {"Authorization": f"Bearer {api_key}"}
 
 
 
 
 
115
 
116
+ # Check if API key is set
117
+ if not assistant.api_key:
118
+ return "Error: No API key provided. Please enter your Hugging Face API key."
119
 
120
+ # Process the query
121
+ return assistant.query(query)
 
 
 
 
 
 
122
 
123
  # Create the interface
124
+ with gr.Blocks(title="Minimal AI Assistant") as interface:
125
+ gr.Markdown("# Minimal AI Assistant")
126
  gr.Markdown("""
127
+ This is a minimal AI assistant using the Hugging Face Inference API.
128
+ Enter your query below and the assistant will respond.
129
+ """)
130
 
131
+ api_key_input = gr.Textbox(
132
+ label="Hugging Face API Key",
133
+ placeholder="Enter your Hugging Face API key here...",
134
+ type="password"
135
+ )
136
 
137
+ query_input = gr.Textbox(
138
+ label="Your Query",
139
+ placeholder="Enter your query here...",
140
+ lines=3
141
+ )
142
 
143
+ submit_button = gr.Button("Submit")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
+ response_output = gr.Textbox(
146
+ label="Assistant Response",
147
+ lines=15
148
+ )
149
 
150
+ # Sample queries
151
+ gr.Markdown("### Sample Queries")
152
+ sample_queries = [
153
+ "What is the capital of France?",
154
+ "Explain the concept of machine learning in simple terms.",
155
+ "Write a short poem about artificial intelligence."
156
+ ]
157
+
158
+ for query in sample_queries:
159
+ sample_button = gr.Button(f"Try: {query}")
160
+ sample_button.click(
161
+ fn=lambda q=query: q,
162
+ outputs=query_input
163
+ )
164
 
165
  # Set up event handlers
166
  submit_button.click(
167
+ fn=process_query,
168
+ inputs=[query_input, api_key_input],
169
+ outputs=response_output
170
  )
171
 
172
+ # Add examples
173
+ gr.Examples(
174
+ examples=sample_queries,
175
+ inputs=query_input
176
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
  return interface
179
 
180
 
181
+ # Create and launch the interface
182
+ interface = create_gradio_interface()
183
 
184
+ # For Hugging Face Spaces
185
  if __name__ == "__main__":
186
+ interface.launch()