Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,63 @@
|
|
1 |
# app.py
|
2 |
|
3 |
import os
|
4 |
-
import asyncio
|
5 |
-
import pandas as pd
|
6 |
import requests
|
|
|
7 |
import gradio as gr
|
8 |
from agent import answer_question
|
|
|
9 |
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
class GAIALlamaAgent:
|
13 |
def __init__(self):
|
14 |
-
print("
|
15 |
|
16 |
def __call__(self, question: str) -> str:
|
17 |
-
print(f"
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
21 |
space_id = os.getenv("SPACE_ID")
|
22 |
username = profile.username if profile else None
|
23 |
if not username:
|
24 |
-
return "Please
|
25 |
|
26 |
-
|
27 |
api_url = DEFAULT_API_URL
|
|
|
|
|
|
|
28 |
|
29 |
try:
|
30 |
-
response = requests.get(
|
31 |
response.raise_for_status()
|
32 |
questions_data = response.json()
|
|
|
33 |
except Exception as e:
|
34 |
-
return f"Error fetching questions: {e}", None
|
35 |
|
|
|
36 |
answers_payload = []
|
37 |
results_log = []
|
38 |
-
agent = GAIALlamaAgent()
|
39 |
|
40 |
for item in questions_data:
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
try:
|
44 |
-
|
45 |
except Exception as e:
|
46 |
-
|
47 |
-
answers_payload.append({"task_id":
|
48 |
-
results_log.append({"Task ID":
|
|
|
|
|
|
|
49 |
|
50 |
submission_data = {
|
51 |
"username": username,
|
@@ -54,7 +66,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
54 |
}
|
55 |
|
56 |
try:
|
57 |
-
response = requests.post(
|
58 |
response.raise_for_status()
|
59 |
result_data = response.json()
|
60 |
status = (
|
@@ -66,14 +78,28 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
66 |
)
|
67 |
return status, pd.DataFrame(results_log)
|
68 |
except Exception as e:
|
69 |
-
return f"Submission failed: {e}", pd.DataFrame(results_log)
|
70 |
|
|
|
71 |
with gr.Blocks() as demo:
|
72 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
gr.LoginButton()
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
1 |
# app.py
|
2 |
|
3 |
import os
|
|
|
|
|
4 |
import requests
|
5 |
+
import pandas as pd
|
6 |
import gradio as gr
|
7 |
from agent import answer_question
|
8 |
+
import asyncio
|
9 |
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
class GAIALlamaAgent:
|
13 |
def __init__(self):
|
14 |
+
print("β
LangChain/LlamaIndex Agent initialized.")
|
15 |
|
16 |
def __call__(self, question: str) -> str:
|
17 |
+
print(f"π¨ Agent received: {question[:50]}...")
|
18 |
+
try:
|
19 |
+
return asyncio.run(answer_question(question))
|
20 |
+
except Exception as e:
|
21 |
+
return f"[ERROR] {str(e)}"
|
22 |
|
23 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
24 |
space_id = os.getenv("SPACE_ID")
|
25 |
username = profile.username if profile else None
|
26 |
if not username:
|
27 |
+
return "Please log in to Hugging Face.", None
|
28 |
|
29 |
+
print(f"π€ User: {username}")
|
30 |
api_url = DEFAULT_API_URL
|
31 |
+
questions_url = f"{api_url}/questions"
|
32 |
+
submit_url = f"{api_url}/submit"
|
33 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
34 |
|
35 |
try:
|
36 |
+
response = requests.get(questions_url, timeout=15)
|
37 |
response.raise_for_status()
|
38 |
questions_data = response.json()
|
39 |
+
print(f"π₯ Fetched {len(questions_data)} questions")
|
40 |
except Exception as e:
|
41 |
+
return f"β Error fetching questions: {e}", None
|
42 |
|
43 |
+
agent = GAIALlamaAgent()
|
44 |
answers_payload = []
|
45 |
results_log = []
|
|
|
46 |
|
47 |
for item in questions_data:
|
48 |
+
qid = item.get("task_id")
|
49 |
+
question = item.get("question")
|
50 |
+
if not qid or not question:
|
51 |
+
continue
|
52 |
try:
|
53 |
+
answer = agent(question)
|
54 |
except Exception as e:
|
55 |
+
answer = f"[AGENT ERROR] {e}"
|
56 |
+
answers_payload.append({"task_id": qid, "submitted_answer": answer})
|
57 |
+
results_log.append({"Task ID": qid, "Question": question, "Submitted Answer": answer})
|
58 |
+
|
59 |
+
if not answers_payload:
|
60 |
+
return "No answers to submit.", pd.DataFrame(results_log)
|
61 |
|
62 |
submission_data = {
|
63 |
"username": username,
|
|
|
66 |
}
|
67 |
|
68 |
try:
|
69 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
70 |
response.raise_for_status()
|
71 |
result_data = response.json()
|
72 |
status = (
|
|
|
78 |
)
|
79 |
return status, pd.DataFrame(results_log)
|
80 |
except Exception as e:
|
81 |
+
return f"β Submission failed: {e}", pd.DataFrame(results_log)
|
82 |
|
83 |
+
# --- Build Gradio Interface using Blocks ---
|
84 |
with gr.Blocks() as demo:
|
85 |
+
gr.Markdown("""
|
86 |
+
# π§ GAIA Agent Evaluation
|
87 |
+
|
88 |
+
This app runs a LlamaIndex + LangChain powered agent through the GAIA benchmark.
|
89 |
+
|
90 |
+
1. Login to Hugging Face below
|
91 |
+
2. Click **Run Evaluation** to test all questions
|
92 |
+
3. Answers will be submitted and scored
|
93 |
+
""")
|
94 |
gr.LoginButton()
|
95 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
96 |
+
status_output = gr.Textbox(label="Status", lines=5, interactive=False)
|
97 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
98 |
+
|
99 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
100 |
|
101 |
+
if __name__ == "__main__":
|
102 |
+
print("\nπ App Starting Up...")
|
103 |
+
if os.getenv("SPACE_ID"):
|
104 |
+
print(f"π Space: https://huggingface.co/spaces/{os.getenv('SPACE_ID')}")
|
105 |
+
demo.launch(debug=True)
|