Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
class GaiaAgent:
|
| 8 |
+
def __init__(self, model_name="mistralai/Mixtral-8x7B-Instruct-v0.1"):
|
| 9 |
+
self.client = InferenceClient(model=model_name)
|
| 10 |
+
self.system_prompt = """You are solving GAIA benchmark questions. Follow these rules:
|
| 11 |
+
- Think step by step.
|
| 12 |
+
- End with: FINAL ANSWER: [answer].
|
| 13 |
+
- Answer must be short: a number, few words, or comma-separated list.
|
| 14 |
+
- No units ($, %) unless specified.
|
| 15 |
+
- No articles (a, an, the) in strings."""
|
| 16 |
+
|
| 17 |
+
def get_questions(self, split="validation"):
|
| 18 |
+
try:
|
| 19 |
+
url = f"https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve/main/{split}/metadata.jsonl"
|
| 20 |
+
response = requests.get(url)
|
| 21 |
+
return [json.loads(line) for line in response.text.splitlines()]
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Error fetching questions: {e}")
|
| 24 |
+
return []
|
| 25 |
+
|
| 26 |
+
def answer_question(self, question):
|
| 27 |
+
prompt = f"{self.system_prompt}\nQuestion: {question['question']}\nReasoning:"
|
| 28 |
+
response = self.client.text_generation(
|
| 29 |
+
prompt,
|
| 30 |
+
max_new_tokens=500,
|
| 31 |
+
temperature=0.1,
|
| 32 |
+
do_sample=False
|
| 33 |
+
)
|
| 34 |
+
return {
|
| 35 |
+
"task_id": question["task_id"],
|
| 36 |
+
"answer": self._extract_final_answer(response),
|
| 37 |
+
"reasoning": response
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
def _extract_final_answer(self, text):
|
| 41 |
+
if "FINAL ANSWER:" in text:
|
| 42 |
+
return text.split("FINAL ANSWER:")[-1].strip()
|
| 43 |
+
return text.strip()
|
| 44 |
+
|
| 45 |
+
def run_evaluation(hf_username, full_name, question_limit=5):
|
| 46 |
+
agent = GaiaAgent()
|
| 47 |
+
questions = agent.get_questions()[:question_limit]
|
| 48 |
+
|
| 49 |
+
if not questions:
|
| 50 |
+
return "Error: No se pudieron cargar las preguntas", ""
|
| 51 |
+
|
| 52 |
+
results = []
|
| 53 |
+
for q in questions:
|
| 54 |
+
result = agent.answer_question(q)
|
| 55 |
+
results.append(f"**Pregunta {q['task_id']}:**\n{q['question']}\n\n**Respuesta:** {result['answer']}\n\n---")
|
| 56 |
+
|
| 57 |
+
# Simular generación de certificado
|
| 58 |
+
certificate_info = f"""
|
| 59 |
+
⭐ Certificado GAIA Benchmark ⭐
|
| 60 |
+
|
| 61 |
+
Nombre: {full_name}
|
| 62 |
+
Usuario HF: {hf_username}
|
| 63 |
+
Fecha: {datetime.now().strftime('%Y-%m-%d')}
|
| 64 |
+
Preguntas respondidas: {len(results)}
|
| 65 |
+
|
| 66 |
+
¡Felicitaciones por completar el benchmark!
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
return certificate_info, "\n".join(results)
|
| 70 |
+
|
| 71 |
+
# Interfaz Gradio
|
| 72 |
+
with gr.Blocks(title="Agente GAIA Benchmark") as demo:
|
| 73 |
+
gr.Markdown("""
|
| 74 |
+
# 🌟 Agente para GAIA Benchmark
|
| 75 |
+
Ejecuta evaluación y obtén tu certificado
|
| 76 |
+
""")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
with gr.Column():
|
| 80 |
+
hf_username = gr.Textbox(label="Usuario de Hugging Face", visible=False)
|
| 81 |
+
full_name = gr.Textbox(label="Nombre completo (aparecerá en el certificado)")
|
| 82 |
+
question_slider = gr.Slider(1, 20, value=5, label="Número de preguntas a evaluar")
|
| 83 |
+
submit_btn = gr.Button("Ejecutar Evaluación", variant="primary")
|
| 84 |
+
|
| 85 |
+
with gr.Column():
|
| 86 |
+
certificate_output = gr.Textbox(label="Certificado", interactive=False)
|
| 87 |
+
answers_output = gr.Markdown(label="Respuestas Generadas")
|
| 88 |
+
|
| 89 |
+
# Integración con login de HF
|
| 90 |
+
demo.load(
|
| 91 |
+
lambda: gr.Textbox(value=gr.Request().username if gr.Request() else ""),
|
| 92 |
+
outputs=hf_username
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
submit_btn.click(
|
| 96 |
+
run_evaluation,
|
| 97 |
+
inputs=[hf_username, full_name, question_slider],
|
| 98 |
+
outputs=[certificate_output, answers_output]
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|