Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,94 +4,116 @@ import requests
|
|
4 |
import pandas as pd
|
5 |
from huggingface_hub import login
|
6 |
from dotenv import load_dotenv
|
7 |
-
from agent import ejecutar_agente
|
8 |
|
9 |
-
# Cargar
|
10 |
-
|
11 |
-
if hf_token:
|
12 |
-
login(token=hf_token)
|
13 |
-
else:
|
14 |
-
raise ValueError("No se encontró el token de Hugging Face. Verifica el secreto HF_TOKEN.")
|
15 |
-
|
16 |
-
# Constantes
|
17 |
API_BASE_URL = "https://my-custom-api.hf.space"
|
|
|
18 |
|
19 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
space_id = os.getenv("MY_SPACE_ID")
|
21 |
|
22 |
if profile:
|
23 |
-
username = profile.username
|
24 |
-
print(f"Usuario: {username}")
|
25 |
else:
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
# Obtener preguntas
|
29 |
try:
|
30 |
-
response = requests.get(
|
31 |
response.raise_for_status()
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
except Exception as e:
|
34 |
return f"Error al obtener preguntas: {e}", None
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
for p in preguntas:
|
40 |
-
task_id = p.get("task_id")
|
41 |
-
if task_id:
|
42 |
-
try:
|
43 |
-
archivo = requests.get(f"{API_BASE_URL}/files/{task_id}", timeout=10)
|
44 |
-
archivo.raise_for_status()
|
45 |
-
p["attachment_b64"] = archivo.text
|
46 |
-
except:
|
47 |
-
p["attachment_b64"] = None
|
48 |
|
49 |
-
|
50 |
-
resultados = []
|
51 |
-
respuestas = []
|
52 |
-
|
53 |
-
for item in preguntas:
|
54 |
task_id = item.get("task_id")
|
55 |
-
|
56 |
-
|
57 |
-
if
|
58 |
-
|
|
|
|
|
59 |
|
60 |
try:
|
61 |
-
|
|
|
|
|
62 |
except Exception as e:
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
"username": username,
|
71 |
"agent_code": f"https://huggingface.co/spaces/{space_id}",
|
72 |
-
"answers":
|
73 |
}
|
74 |
|
75 |
try:
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
80 |
except Exception as e:
|
81 |
-
return f"Error al enviar: {e}", pd.DataFrame(
|
82 |
|
83 |
# Interfaz Gradio
|
84 |
-
with gr.Blocks() as
|
85 |
-
gr.Markdown("# Evaluador de Agente")
|
86 |
-
gr.Markdown("Inicia sesión, ejecuta el agente y envía tus respuestas.")
|
87 |
-
|
88 |
gr.LoginButton()
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
salida_tabla = gr.DataFrame(label="Resultados")
|
93 |
|
94 |
-
|
|
|
|
|
|
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
-
|
|
|
4 |
import pandas as pd
|
5 |
from huggingface_hub import login
|
6 |
from dotenv import load_dotenv
|
|
|
7 |
|
8 |
+
# Cargar variables de entorno
|
9 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
API_BASE_URL = "https://my-custom-api.hf.space"
|
11 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Meta-Llama-3-8B-Instruct") # o usa 'google/gemma-2b-it'
|
12 |
|
13 |
+
def call_model(prompt):
|
14 |
+
headers = {
|
15 |
+
"Authorization": f"Bearer {os.environ['HF_TOKEN']}",
|
16 |
+
"Content-Type": "application/json"
|
17 |
+
}
|
18 |
+
payload = {
|
19 |
+
"inputs": prompt,
|
20 |
+
"parameters": {"max_new_tokens": 512}
|
21 |
+
}
|
22 |
+
response = requests.post(
|
23 |
+
f"https://api-inference.huggingface.co/models/{MODEL_NAME}",
|
24 |
+
headers=headers,
|
25 |
+
json=payload,
|
26 |
+
timeout=60
|
27 |
+
)
|
28 |
+
result = response.json()
|
29 |
+
if isinstance(result, dict) and result.get("error"):
|
30 |
+
return f"ERROR: {result['error']}"
|
31 |
+
return result[0]["generated_text"] if isinstance(result, list) else result
|
32 |
+
|
33 |
+
def execute_agent_operations(profile: gr.OAuthProfile | None):
|
34 |
space_id = os.getenv("MY_SPACE_ID")
|
35 |
|
36 |
if profile:
|
37 |
+
username = f"{profile.username}"
|
38 |
+
print(f"Usuario conectado: {username}")
|
39 |
else:
|
40 |
+
print("No has iniciado sesión.")
|
41 |
+
return "Inicia sesión en Hugging Face.", None
|
42 |
+
|
43 |
+
questions_url = f"{API_BASE_URL}/questions"
|
44 |
+
attachments_url = f"{API_BASE_URL}/files/"
|
45 |
+
submit_url = f"{API_BASE_URL}/submit"
|
46 |
|
|
|
47 |
try:
|
48 |
+
response = requests.get(questions_url, timeout=15)
|
49 |
response.raise_for_status()
|
50 |
+
questions = response.json()
|
51 |
+
if not questions:
|
52 |
+
return "No se encontraron preguntas.", None
|
53 |
+
|
54 |
+
for q in questions:
|
55 |
+
task_id = q.get("task_id")
|
56 |
+
file_name = q.get("file_name", "")
|
57 |
+
if task_id and file_name:
|
58 |
+
try:
|
59 |
+
att_response = requests.get(f"{attachments_url}{task_id}", timeout=15)
|
60 |
+
att_response.raise_for_status()
|
61 |
+
q["attachment_b64"] = att_response.text
|
62 |
+
except Exception as e:
|
63 |
+
print(f"Error al obtener adjunto: {e}")
|
64 |
+
q["attachment_b64"] = None
|
65 |
except Exception as e:
|
66 |
return f"Error al obtener preguntas: {e}", None
|
67 |
|
68 |
+
results_log = []
|
69 |
+
answers_payload = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
for item in questions:
|
|
|
|
|
|
|
|
|
72 |
task_id = item.get("task_id")
|
73 |
+
question_text = item.get("question", "")
|
74 |
+
attachment = item.get("attachment_b64", "")
|
75 |
+
full_prompt = f"{question_text}\n\nArchivo adjunto:\n{attachment}" if attachment else question_text
|
76 |
+
|
77 |
+
if not task_id or not full_prompt:
|
78 |
+
continue
|
79 |
|
80 |
try:
|
81 |
+
submitted_answer = call_model(full_prompt)
|
82 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
83 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
84 |
except Exception as e:
|
85 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
86 |
|
87 |
+
if not answers_payload:
|
88 |
+
return "El agente no produjo respuestas.", pd.DataFrame(results_log)
|
89 |
|
90 |
+
submission_data = {
|
91 |
+
"username": username.strip(),
|
|
|
92 |
"agent_code": f"https://huggingface.co/spaces/{space_id}",
|
93 |
+
"answers": answers_payload
|
94 |
}
|
95 |
|
96 |
try:
|
97 |
+
post_response = requests.post(submit_url, json=submission_data, timeout=60)
|
98 |
+
post_response.raise_for_status()
|
99 |
+
result_data = post_response.json()
|
100 |
+
score = result_data.get("score", "N/A")
|
101 |
+
return f"¡Envío exitoso!\nPuntuación: {score}", pd.DataFrame(results_log)
|
102 |
except Exception as e:
|
103 |
+
return f"Error al enviar: {e}", pd.DataFrame(results_log)
|
104 |
|
105 |
# Interfaz Gradio
|
106 |
+
with gr.Blocks() as demo:
|
107 |
+
gr.Markdown("# Evaluador de Agente (versión personalizada)")
|
|
|
|
|
108 |
gr.LoginButton()
|
109 |
+
run_button = gr.Button("Ejecutar Evaluación y Enviar Respuestas")
|
110 |
+
status_output = gr.Textbox(label="Estado", lines=3)
|
111 |
+
results_table = gr.DataFrame(label="Respuestas del agente")
|
|
|
112 |
|
113 |
+
run_button.click(
|
114 |
+
fn=execute_agent_operations,
|
115 |
+
outputs=[status_output, results_table]
|
116 |
+
)
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
+
demo.launch(debug=True)
|