dlaima's picture
Update app.py
05b8101 verified
raw
history blame
4.47 kB
import os
import gradio as gr
import requests
import pandas as pd
from smolagents import CodeAgent, DuckDuckGoSearchTool
from smolagents.models import OpenAIServerModel
import openai
# --- Setup ---
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise RuntimeError("Please set OPENAI_API_KEY in your Space secrets.")
openai.api_key = OPENAI_API_KEY
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4o")
model = OpenAIServerModel(model_id=OPENAI_MODEL_ID, api_key=OPENAI_API_KEY)
search_tool = DuckDuckGoSearchTool()
agent = CodeAgent(tools=[search_tool], model=model)
answer_formatting_prompt = """
You are a smart assistant with access to tools like DuckDuckGoSearchTool(query: str).
Think step-by-step, then output your response.
IMPORTANT:
FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers/strings.
Do NOT include commas, $ or % unless asked.
Write digits plainly (e.g., '10', not 'ten').
Use format:
FINAL ANSWER: <your_answer>
"""
def show_profile(profile):
if not profile:
return "⚠️ Not logged in."
return f"βœ… Logged in as: {profile['username']}"
def run_and_submit_all(login_info):
# login_info comes from LoginButton, it's None if not logged in
if not login_info:
return "⚠️ Please log in with your Hugging Face account.", pd.DataFrame()
username = login_info["username"]
space_id = os.getenv("SPACE_ID", "")
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
try:
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
resp.raise_for_status()
questions = resp.json()
except Exception as e:
return f"❌ Error fetching questions: {e}", pd.DataFrame()
results, payload = [], []
for item in questions:
task_id = item.get("task_id")
question = item.get("question")
if not task_id or not question:
continue
prompt = answer_formatting_prompt.strip() + f"\n\nQUESTION: {question.strip()}"
try:
answer = agent.run(prompt)
except Exception as e:
answer = f"AGENT ERROR: {e}"
results.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
payload.append({"task_id": task_id, "submitted_answer": answer})
if not payload:
return "⚠️ Agent returned no answers.", pd.DataFrame(results)
try:
post = requests.post(
f"{DEFAULT_API_URL}/submit",
json={"username": username, "agent_code": agent_code, "answers": payload},
timeout=60
)
post.raise_for_status()
result = post.json()
score = result.get("score", "N/A")
correct = result.get("correct_count", "?")
attempted = result.get("total_attempted", "?")
message = result.get("message", "")
return (
f"βœ… Submission Successful!\nUser: {username}\nScore: {score}% "
f"({correct}/{attempted})\nMessage: {message}",
pd.DataFrame(results)
)
except Exception as e:
return f"❌ Submission failed: {e}", pd.DataFrame(results)
with gr.Blocks() as demo:
gr.Markdown("# Basic Agent Evaluation Runner")
login_button = gr.LoginButton()
login_status = gr.Textbox(label="Login Status")
run_button = gr.Button("Run Evaluation & Submit All Answers")
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
# Show login status when user logs in
login_button.click(fn=show_profile, inputs=[login_button], outputs=[login_status])
# Run evaluation on click, pass login_button's state as input
run_button.click(fn=run_and_submit_all, inputs=[login_button], outputs=[status_output, results_table])
if __name__ == "__main__":
demo.launch()
#import gradio as gr
#def show_profile(profile):
# if not profile:
# return "⚠️ Not logged in."
# return f"βœ… Logged in as: {profile['username']}"
# with gr.Blocks() as demo:
# gr.Markdown("## πŸ” Hugging Face OAuth Login")
# login_button = gr.LoginButton()
# output = gr.Textbox(label="Login Status")
# login_button.click(fn=show_profile, inputs=[login_button], outputs=[output])
# demo.launch()