Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,79 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
-
import requests
|
| 5 |
-
import openai
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
class
|
| 13 |
def __init__(self):
|
| 14 |
-
print("
|
| 15 |
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
print(f"[Question] {question[:60]}...")
|
| 19 |
-
response = openai.ChatCompletion.create(
|
| 20 |
-
model="gpt-3.5-turbo", # use free-tier model
|
| 21 |
-
messages=[
|
| 22 |
-
{"role": "system", "content": "You are a helpful, accurate, and concise AI assistant."},
|
| 23 |
-
{"role": "user", "content": question}
|
| 24 |
-
],
|
| 25 |
-
temperature=0.3,
|
| 26 |
-
max_tokens=500
|
| 27 |
-
)
|
| 28 |
-
return response.choices[0].message["content"].strip()
|
| 29 |
-
except Exception as e:
|
| 30 |
-
print(f"[OpenAI Error]: {e}")
|
| 31 |
-
return "Error: Could not generate answer."
|
| 32 |
|
| 33 |
-
# --- Main
|
| 34 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 35 |
-
if not
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
username = profile.username
|
| 39 |
-
space_id = os.getenv("SPACE_ID", "your-username/your-space-name")
|
| 40 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 41 |
-
agent = OpenAIAgent()
|
| 42 |
|
| 43 |
try:
|
| 44 |
-
response = requests.get(
|
| 45 |
response.raise_for_status()
|
| 46 |
questions = response.json()
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"❌
|
| 49 |
|
| 50 |
results_log = []
|
| 51 |
answers_payload = []
|
|
@@ -55,40 +83,49 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 55 |
question_text = item.get("question")
|
| 56 |
if not task_id or not question_text:
|
| 57 |
continue
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
"Task ID": task_id,
|
| 62 |
-
|
| 63 |
-
"Submitted Answer":
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
"username": username,
|
| 68 |
"agent_code": agent_code,
|
| 69 |
"answers": answers_payload
|
| 70 |
}
|
| 71 |
|
| 72 |
try:
|
| 73 |
-
resp = requests.post(
|
| 74 |
resp.raise_for_status()
|
| 75 |
result = resp.json()
|
| 76 |
score = result.get("score", "N/A")
|
| 77 |
correct = result.get("correct_count", "?")
|
| 78 |
total = result.get("total_attempted", "?")
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
return f"❌ Submission failed: {e}", pd.DataFrame(results_log)
|
| 83 |
|
| 84 |
# --- Gradio UI ---
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
-
gr.Markdown("## 🤖 GAIA Benchmark Agent Submission
|
| 87 |
-
gr.Markdown("
|
|
|
|
| 88 |
gr.LoginButton()
|
| 89 |
-
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 90 |
status = gr.Textbox(label="Status", lines=5)
|
| 91 |
table = gr.DataFrame(label="Results")
|
|
|
|
| 92 |
run_btn.click(fn=run_and_submit_all, outputs=[status, table])
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# --- Constants ---
|
|
|
|
| 6 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 7 |
|
| 8 |
+
# --- Dummy Agent ---
|
| 9 |
+
class DummyAgent:
|
| 10 |
def __init__(self):
|
| 11 |
+
print("DummyAgent initialized (no SSL support).")
|
| 12 |
|
| 13 |
def __call__(self, question: str) -> str:
|
| 14 |
+
return "Error: Could not generate answer."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# --- Main Run & Submit Function ---
|
| 17 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 18 |
+
# Use your actual Space ID here as fallback if env not set
|
| 19 |
+
space_id = os.getenv("SPACE_ID", "XeroDN/final_assignment")
|
| 20 |
+
|
| 21 |
+
if profile:
|
| 22 |
+
username = profile.username
|
| 23 |
+
print(f"✅ Logged in as: {username}")
|
| 24 |
+
else:
|
| 25 |
+
return "⚠️ Please log in to Hugging Face first.", None
|
| 26 |
+
|
| 27 |
+
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 28 |
+
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
import ssl
|
| 32 |
+
import requests
|
| 33 |
+
ssl_supported = True
|
| 34 |
+
except ImportError:
|
| 35 |
+
ssl_supported = False
|
| 36 |
+
|
| 37 |
+
if ssl_supported:
|
| 38 |
+
try:
|
| 39 |
+
from openai import OpenAI
|
| 40 |
+
|
| 41 |
+
class OpenAIAgent:
|
| 42 |
+
def __init__(self):
|
| 43 |
+
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 44 |
+
|
| 45 |
+
def __call__(self, question: str) -> str:
|
| 46 |
+
try:
|
| 47 |
+
response = self.client.chat.completions.create(
|
| 48 |
+
model="gpt-3.5-turbo", # or "gpt-4" if you have access
|
| 49 |
+
messages=[
|
| 50 |
+
{"role": "system", "content": "You are a helpful and precise reasoning agent. Think step-by-step, and be concise."},
|
| 51 |
+
{"role": "user", "content": question}
|
| 52 |
+
]
|
| 53 |
+
)
|
| 54 |
+
return response.choices[0].message.content.strip()
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"[OpenAI API Error] {e}")
|
| 57 |
+
return "Error: Could not generate answer."
|
| 58 |
+
|
| 59 |
+
agent = OpenAIAgent()
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print("Falling back to DummyAgent:", e)
|
| 63 |
+
agent = DummyAgent()
|
| 64 |
+
else:
|
| 65 |
+
agent = DummyAgent()
|
| 66 |
|
|
|
|
|
|
|
| 67 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
| 68 |
|
| 69 |
try:
|
| 70 |
+
response = requests.get(questions_url, timeout=15)
|
| 71 |
response.raise_for_status()
|
| 72 |
questions = response.json()
|
| 73 |
+
if not questions:
|
| 74 |
+
return "⚠️ No questions fetched.", None
|
| 75 |
except Exception as e:
|
| 76 |
+
return f"❌ Error fetching questions: {e}", None
|
| 77 |
|
| 78 |
results_log = []
|
| 79 |
answers_payload = []
|
|
|
|
| 83 |
question_text = item.get("question")
|
| 84 |
if not task_id or not question_text:
|
| 85 |
continue
|
| 86 |
+
try:
|
| 87 |
+
submitted_answer = agent(question_text)
|
| 88 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 89 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 90 |
+
except Exception as e:
|
| 91 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"Error: {e}"})
|
| 92 |
+
|
| 93 |
+
if not answers_payload:
|
| 94 |
+
return "⚠️ Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 95 |
+
|
| 96 |
+
submission = {
|
| 97 |
"username": username,
|
| 98 |
"agent_code": agent_code,
|
| 99 |
"answers": answers_payload
|
| 100 |
}
|
| 101 |
|
| 102 |
try:
|
| 103 |
+
resp = requests.post(submit_url, json=submission, timeout=60)
|
| 104 |
resp.raise_for_status()
|
| 105 |
result = resp.json()
|
| 106 |
score = result.get("score", "N/A")
|
| 107 |
correct = result.get("correct_count", "?")
|
| 108 |
total = result.get("total_attempted", "?")
|
| 109 |
+
|
| 110 |
+
final_msg = (
|
| 111 |
+
f"✅ Submission Successful!\n"
|
| 112 |
+
f"User: {username}\n"
|
| 113 |
+
f"Score: {score}% ({correct}/{total} correct)"
|
| 114 |
+
)
|
| 115 |
+
return final_msg, pd.DataFrame(results_log)
|
| 116 |
except Exception as e:
|
| 117 |
return f"❌ Submission failed: {e}", pd.DataFrame(results_log)
|
| 118 |
|
| 119 |
# --- Gradio UI ---
|
| 120 |
with gr.Blocks() as demo:
|
| 121 |
+
gr.Markdown("## 🤖 GAIA Benchmark Agent Submission")
|
| 122 |
+
gr.Markdown("Click below to log in and run your agent on the GAIA benchmark.")
|
| 123 |
+
|
| 124 |
gr.LoginButton()
|
| 125 |
+
run_btn = gr.Button("🚀 Run Evaluation & Submit All Answers")
|
| 126 |
status = gr.Textbox(label="Status", lines=5)
|
| 127 |
table = gr.DataFrame(label="Results")
|
| 128 |
+
|
| 129 |
run_btn.click(fn=run_and_submit_all, outputs=[status, table])
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|