Update app.py
Browse files
app.py
CHANGED
|
@@ -1,186 +1,479 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
|
| 4 |
-
This
|
| 5 |
-
It
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
| 9 |
-
import sys
|
| 10 |
import json
|
| 11 |
-
import requests
|
| 12 |
import gradio as gr
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
class
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
"""
|
| 21 |
-
def __init__(self,
|
| 22 |
"""
|
| 23 |
-
Initialize the
|
| 24 |
|
| 25 |
Args:
|
| 26 |
-
|
| 27 |
-
model_id: Model ID to use
|
| 28 |
"""
|
| 29 |
-
self.
|
| 30 |
-
self.
|
| 31 |
-
self.
|
| 32 |
-
self.
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
Args:
|
| 46 |
-
|
| 47 |
|
| 48 |
Returns:
|
| 49 |
-
|
| 50 |
"""
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 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 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
-
def
|
| 91 |
"""
|
| 92 |
-
Create a Gradio interface for the
|
| 93 |
|
| 94 |
Returns:
|
| 95 |
Gradio interface
|
| 96 |
"""
|
| 97 |
-
# Initialize the
|
| 98 |
-
|
| 99 |
|
| 100 |
-
def
|
| 101 |
-
"""
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
return "Error: No API key provided. Please enter your Hugging Face API key."
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# Create the interface
|
| 124 |
-
with gr.Blocks(title="
|
| 125 |
-
gr.Markdown("#
|
| 126 |
gr.Markdown("""
|
| 127 |
-
This
|
| 128 |
-
Enter your query below and the assistant will respond.
|
| 129 |
-
""")
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
)
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
placeholder="Enter your query here...",
|
| 140 |
-
lines=3
|
| 141 |
-
)
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
)
|
| 149 |
|
| 150 |
-
|
| 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=
|
| 168 |
-
inputs=[
|
| 169 |
-
outputs=
|
| 170 |
)
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
return interface
|
| 179 |
|
| 180 |
|
| 181 |
-
# Create and launch the
|
| 182 |
-
|
| 183 |
|
| 184 |
-
# For Hugging Face Spaces
|
| 185 |
if __name__ == "__main__":
|
| 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()
|