Spaces:
Sleeping
Sleeping
File size: 7,333 Bytes
602a16c b712b2b 602a16c 10e9b7d 602a16c 10e9b7d eccf8e4 3c4371f af57858 602a16c e80aab9 66aa5ca 602a16c 4021bf3 aa9a022 b712b2b aa9a022 602a16c 7e4a06b aa9a022 7e4a06b 7d65c66 3c4371f 7e4a06b 31243f4 602a16c e80aab9 b712b2b eccf8e4 b712b2b 31243f4 b712b2b 7d65c66 aa9a022 e80aab9 b712b2b 602a16c 7d65c66 602a16c aa9a022 602a16c b712b2b 602a16c aa9a022 602a16c 31243f4 602a16c aa9a022 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b aa9a022 31243f4 602a16c aa9a022 602a16c aa9a022 66aa5ca 31243f4 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 602a16c b712b2b 31243f4 b712b2b 602a16c b712b2b 602a16c aa9a022 e80aab9 7d65c66 e80aab9 66aa5ca 602a16c 66aa5ca 7d65c66 aa9a022 e80aab9 602a16c e80aab9 602a16c b712b2b 602a16c b712b2b 602a16c 7e4a06b 602a16c b712b2b 602a16c b712b2b 602a16c e80aab9 b712b2b aa9a022 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
"""
GAIA Agent - Main Gradio Interface
Enhanced agent with question-type specific strategies
"""
import os
import re
import json
import time
import gradio as gr
import requests
import pandas as pd
# Importar mΓ³dulos locales
from config import DEFAULT_API_URL
from agent import EnhancedAgent
from utils import detect_question_type, download_file_for_task
# ============================================================================
# FUNCIONES PRINCIPALES DE GRADIO
# ============================================================================
def run_and_submit_all(profile: gr.OAuthProfile | None):
"""Run the agent on all questions and submit the results."""
space_id = os.getenv("SPACE_ID")
if profile:
username = f"{profile.username}"
else:
return "Please Login to Hugging Face with the button.", None
api_url = DEFAULT_API_URL
questions_url = f"{api_url}/questions"
submit_url = f"{api_url}/submit"
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
# Load questions
try:
print("π₯ Loading questions from server...")
response = requests.get(questions_url, timeout=15)
questions_data = response.json()
print(f" β {len(questions_data)} questions loaded")
except Exception as e:
return f"Error fetching questions: {e}", None
# Create agent (reusable)
print("\nπ€ Creating agent...")
agent = EnhancedAgent()
results_log = []
answers_payload = []
diagnostics = []
print(f"\n{'='*80}")
print(f"π Starting processing of {len(questions_data)} questions")
print(f"{'='*80}\n")
for i, item in enumerate(questions_data):
task_id = item.get("task_id")
question_text = item.get("question", "")
file_name = item.get("file_name", "")
if not task_id:
continue
print(f"\n{'='*80}")
print(f"[{i+1}/{len(questions_data)}] Task: {task_id}")
print(f"{'='*80}")
print(f"β Question: {question_text[:150]}...")
# Detect question type
question_type = detect_question_type(question_text, file_name)
print(f"π Detected type: {question_type}")
if file_name:
print(f"π Expected file: {file_name}")
# Download file if exists
local_file = download_file_for_task(task_id)
# Show URLs found in the question
url_pattern = r"https?://[\w\-\./?&=%#]+"
found_urls = re.findall(url_pattern, question_text)
for url in found_urls:
print(f" π URL found: {url}")
# Execute agent
start_time = time.time()
print(f"βοΈ Processing with strategy '{question_type}'...")
try:
submitted_answer, execution_logs = agent.solve(
question_text,
local_file,
question_type
)
# Limpieza de seguridad
if len(submitted_answer) > 200:
submitted_answer = submitted_answer[:200]
except Exception as e:
print(f"β Error: {e}")
submitted_answer = "Error"
execution_logs = str(e)
elapsed = time.time() - start_time
print(f"\nβ
Answer: {submitted_answer}")
print(f"β±οΈ Time: {elapsed:.1f}s")
# Save results
answers_payload.append({
"task_id": task_id,
"submitted_answer": submitted_answer
})
results_log.append({
"Task ID": task_id,
"Index": i,
"Type": question_type,
"Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
"File": file_name or "N/A",
"Answer": submitted_answer,
"Time (s)": round(elapsed, 1)
})
diagnostics.append({
"index": i,
"task_id": task_id,
"question_type": question_type,
"question": question_text,
"file_name": file_name,
"answer": submitted_answer,
"elapsed_seconds": round(elapsed, 1)
})
# Clean up temporary file
if local_file and os.path.exists(local_file):
try:
os.remove(local_file)
except:
pass
# Save diagnostics
try:
ts = time.strftime("%Y%m%d_%H%M%S")
diag_path = f"diagnostics_{ts}.json"
with open(diag_path, "w", encoding="utf-8") as f:
json.dump(diagnostics, f, ensure_ascii=False, indent=2)
print(f"\nπ Diagnostics saved: {diag_path}")
except Exception as e:
print(f"β οΈ Error saving diagnostics: {e}")
# Submit results
print(f"\n{'='*80}")
print("π€ Submitting answers to server...")
submission_data = {
"username": username.strip(),
"agent_code": agent_code,
"answers": answers_payload
}
try:
response = requests.post(submit_url, json=submission_data, timeout=60)
try:
result = response.json()
score = result.get('score', 'N/A')
correct = result.get('correct_count', '?')
total = result.get('total_count', len(questions_data))
status_msg = f"""β
Submission Successful!
π Score: {score}%
β Correct: {correct}/{total}
π€ Username: {username}
"""
print(status_msg)
return status_msg, pd.DataFrame(results_log)
except Exception:
return f"Submission Failed (Server Error): {response.text}", pd.DataFrame(results_log)
except Exception as e:
return f"Submission Failed: {e}", pd.DataFrame(results_log)
# ============================================================================
# INTERFAZ GRADIO
# ============================================================================
with gr.Blocks() as demo:
gr.Markdown("""
# π€ GAIA Agent - Optimized for Files, YouTube and Logic
This agent uses question-specific strategies:
- π **Excel/CSV Files**: Reads and analyzes data with pandas
- π¬ **YouTube**: Searches for transcripts and online discussions
- πΌοΈ **Images**: Searches for information on the web
- π΅ **Audio**: Searches for transcripts online
- π **Wikipedia**: Navigates and extracts information
- π’ **Counting**: Lists items and counts programmatically
- π **Text Manipulation**: Handles reversed text, opposites, etc.
""")
gr.LoginButton()
with gr.Row():
run_button = gr.Button("π Run Evaluation & Submit All Answers", variant="primary")
status_output = gr.Textbox(label="π Status", lines=6, interactive=False)
results_table = gr.DataFrame(
label="π Detailed Results",
wrap=True,
headers=["Task ID", "Index", "Type", "Question", "File", "Answer", "Time (s)"]
)
run_button.click(
fn=run_and_submit_all,
outputs=[status_output, results_table]
)
if __name__ == "__main__":
print("π Starting GAIA Agent...")
demo.launch() |