Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,71 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
-
import
|
5 |
import pandas as pd
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
-
# ---
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def __init__(self):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
19 |
)
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
)
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
def read_file(self, filename: str) -> str:
|
32 |
-
filepath = os.path.join("./", filename)
|
33 |
-
if filename.endswith(".txt") and os.path.exists(filepath):
|
34 |
-
with open(filepath, "r") as file:
|
35 |
-
return file.read()[:1000] # limit to 1000 chars
|
36 |
-
return ""
|
37 |
-
|
38 |
-
def __call__(self, question: str, file_name: str = None) -> str:
|
39 |
-
file_content = self.read_file(file_name) if file_name else None
|
40 |
-
prompt = self.format_prompt(question, file_content)
|
41 |
try:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
if not result or not isinstance(result, str):
|
46 |
-
return "AGENT ERROR: Empty or invalid response"
|
47 |
-
return result.strip().split("Answer:")[-1].strip()
|
48 |
except Exception as e:
|
49 |
-
print(f"
|
50 |
return f"AGENT ERROR: {e}"
|
51 |
|
52 |
-
|
53 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
54 |
-
space_id =
|
55 |
if profile:
|
56 |
username = f"{profile.username}"
|
57 |
-
print(f"User logged in: {username}")
|
58 |
else:
|
59 |
-
print("User not logged in.")
|
60 |
return "Please Login to Hugging Face with the button.", None
|
61 |
|
62 |
api_url = DEFAULT_API_URL
|
@@ -64,7 +73,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
64 |
submit_url = f"{api_url}/submit"
|
65 |
|
66 |
try:
|
67 |
-
agent =
|
68 |
except Exception as e:
|
69 |
return f"Error initializing agent: {e}", None
|
70 |
|
@@ -82,11 +91,18 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
82 |
for item in questions_data:
|
83 |
task_id = item.get("task_id")
|
84 |
question_text = item.get("question")
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
if not task_id or question_text is None:
|
87 |
continue
|
|
|
88 |
try:
|
89 |
-
submitted_answer = agent(
|
90 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
91 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
92 |
except Exception as e:
|
@@ -112,9 +128,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
112 |
except Exception as e:
|
113 |
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
114 |
|
115 |
-
|
116 |
with gr.Blocks() as demo:
|
117 |
-
gr.Markdown("#
|
118 |
gr.Markdown("""
|
119 |
**Instructions:**
|
120 |
1. Log in to your Hugging Face account.
|
@@ -125,12 +141,8 @@ with gr.Blocks() as demo:
|
|
125 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
126 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
127 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
128 |
-
|
129 |
-
run_button.click(
|
130 |
-
fn=run_and_submit_all,
|
131 |
-
outputs=[status_output, results_table]
|
132 |
-
)
|
133 |
|
134 |
if __name__ == "__main__":
|
135 |
print("Launching GAIA agent app...")
|
136 |
-
demo.launch(debug=True, share=False)
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
+
from smolagents import HfApiModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
|
5 |
import pandas as pd
|
6 |
+
import tempfile
|
7 |
+
from pathlib import Path
|
8 |
+
import re
|
9 |
|
10 |
# --- Constants ---
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
13 |
+
# --- File Handling ---
|
14 |
+
def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
15 |
+
url = f"{base_api_url}/files/{task_id}"
|
16 |
+
try:
|
17 |
+
resp = requests.get(url, timeout=30)
|
18 |
+
if resp.status_code == 404:
|
19 |
+
return None
|
20 |
+
resp.raise_for_status()
|
21 |
+
except requests.exceptions.HTTPError as e:
|
22 |
+
raise e
|
23 |
+
cdisp = resp.headers.get("content-disposition", "")
|
24 |
+
filename = task_id
|
25 |
+
if "filename=" in cdisp:
|
26 |
+
m = re.search(r'filename="([^\"]+)"', cdisp)
|
27 |
+
if m:
|
28 |
+
filename = m.group(1)
|
29 |
+
tmp_dir = Path(tempfile.gettempdir()) / "gaia_files"
|
30 |
+
tmp_dir.mkdir(exist_ok=True)
|
31 |
+
file_path = tmp_dir / filename
|
32 |
+
with open(file_path, "wb") as f:
|
33 |
+
f.write(resp.content)
|
34 |
+
return str(file_path)
|
35 |
+
|
36 |
+
# --- Basic Agent Definition ---
|
37 |
+
class BasicAgent:
|
38 |
def __init__(self):
|
39 |
+
model = HfApiModel(
|
40 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
41 |
+
max_tokens=2096,
|
42 |
+
temperature=0.5,
|
43 |
+
custom_role_conversions=None,
|
44 |
)
|
45 |
+
self.agent = CodeAgent(
|
46 |
+
model=model,
|
47 |
+
tools=[DuckDuckGoSearchTool(), WikipediaSearchTool()],
|
48 |
+
add_base_tools=True,
|
49 |
+
additional_authorized_imports=[]
|
50 |
)
|
51 |
+
print("BasicAgent initialized.")
|
52 |
+
|
53 |
+
def __call__(self, question: str) -> str:
|
54 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
try:
|
56 |
+
fixed_answer = self.agent.run(question)
|
57 |
+
print(f"Agent returning answer: {fixed_answer}")
|
58 |
+
return fixed_answer
|
|
|
|
|
|
|
59 |
except Exception as e:
|
60 |
+
print(f"Error during inference: {e}")
|
61 |
return f"AGENT ERROR: {e}"
|
62 |
|
63 |
+
# --- Run and Submit ---
|
64 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
65 |
+
space_id = "l3xv/Final_Assignment_Template"
|
66 |
if profile:
|
67 |
username = f"{profile.username}"
|
|
|
68 |
else:
|
|
|
69 |
return "Please Login to Hugging Face with the button.", None
|
70 |
|
71 |
api_url = DEFAULT_API_URL
|
|
|
73 |
submit_url = f"{api_url}/submit"
|
74 |
|
75 |
try:
|
76 |
+
agent = BasicAgent()
|
77 |
except Exception as e:
|
78 |
return f"Error initializing agent: {e}", None
|
79 |
|
|
|
91 |
for item in questions_data:
|
92 |
task_id = item.get("task_id")
|
93 |
question_text = item.get("question")
|
94 |
+
try:
|
95 |
+
file_path = download_file_if_any(api_url, task_id)
|
96 |
+
except Exception as e:
|
97 |
+
file_path = None
|
98 |
+
|
99 |
+
q_for_agent = f"{question_text}\n\n---\nFile: {file_path}\n---\n" if file_path else question_text
|
100 |
+
|
101 |
if not task_id or question_text is None:
|
102 |
continue
|
103 |
+
|
104 |
try:
|
105 |
+
submitted_answer = agent(q_for_agent)
|
106 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
107 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
108 |
except Exception as e:
|
|
|
128 |
except Exception as e:
|
129 |
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
130 |
|
131 |
+
# --- UI ---
|
132 |
with gr.Blocks() as demo:
|
133 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
134 |
gr.Markdown("""
|
135 |
**Instructions:**
|
136 |
1. Log in to your Hugging Face account.
|
|
|
141 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
142 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
143 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
144 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
145 |
|
146 |
if __name__ == "__main__":
|
147 |
print("Launching GAIA agent app...")
|
148 |
+
demo.launch(debug=True, share=False)
|