Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,51 @@
|
|
1 |
import os
|
2 |
-
import
|
|
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
# ---
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
"""
|
24 |
-
return f"Summarize and reframe: {query}"
|
25 |
-
|
26 |
-
search_tool = DuckDuckGoSearchTool()
|
27 |
-
|
28 |
-
# --- ReACT + Scratchpad + Auto-Retry Instructions ---
|
29 |
-
|
30 |
-
instruction_prompt = """
|
31 |
-
You are a ReACT agent with scratchpad memory and a retry mechanism.
|
32 |
-
|
33 |
-
For every question:
|
34 |
-
1. Thought: Figure out what's needed.
|
35 |
-
2. Action: (Optional) Call a tool with a precise query.
|
36 |
-
3. Observation: Record tool output.
|
37 |
-
|
38 |
-
If the first Observation is empty or irrelevant:
|
39 |
-
4. Thought: Unclear result; reframe and retry.
|
40 |
-
5. Action: summarize_query(original question).
|
41 |
-
6. Action: DuckDuckGoSearchTool(reframed query).
|
42 |
-
7. Observation: Record new result.
|
43 |
-
|
44 |
-
Then:
|
45 |
-
8. Thought: Reflect on all observations.
|
46 |
-
9. FINAL ANSWER: Provide your answer.
|
47 |
-
|
48 |
-
Formatting rules:
|
49 |
-
- Begin with `FINAL ANSWER: [your answer]`
|
50 |
-
- Numbers: plain (no commas unless in a list)
|
51 |
-
- Strings: no articles unless part of proper names
|
52 |
-
- Lists: comma-separated without extra punctuation
|
53 |
-
"""
|
54 |
-
|
55 |
-
# --- Build the Smart Agent ---
|
56 |
-
|
57 |
-
smart_agent = CodeAgent(
|
58 |
-
tools=[search_tool, summarize_query],
|
59 |
-
model=HfApiModel()
|
60 |
-
)
|
61 |
-
|
62 |
-
# --- Integrate into Gradio App ---
|
63 |
|
|
|
64 |
class BasicAgent:
|
65 |
def __init__(self):
|
66 |
-
print("
|
67 |
|
68 |
def __call__(self, question: str) -> str:
|
69 |
-
|
70 |
-
print(f"Agent input (first 100 chars): {full_input[:100]}...")
|
71 |
try:
|
72 |
-
return
|
73 |
except Exception as e:
|
74 |
return f"AGENT ERROR: {e}"
|
75 |
|
|
|
76 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
77 |
if not profile:
|
78 |
-
return "Please log in to Hugging Face
|
79 |
username = profile.username
|
80 |
space_id = os.getenv("SPACE_ID", "")
|
81 |
agent = BasicAgent()
|
82 |
-
|
83 |
|
84 |
# 1. Fetch questions
|
85 |
try:
|
86 |
-
resp = requests.get(f"{
|
87 |
resp.raise_for_status()
|
88 |
questions = resp.json()
|
89 |
if not questions:
|
@@ -91,7 +53,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
91 |
except Exception as e:
|
92 |
return f"Error fetching questions: {e}", None
|
93 |
|
94 |
-
# 2.
|
95 |
logs = []
|
96 |
payload = []
|
97 |
for item in questions:
|
@@ -100,46 +62,46 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
100 |
if not tid or q is None:
|
101 |
continue
|
102 |
ans = agent(q)
|
103 |
-
logs.append({"Task ID": tid, "Question": q, "
|
104 |
payload.append({"task_id": tid, "submitted_answer": ans})
|
105 |
|
106 |
if not payload:
|
107 |
-
return "
|
108 |
-
|
109 |
-
# 3. Submit
|
110 |
-
submission = {
|
|
|
|
|
|
|
|
|
111 |
try:
|
112 |
-
post = requests.post(f"{
|
113 |
post.raise_for_status()
|
114 |
res = post.json()
|
115 |
status = (
|
116 |
-
f"
|
117 |
-
f"
|
118 |
-
f"
|
119 |
-
f"({res.get('correct_count', '?')}/{res.get('total_attempted', '?')})\n"
|
120 |
-
f"Message: {res.get('message', '')}"
|
121 |
)
|
122 |
return status, pd.DataFrame(logs)
|
123 |
except Exception as e:
|
124 |
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
125 |
|
126 |
# --- Gradio Interface ---
|
127 |
-
|
128 |
with gr.Blocks() as demo:
|
129 |
gr.Markdown("# SmolAgent GAIA Evaluation Runner π")
|
130 |
gr.Markdown(
|
131 |
"""
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
**Note:** Evaluation may take several minutes.
|
137 |
"""
|
138 |
)
|
139 |
gr.LoginButton()
|
140 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
141 |
status_out = gr.Textbox(label="Status", lines=5, interactive=False)
|
142 |
-
table_out = gr.DataFrame(label="
|
143 |
|
144 |
run_btn.click(fn=run_and_submit_all, outputs=[status_out, table_out])
|
145 |
|
|
|
1 |
import os
|
2 |
+
import openai # π https://www.google.com/search?q=openai.ChatCompletion.create
|
3 |
+
import gradio as gr # π£ https://www.google.com/search?q=Gradio+python
|
4 |
import requests
|
5 |
import pandas as pd
|
6 |
|
7 |
+
# --- Configuration ---
|
8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # π± https://www.google.com/search?q=python+environment+variables
|
9 |
+
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
+
MODEL_NAME = "gpt-4.1" # π https://www.google.com/search?q=gpt-4.1+openai+model+id
|
11 |
+
|
12 |
+
# --- ChatGPT-4.1 Caller ---
|
13 |
+
def ask_chatgpt_4_1(question: str) -> str:
|
14 |
+
response = openai.ChatCompletion.create(
|
15 |
+
model=MODEL_NAME,
|
16 |
+
messages=[
|
17 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
18 |
+
{"role": "user", "content": question}
|
19 |
+
],
|
20 |
+
temperature=0.7,
|
21 |
+
max_tokens=1500
|
22 |
+
)
|
23 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# --- Agent Class ---
|
26 |
class BasicAgent:
|
27 |
def __init__(self):
|
28 |
+
print("BasicAgent using OpenAI GPT-4.1 ready.")
|
29 |
|
30 |
def __call__(self, question: str) -> str:
|
31 |
+
print(f"Q>> {question}")
|
|
|
32 |
try:
|
33 |
+
return ask_chatgpt_4_1(question)
|
34 |
except Exception as e:
|
35 |
return f"AGENT ERROR: {e}"
|
36 |
|
37 |
+
# --- Evaluation & Submission ---
|
38 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
39 |
if not profile:
|
40 |
+
return "Please log in to Hugging Face.", None
|
41 |
username = profile.username
|
42 |
space_id = os.getenv("SPACE_ID", "")
|
43 |
agent = BasicAgent()
|
44 |
+
code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
45 |
|
46 |
# 1. Fetch questions
|
47 |
try:
|
48 |
+
resp = requests.get(f"{API_URL}/questions", timeout=15)
|
49 |
resp.raise_for_status()
|
50 |
questions = resp.json()
|
51 |
if not questions:
|
|
|
53 |
except Exception as e:
|
54 |
return f"Error fetching questions: {e}", None
|
55 |
|
56 |
+
# 2. Answer each
|
57 |
logs = []
|
58 |
payload = []
|
59 |
for item in questions:
|
|
|
62 |
if not tid or q is None:
|
63 |
continue
|
64 |
ans = agent(q)
|
65 |
+
logs.append({"Task ID": tid, "Question": q, "Answer": ans})
|
66 |
payload.append({"task_id": tid, "submitted_answer": ans})
|
67 |
|
68 |
if not payload:
|
69 |
+
return "No answers generated.", pd.DataFrame(logs)
|
70 |
+
|
71 |
+
# 3. Submit
|
72 |
+
submission = {
|
73 |
+
"username": username,
|
74 |
+
"agent_code": code_link,
|
75 |
+
"answers": payload
|
76 |
+
}
|
77 |
try:
|
78 |
+
post = requests.post(f"{API_URL}/submit", json=submission, timeout=60)
|
79 |
post.raise_for_status()
|
80 |
res = post.json()
|
81 |
status = (
|
82 |
+
f"Success! {res.get('username')} scored "
|
83 |
+
f"{res.get('score', 'N/A')}% "
|
84 |
+
f"({res.get('correct_count')}/{res.get('total_attempted')})"
|
|
|
|
|
85 |
)
|
86 |
return status, pd.DataFrame(logs)
|
87 |
except Exception as e:
|
88 |
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
89 |
|
90 |
# --- Gradio Interface ---
|
|
|
91 |
with gr.Blocks() as demo:
|
92 |
gr.Markdown("# SmolAgent GAIA Evaluation Runner π")
|
93 |
gr.Markdown(
|
94 |
"""
|
95 |
+
1. Clone space and modify if needed
|
96 |
+
2. Log in to Hugging Face
|
97 |
+
3. Click **Run Evaluation & Submit All Answers**
|
98 |
+
(May take several minutes)
|
|
|
99 |
"""
|
100 |
)
|
101 |
gr.LoginButton()
|
102 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
103 |
status_out = gr.Textbox(label="Status", lines=5, interactive=False)
|
104 |
+
table_out = gr.DataFrame(label="Q&A Log", wrap=True)
|
105 |
|
106 |
run_btn.click(fn=run_and_submit_all, outputs=[status_out, table_out])
|
107 |
|