Update app.py
Browse files
app.py
CHANGED
@@ -1,186 +1,384 @@
|
|
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 |
-
#
|
15 |
-
|
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 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
Args:
|
46 |
-
prompt: User prompt
|
47 |
-
|
48 |
-
Returns:
|
49 |
-
Model response
|
50 |
-
"""
|
51 |
try:
|
52 |
-
#
|
53 |
-
|
|
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
#
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
|
74 |
-
|
|
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
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 |
-
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 |
-
|
95 |
-
|
96 |
-
|
97 |
-
# Initialize the assistant
|
98 |
-
assistant = MinimalAIAssistant()
|
99 |
-
|
100 |
-
def process_query(query, api_key=""):
|
101 |
-
"""
|
102 |
-
Process a user query
|
103 |
|
104 |
-
|
105 |
-
|
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 |
-
|
138 |
-
|
139 |
-
placeholder="Enter your query here...",
|
140 |
-
lines=3
|
141 |
-
)
|
142 |
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
149 |
|
150 |
-
#
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
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 |
-
#
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
|
|
|
|
|
|
180 |
|
181 |
-
#
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
-
# For Hugging Face Spaces
|
185 |
if __name__ == "__main__":
|
186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import inspect
|
5 |
+
import pandas as pd
|
6 |
+
import json
|
7 |
+
import re
|
8 |
+
from typing import List, Dict, Any, Optional
|
9 |
|
10 |
+
# (Keep Constants as is)
|
11 |
+
# --- Constants ---
|
12 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# --- Enhanced GAIA Agent Definition ---
|
15 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
16 |
+
class EnhancedGAIAAgent:
|
17 |
+
def __init__(self):
|
18 |
+
print("EnhancedGAIAAgent initialized.")
|
19 |
+
self.tools = {
|
20 |
+
"web_search": self._web_search,
|
21 |
+
"calculator": self._calculator,
|
22 |
+
"image_analysis": self._image_analysis,
|
23 |
+
"text_analysis": self._text_analysis,
|
24 |
+
"code_execution": self._code_execution
|
25 |
+
}
|
26 |
+
|
27 |
+
# Tracking for reasoning steps
|
28 |
+
self.reasoning_steps = []
|
29 |
+
self.max_reasoning_steps = 5
|
30 |
+
|
31 |
+
def _web_search(self, query: str) -> str:
|
32 |
+
"""Simulates web search functionality"""
|
33 |
+
print(f"Performing web search for: {query}")
|
34 |
+
|
35 |
+
# Simulate search results based on query keywords
|
36 |
+
if "population" in query.lower():
|
37 |
+
return "The population of the queried location is approximately X million people as of 2023."
|
38 |
+
elif "capital" in query.lower():
|
39 |
+
return "The capital city of the queried location is X, with a population of Y million."
|
40 |
+
elif "president" in query.lower() or "prime minister" in query.lower() or "leader" in query.lower():
|
41 |
+
return "The current leader of the queried location is X, who has been in office since Y."
|
42 |
+
elif "tallest" in query.lower() or "highest" in query.lower():
|
43 |
+
return "The tallest structure in the queried location is X, with a height of Y meters."
|
44 |
+
elif "founded" in query.lower() or "established" in query.lower() or "history" in query.lower():
|
45 |
+
return "The queried entity was established/founded in X year. Its history includes Y and Z significant events."
|
46 |
+
elif "weather" in query.lower() or "temperature" in query.lower() or "climate" in query.lower():
|
47 |
+
return "The current weather/climate in the queried location is X with temperatures ranging from Y to Z degrees."
|
48 |
+
else:
|
49 |
+
return f"Search results for '{query}' include various websites and information sources that may contain relevant information."
|
50 |
+
|
51 |
+
def _calculator(self, expression: str) -> str:
|
52 |
+
"""Performs mathematical calculations"""
|
53 |
+
print(f"Calculating: {expression}")
|
54 |
+
|
55 |
+
# Clean the expression
|
56 |
+
cleaned_expr = expression.replace('×', '*').replace('÷', '/')
|
57 |
+
cleaned_expr = re.sub(r'[^0-9+\-*/().^ ]', '', cleaned_expr)
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
try:
|
60 |
+
# Handle exponentiation separately
|
61 |
+
if '^' in cleaned_expr:
|
62 |
+
cleaned_expr = cleaned_expr.replace('^', '**')
|
63 |
|
64 |
+
# Safely evaluate the expression
|
65 |
+
result = eval(cleaned_expr)
|
66 |
+
return f"The result of {expression} is {result}"
|
67 |
+
except Exception as e:
|
68 |
+
return f"Error calculating {expression}: {str(e)}"
|
69 |
+
|
70 |
+
def _image_analysis(self, image_description: str) -> str:
|
71 |
+
"""Simulates image analysis functionality"""
|
72 |
+
print(f"Analyzing image: {image_description}")
|
73 |
+
|
74 |
+
# Simulate image analysis based on description keywords
|
75 |
+
if "person" in image_description.lower() or "people" in image_description.lower() or "human" in image_description.lower():
|
76 |
+
return "The image contains one or more people. They appear to be [activity/pose/expression]."
|
77 |
+
elif "animal" in image_description.lower() or "dog" in image_description.lower() or "cat" in image_description.lower():
|
78 |
+
return "The image shows an animal, likely a [specific animal]. It appears to be [activity/state]."
|
79 |
+
elif "building" in image_description.lower() or "architecture" in image_description.lower():
|
80 |
+
return "The image depicts a building or architectural structure. It appears to be [style/type] architecture."
|
81 |
+
elif "landscape" in image_description.lower() or "nature" in image_description.lower():
|
82 |
+
return "The image shows a natural landscape featuring [elements like mountains, rivers, forests, etc.]."
|
83 |
+
elif "chart" in image_description.lower() or "graph" in image_description.lower() or "diagram" in image_description.lower():
|
84 |
+
return "The image contains a chart/graph showing data about [topic]. The trend appears to be [increasing/decreasing/stable]."
|
85 |
+
else:
|
86 |
+
return f"The image appears to show {image_description}. Key elements include [objects/subjects] and [notable features]."
|
87 |
+
|
88 |
+
def _text_analysis(self, text: str) -> str:
|
89 |
+
"""Analyzes text for sentiment, entities, and key information"""
|
90 |
+
print(f"Analyzing text (first 50 chars): {text[:50]}...")
|
91 |
+
|
92 |
+
# Count words and sentences
|
93 |
+
word_count = len(text.split())
|
94 |
+
sentence_count = len(re.split(r'[.!?]+', text))
|
95 |
+
|
96 |
+
# Simple sentiment analysis
|
97 |
+
positive_words = ['good', 'great', 'excellent', 'positive', 'happy', 'best', 'love', 'wonderful', 'fantastic']
|
98 |
+
negative_words = ['bad', 'poor', 'negative', 'terrible', 'worst', 'hate', 'awful', 'horrible', 'disappointing']
|
99 |
+
|
100 |
+
positive_count = sum(1 for word in text.lower().split() if word in positive_words)
|
101 |
+
negative_count = sum(1 for word in text.lower().split() if word in negative_words)
|
102 |
+
|
103 |
+
if positive_count > negative_count:
|
104 |
+
sentiment = "positive"
|
105 |
+
elif negative_count > positive_count:
|
106 |
+
sentiment = "negative"
|
107 |
+
else:
|
108 |
+
sentiment = "neutral"
|
109 |
+
|
110 |
+
return f"Text analysis: {word_count} words, {sentence_count} sentences. The sentiment appears to be {sentiment}."
|
111 |
+
|
112 |
+
def _code_execution(self, code: str) -> str:
|
113 |
+
"""Simulates code execution and analysis"""
|
114 |
+
print(f"Analyzing code (first 50 chars): {code[:50]}...")
|
115 |
+
|
116 |
+
# Identify language
|
117 |
+
language = "unknown"
|
118 |
+
if "def " in code or "import " in code or "print(" in code:
|
119 |
+
language = "Python"
|
120 |
+
elif "function " in code or "var " in code or "const " in code or "let " in code:
|
121 |
+
language = "JavaScript"
|
122 |
+
elif "public class " in code or "System.out.println" in code:
|
123 |
+
language = "Java"
|
124 |
+
elif "#include" in code or "int main" in code:
|
125 |
+
language = "C/C++"
|
126 |
+
|
127 |
+
# Simple code analysis
|
128 |
+
lines = code.count('\n') + 1
|
129 |
+
|
130 |
+
return f"Code analysis: {lines} lines of {language} code. The code appears to [purpose/functionality]."
|
131 |
+
|
132 |
+
def _reason(self, question: str) -> List[str]:
|
133 |
+
"""Performs step-by-step reasoning about the question"""
|
134 |
+
reasoning = []
|
135 |
+
|
136 |
+
# Initial analysis
|
137 |
+
reasoning.append(f"Question: '{question}'")
|
138 |
+
reasoning.append("Let me analyze what this question is asking for.")
|
139 |
+
|
140 |
+
# Identify question type
|
141 |
+
if any(keyword in question.lower() for keyword in ["calculate", "compute", "sum", "difference", "product", "divide"]):
|
142 |
+
reasoning.append("This appears to be a calculation question.")
|
143 |
|
144 |
+
# Extract mathematical expression
|
145 |
+
expression = re.search(r'calculate\s+(.+?)(?:\?|$)', question.lower())
|
146 |
+
if expression:
|
147 |
+
reasoning.append(f"I need to calculate: {expression.group(1)}")
|
148 |
+
reasoning.append(f"Using the calculator tool to compute this.")
|
149 |
+
else:
|
150 |
+
reasoning.append("I need to identify the mathematical operation required.")
|
151 |
+
|
152 |
+
elif any(keyword in question.lower() for keyword in ["image", "picture", "photo", "graph", "chart"]):
|
153 |
+
reasoning.append("This question involves analyzing an image or visual content.")
|
154 |
+
reasoning.append("I should use image analysis to identify key elements in the image.")
|
155 |
|
156 |
+
elif any(keyword in question.lower() for keyword in ["population", "capital", "country", "city", "president", "leader"]):
|
157 |
+
reasoning.append("This is a factual question about geography, demographics, or leadership.")
|
158 |
+
reasoning.append("I should search for the most up-to-date information.")
|
159 |
|
160 |
+
elif any(keyword in question.lower() for keyword in ["code", "function", "program", "algorithm"]):
|
161 |
+
reasoning.append("This question involves code analysis or programming.")
|
162 |
+
reasoning.append("I should examine the code structure and functionality.")
|
163 |
|
164 |
+
else:
|
165 |
+
reasoning.append("This appears to be a general knowledge question.")
|
166 |
+
reasoning.append("I'll need to search for relevant information and synthesize an answer.")
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
+
return reasoning
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
+
def __call__(self, question: str) -> str:
|
171 |
+
"""Main method to process questions and generate answers"""
|
172 |
+
print(f"Agent received question: {question}")
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
+
# Step 1: Reasoning
|
175 |
+
self.reasoning_steps = self._reason(question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
# Step 2: Determine approach and tools to use
|
178 |
+
answer = ""
|
|
|
|
|
|
|
179 |
|
180 |
+
# Handle calculation questions
|
181 |
+
if any(keyword in question.lower() for keyword in ["calculate", "compute", "sum", "difference", "product", "divide"]):
|
182 |
+
# Extract mathematical expression
|
183 |
+
expression_match = re.search(r'calculate\s+(.+?)(?:\?|$)', question.lower())
|
184 |
+
if expression_match:
|
185 |
+
expression = expression_match.group(1).strip()
|
186 |
+
answer = self._calculator(expression)
|
187 |
+
else:
|
188 |
+
# Try to extract numbers and operations
|
189 |
+
numbers = re.findall(r'\d+', question)
|
190 |
+
if len(numbers) >= 2:
|
191 |
+
if "sum" in question.lower() or "add" in question.lower() or "plus" in question.lower():
|
192 |
+
result = sum(int(num) for num in numbers)
|
193 |
+
answer = f"The sum of the numbers is {result}"
|
194 |
+
elif "difference" in question.lower() or "subtract" in question.lower() or "minus" in question.lower():
|
195 |
+
result = int(numbers[0]) - int(numbers[1])
|
196 |
+
answer = f"The difference between {numbers[0]} and {numbers[1]} is {result}"
|
197 |
+
elif "product" in question.lower() or "multiply" in question.lower():
|
198 |
+
result = int(numbers[0]) * int(numbers[1])
|
199 |
+
answer = f"The product of {numbers[0]} and {numbers[1]} is {result}"
|
200 |
+
elif "divide" in question.lower():
|
201 |
+
if int(numbers[1]) != 0:
|
202 |
+
result = int(numbers[0]) / int(numbers[1])
|
203 |
+
answer = f"The result of dividing {numbers[0]} by {numbers[1]} is {result}"
|
204 |
+
else:
|
205 |
+
answer = "Cannot divide by zero"
|
206 |
+
else:
|
207 |
+
answer = "I couldn't identify a clear calculation to perform."
|
208 |
|
209 |
+
# Handle image analysis questions
|
210 |
+
elif any(keyword in question.lower() for keyword in ["image", "picture", "photo", "graph", "chart"]):
|
211 |
+
# Extract image description if available
|
212 |
+
image_desc = question
|
213 |
+
answer = self._image_analysis(image_desc)
|
214 |
|
215 |
+
# Handle factual questions
|
216 |
+
elif any(keyword in question.lower() for keyword in ["who", "what", "where", "when", "why", "how"]):
|
217 |
+
search_query = question.replace("?", "")
|
218 |
+
search_results = self._web_search(search_query)
|
219 |
+
|
220 |
+
# Process and synthesize search results
|
221 |
+
answer = f"Based on available information: {search_results}"
|
222 |
+
|
223 |
+
# Add specific details for common question types
|
224 |
+
if "who" in question.lower():
|
225 |
+
answer += " The individual mentioned is known for their contributions to the field."
|
226 |
+
elif "when" in question.lower():
|
227 |
+
answer += " This occurred during a significant period in history."
|
228 |
+
elif "where" in question.lower():
|
229 |
+
answer += " The location is notable for its geographical and cultural significance."
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
|
231 |
+
# Handle code questions
|
232 |
+
elif any(keyword in question.lower() for keyword in ["code", "function", "program", "algorithm"]):
|
233 |
+
# Extract code if present or use the question itself
|
234 |
+
code_sample = question
|
235 |
+
answer = self._code_execution(code_sample)
|
236 |
+
|
237 |
+
# General knowledge questions
|
238 |
+
else:
|
239 |
+
# Combine web search and text analysis
|
240 |
+
search_results = self._web_search(question)
|
241 |
+
text_analysis = self._text_analysis(question)
|
242 |
+
|
243 |
+
answer = f"To answer your question: {search_results}"
|
244 |
+
|
245 |
+
# Add reasoning steps if available
|
246 |
+
if self.reasoning_steps:
|
247 |
+
reasoning_summary = " ".join(self.reasoning_steps[-2:]) # Use last two reasoning steps
|
248 |
+
answer = f"{answer}\n\nReasoning: {reasoning_summary}"
|
249 |
+
|
250 |
+
return answer
|
251 |
+
|
252 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
253 |
+
"""
|
254 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers, and displays the results.
|
255 |
+
"""
|
256 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
257 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
258 |
+
if profile:
|
259 |
+
username= f"{profile.username}"
|
260 |
+
print(f"User logged in: {username}")
|
261 |
+
else:
|
262 |
+
print("User not logged in.")
|
263 |
+
return "Please Login to Hugging Face with the button.", None
|
264 |
|
265 |
+
api_url = DEFAULT_API_URL
|
266 |
+
questions_url = f"{api_url}/questions"
|
267 |
+
submit_url = f"{api_url}/submit"
|
268 |
|
269 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
270 |
+
try:
|
271 |
+
agent = EnhancedGAIAAgent()
|
272 |
+
except Exception as e:
|
273 |
+
print(f"Error instantiating agent: {e}")
|
274 |
+
return f"Error initializing agent: {e}", None
|
275 |
+
|
276 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
277 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
278 |
+
print(agent_code)
|
279 |
+
|
280 |
+
# 2. Fetch Questions
|
281 |
+
print(f"Fetching questions from: {questions_url}")
|
282 |
+
try:
|
283 |
+
response = requests.get(questions_url, timeout=15)
|
284 |
+
response.raise_for_status()
|
285 |
+
questions_data = response.json()
|
286 |
+
if not questions_data:
|
287 |
+
print("Fetched questions list is empty.")
|
288 |
+
return "Fetched questions list is empty or invalid format.", None
|
289 |
+
print(f"Fetched {len(questions_data)} questions.")
|
290 |
+
except requests.exceptions.RequestException as e:
|
291 |
+
print(f"Error fetching questions: {e}")
|
292 |
+
return f"Error fetching questions: {e}", None
|
293 |
+
except requests.exceptions.JSONDecodeError as e:
|
294 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
295 |
+
print(f"Response text: {response.text[:500]}")
|
296 |
+
return f"Error decoding server response for questions: {e}", None
|
297 |
+
except Exception as e:
|
298 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
299 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
300 |
+
|
301 |
+
# 3. Run your Agent
|
302 |
+
results_log = []
|
303 |
+
answers_payload = []
|
304 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
305 |
+
for item in questions_data:
|
306 |
+
task_id = item.get("task_id")
|
307 |
+
question_text = item.get("question")
|
308 |
+
if not task_id or question_text is None:
|
309 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
310 |
+
continue
|
311 |
+
|
312 |
+
try:
|
313 |
+
submitted_answer = agent(question_text)
|
314 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
315 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
316 |
+
except Exception as e:
|
317 |
+
print(f"Error running agent on task {task_id}: {e}")
|
318 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
319 |
+
|
320 |
+
if not answers_payload:
|
321 |
+
print("Agent did not produce any answers to submit.")
|
322 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
323 |
+
|
324 |
+
# 4. Prepare Submission
|
325 |
+
submission_data = {
|
326 |
+
"username": username.strip(),
|
327 |
+
"agent_code": agent_code,
|
328 |
+
"answers": answers_payload
|
329 |
+
}
|
330 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
331 |
+
print(status_update)
|
332 |
+
|
333 |
+
# 5. Submit
|
334 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
335 |
+
try:
|
336 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
337 |
+
response.raise_for_status()
|
338 |
+
result_data = response.json()
|
339 |
+
final_status = (
|
340 |
+
f"Submission Successful!\n"
|
341 |
+
f"User: {result_data.get('username')}\n"
|
342 |
+
f"Overall Score: {result_data.get('overall_score', 'N/A')}\n"
|
343 |
+
f"Correct Answers: {result_data.get('correct_answers', 'N/A')}\n"
|
344 |
+
f"Total Questions: {result_data.get('total_questions', 'N/A')}\n"
|
345 |
+
)
|
346 |
+
print(final_status)
|
347 |
+
return final_status, pd.DataFrame(results_log)
|
348 |
+
except requests.exceptions.RequestException as e:
|
349 |
+
error_msg = f"Error submitting answers: {e}"
|
350 |
+
print(error_msg)
|
351 |
+
return error_msg, pd.DataFrame(results_log)
|
352 |
+
except Exception as e:
|
353 |
+
error_msg = f"An unexpected error occurred during submission: {e}"
|
354 |
+
print(error_msg)
|
355 |
+
return error_msg, pd.DataFrame(results_log)
|
356 |
+
|
357 |
+
# --- Gradio Interface ---
|
358 |
+
with gr.Blocks() as demo:
|
359 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
360 |
+
|
361 |
+
gr.Markdown("Instructions:")
|
362 |
+
gr.Markdown("1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...")
|
363 |
+
gr.Markdown("2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.")
|
364 |
+
gr.Markdown("3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.")
|
365 |
+
|
366 |
+
gr.Markdown("---")
|
367 |
+
|
368 |
+
gr.Markdown("Disclaimers: Once clicking on the \"submit button, it can take quite some time ( this is the time for the agent to go through all the questions). This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.")
|
369 |
+
|
370 |
+
with gr.Row():
|
371 |
+
login_button = gr.LoginButton(value="Sign in with Hugging Face")
|
372 |
+
|
373 |
+
with gr.Row():
|
374 |
+
submit_button = gr.Button("Run Evaluation & Submit All Answers")
|
375 |
+
|
376 |
+
with gr.Row():
|
377 |
+
with gr.Column():
|
378 |
+
output_status = gr.Textbox(label="Run Status / Submission Result")
|
379 |
+
output_results = gr.Dataframe(label="Questions and Agent Answers")
|
380 |
+
|
381 |
+
submit_button.click(run_and_submit_all, inputs=[login_button], outputs=[output_status, output_results])
|
382 |
|
|
|
383 |
if __name__ == "__main__":
|
384 |
+
demo.launch()
|