Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,48 @@
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool,
|
| 7 |
|
| 8 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
AGENT_CODE_URL = "https://huggingface.co/spaces/bacchi87/Final_Assignment_Template/tree/main"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
model =
|
| 13 |
-
model_id="
|
| 14 |
-
|
|
|
|
| 15 |
max_tokens=1500
|
| 16 |
)
|
| 17 |
|
| 18 |
agent = CodeAgent(
|
| 19 |
tools=[DuckDuckGoSearchTool()],
|
| 20 |
model=model,
|
| 21 |
-
additional_authorized_imports=[
|
|
|
|
|
|
|
| 22 |
max_steps=6
|
| 23 |
)
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def get_attachment(task_id):
|
| 27 |
-
"""Download and read spreadsheet attachments, if available."""
|
| 28 |
try:
|
| 29 |
-
response = requests.get(
|
|
|
|
|
|
|
| 30 |
response.raise_for_status()
|
| 31 |
data = BytesIO(response.content)
|
| 32 |
|
|
@@ -39,10 +55,10 @@ def get_attachment(task_id):
|
|
| 39 |
df = pd.read_csv(data)
|
| 40 |
return df.to_string(index=False)
|
| 41 |
except Exception:
|
| 42 |
-
return "Attachment
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
-
return f"Attachment
|
| 46 |
|
| 47 |
|
| 48 |
def answer_question(question, attachment=""):
|
|
@@ -52,11 +68,11 @@ Answer this question accurately.
|
|
| 52 |
Question:
|
| 53 |
{question}
|
| 54 |
|
| 55 |
-
Attached file data
|
| 56 |
{attachment}
|
| 57 |
|
| 58 |
Use web search or calculations when required.
|
| 59 |
-
Return ONLY the answer.
|
| 60 |
Do not write FINAL ANSWER.
|
| 61 |
"""
|
| 62 |
try:
|
|
@@ -68,6 +84,7 @@ Do not write FINAL ANSWER.
|
|
| 68 |
|
| 69 |
@spaces.GPU
|
| 70 |
def run_evaluation(profile: gr.OAuthProfile | None):
|
|
|
|
| 71 |
if profile is None:
|
| 72 |
return "Please login with Hugging Face.", []
|
| 73 |
|
|
@@ -75,12 +92,14 @@ def run_evaluation(profile: gr.OAuthProfile | None):
|
|
| 75 |
log = f"Logged in as: {username}\n"
|
| 76 |
|
| 77 |
try:
|
| 78 |
-
response = requests.get(
|
|
|
|
|
|
|
| 79 |
response.raise_for_status()
|
| 80 |
questions = response.json()
|
| 81 |
|
| 82 |
if not questions:
|
| 83 |
-
return "No
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
return f"Could not fetch questions: {e}", []
|
|
@@ -88,21 +107,23 @@ def run_evaluation(profile: gr.OAuthProfile | None):
|
|
| 88 |
answers = []
|
| 89 |
|
| 90 |
for i, item in enumerate(questions, start=1):
|
|
|
|
| 91 |
task_id = str(item["task_id"])
|
| 92 |
question = item["question"]
|
| 93 |
|
| 94 |
log += f"\nQuestion {i}/{len(questions)}: {task_id}\n"
|
| 95 |
|
| 96 |
attachment = ""
|
|
|
|
| 97 |
if item.get("file_name"):
|
| 98 |
attachment = get_attachment(task_id)
|
| 99 |
|
| 100 |
answer = answer_question(question, attachment)
|
| 101 |
|
| 102 |
-
# Stop if
|
| 103 |
if answer.startswith("Agent error:"):
|
| 104 |
log += f"Model inference failed: {answer}\n"
|
| 105 |
-
return log + "\nNo answers submitted.
|
| 106 |
|
| 107 |
answers.append({
|
| 108 |
"task_id": task_id,
|
|
@@ -111,7 +132,7 @@ def run_evaluation(profile: gr.OAuthProfile | None):
|
|
| 111 |
|
| 112 |
log += f"Answer: {answer}\n"
|
| 113 |
|
| 114 |
-
#
|
| 115 |
payload = {
|
| 116 |
"username": username,
|
| 117 |
"agent_code": AGENT_CODE_URL,
|
|
@@ -125,6 +146,7 @@ def run_evaluation(profile: gr.OAuthProfile | None):
|
|
| 125 |
timeout=60
|
| 126 |
)
|
| 127 |
result.raise_for_status()
|
|
|
|
| 128 |
score_data = result.json()
|
| 129 |
|
| 130 |
log += "\n--- EVALUATION RESULT ---\n"
|
|
@@ -135,17 +157,25 @@ def run_evaluation(profile: gr.OAuthProfile | None):
|
|
| 135 |
|
| 136 |
except Exception as e:
|
| 137 |
log += f"\nSubmission failed: {e}\n"
|
| 138 |
-
if "result" in locals():
|
| 139 |
-
log += result.text[:1000]
|
| 140 |
|
| 141 |
return log, answers
|
| 142 |
|
| 143 |
|
| 144 |
with gr.Blocks() as demo:
|
|
|
|
| 145 |
gr.Markdown("# Hugging Face Unit 4 - GAIA Evaluation")
|
| 146 |
|
| 147 |
gr.LoginButton()
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
run_btn = gr.Button(
|
| 150 |
"Run Evaluation & Submit Answers",
|
| 151 |
variant="primary"
|
|
|
|
| 1 |
+
import os
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
import spaces
|
| 6 |
+
|
| 7 |
from io import BytesIO
|
| 8 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
|
| 9 |
|
| 10 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
AGENT_CODE_URL = "https://huggingface.co/spaces/bacchi87/Final_Assignment_Template/tree/main"
|
| 12 |
|
| 13 |
+
# Gemini model setup
|
| 14 |
+
model = OpenAIServerModel(
|
| 15 |
+
model_id="gemini-3.8-flash",
|
| 16 |
+
api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 17 |
+
api_key=os.environ["GEMINI_API_KEY"],
|
| 18 |
max_tokens=1500
|
| 19 |
)
|
| 20 |
|
| 21 |
agent = CodeAgent(
|
| 22 |
tools=[DuckDuckGoSearchTool()],
|
| 23 |
model=model,
|
| 24 |
+
additional_authorized_imports=[
|
| 25 |
+
"math", "re", "json", "datetime", "pandas"
|
| 26 |
+
],
|
| 27 |
max_steps=6
|
| 28 |
)
|
| 29 |
|
| 30 |
|
| 31 |
+
def test_gemini():
|
| 32 |
+
try:
|
| 33 |
+
result = agent.run(
|
| 34 |
+
"Reply with exactly GEMINI_OK. Do not use tools."
|
| 35 |
+
)
|
| 36 |
+
return f"Gemini connection successful!\nResponse: {result}"
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Gemini connection failed:\n{e}"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
def get_attachment(task_id):
|
|
|
|
| 42 |
try:
|
| 43 |
+
response = requests.get(
|
| 44 |
+
f"{API_URL}/files/{task_id}", timeout=30
|
| 45 |
+
)
|
| 46 |
response.raise_for_status()
|
| 47 |
data = BytesIO(response.content)
|
| 48 |
|
|
|
|
| 55 |
df = pd.read_csv(data)
|
| 56 |
return df.to_string(index=False)
|
| 57 |
except Exception:
|
| 58 |
+
return "Attachment format is not readable."
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
+
return f"Attachment download failed: {e}"
|
| 62 |
|
| 63 |
|
| 64 |
def answer_question(question, attachment=""):
|
|
|
|
| 68 |
Question:
|
| 69 |
{question}
|
| 70 |
|
| 71 |
+
Attached file data:
|
| 72 |
{attachment}
|
| 73 |
|
| 74 |
Use web search or calculations when required.
|
| 75 |
+
Return ONLY the answer.
|
| 76 |
Do not write FINAL ANSWER.
|
| 77 |
"""
|
| 78 |
try:
|
|
|
|
| 84 |
|
| 85 |
@spaces.GPU
|
| 86 |
def run_evaluation(profile: gr.OAuthProfile | None):
|
| 87 |
+
|
| 88 |
if profile is None:
|
| 89 |
return "Please login with Hugging Face.", []
|
| 90 |
|
|
|
|
| 92 |
log = f"Logged in as: {username}\n"
|
| 93 |
|
| 94 |
try:
|
| 95 |
+
response = requests.get(
|
| 96 |
+
f"{API_URL}/questions", timeout=30
|
| 97 |
+
)
|
| 98 |
response.raise_for_status()
|
| 99 |
questions = response.json()
|
| 100 |
|
| 101 |
if not questions:
|
| 102 |
+
return "No questions returned.", []
|
| 103 |
|
| 104 |
except Exception as e:
|
| 105 |
return f"Could not fetch questions: {e}", []
|
|
|
|
| 107 |
answers = []
|
| 108 |
|
| 109 |
for i, item in enumerate(questions, start=1):
|
| 110 |
+
|
| 111 |
task_id = str(item["task_id"])
|
| 112 |
question = item["question"]
|
| 113 |
|
| 114 |
log += f"\nQuestion {i}/{len(questions)}: {task_id}\n"
|
| 115 |
|
| 116 |
attachment = ""
|
| 117 |
+
|
| 118 |
if item.get("file_name"):
|
| 119 |
attachment = get_attachment(task_id)
|
| 120 |
|
| 121 |
answer = answer_question(question, attachment)
|
| 122 |
|
| 123 |
+
# Stop if Gemini fails
|
| 124 |
if answer.startswith("Agent error:"):
|
| 125 |
log += f"Model inference failed: {answer}\n"
|
| 126 |
+
return log + "\nNo answers submitted.", answers
|
| 127 |
|
| 128 |
answers.append({
|
| 129 |
"task_id": task_id,
|
|
|
|
| 132 |
|
| 133 |
log += f"Answer: {answer}\n"
|
| 134 |
|
| 135 |
+
# Official scoring API
|
| 136 |
payload = {
|
| 137 |
"username": username,
|
| 138 |
"agent_code": AGENT_CODE_URL,
|
|
|
|
| 146 |
timeout=60
|
| 147 |
)
|
| 148 |
result.raise_for_status()
|
| 149 |
+
|
| 150 |
score_data = result.json()
|
| 151 |
|
| 152 |
log += "\n--- EVALUATION RESULT ---\n"
|
|
|
|
| 157 |
|
| 158 |
except Exception as e:
|
| 159 |
log += f"\nSubmission failed: {e}\n"
|
|
|
|
|
|
|
| 160 |
|
| 161 |
return log, answers
|
| 162 |
|
| 163 |
|
| 164 |
with gr.Blocks() as demo:
|
| 165 |
+
|
| 166 |
gr.Markdown("# Hugging Face Unit 4 - GAIA Evaluation")
|
| 167 |
|
| 168 |
gr.LoginButton()
|
| 169 |
|
| 170 |
+
test_btn = gr.Button("Test Gemini Connection")
|
| 171 |
+
test_output = gr.Textbox(label="Gemini Test Result")
|
| 172 |
+
|
| 173 |
+
test_btn.click(
|
| 174 |
+
fn=test_gemini,
|
| 175 |
+
inputs=[],
|
| 176 |
+
outputs=[test_output]
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
run_btn = gr.Button(
|
| 180 |
"Run Evaluation & Submit Answers",
|
| 181 |
variant="primary"
|