Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from agent import build_agent, answer
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
return answer(build_agent(use_case=True), message)
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import threading
|
| 3 |
+
import requests
|
| 4 |
import gradio as gr
|
| 5 |
from agent import build_agent, answer
|
| 6 |
|
| 7 |
+
OWNER = "SystemReset"
|
| 8 |
+
lock = threading.Lock()
|
| 9 |
+
|
| 10 |
+
def require_owner(profile):
|
| 11 |
+
if profile is None or profile.username != OWNER:
|
| 12 |
+
raise gr.Error("Sign in as SystemReset to use these controls.")
|
| 13 |
+
|
| 14 |
+
def respond(message, profile: gr.OAuthProfile | None):
|
| 15 |
+
require_owner(profile)
|
| 16 |
return answer(build_agent(use_case=True), message)
|
| 17 |
|
| 18 |
+
def run_challenge(profile: gr.OAuthProfile | None):
|
| 19 |
+
require_owner(profile)
|
| 20 |
+
if not lock.acquire(blocking=False):
|
| 21 |
+
raise gr.Error("An evaluation is already running.")
|
| 22 |
+
try:
|
| 23 |
+
response = requests.get("https://agents-course-unit4-scoring.hf.space/questions", timeout=60)
|
| 24 |
+
response.raise_for_status()
|
| 25 |
+
questions = response.json()
|
| 26 |
+
agent = build_agent()
|
| 27 |
+
results = []
|
| 28 |
+
for index, item in enumerate(questions):
|
| 29 |
+
if item.get("file_name"):
|
| 30 |
+
results.append({"task_id": item["task_id"], "submitted_answer": "", "status": "attachment unsupported"})
|
| 31 |
+
else:
|
| 32 |
+
try:
|
| 33 |
+
result = answer(agent, item["question"])
|
| 34 |
+
results.append({"task_id": item["task_id"], "submitted_answer": result, "status": "generated"})
|
| 35 |
+
except Exception as exc:
|
| 36 |
+
results.append({"task_id": item["task_id"], "submitted_answer": "", "status": type(exc).__name__})
|
| 37 |
+
yield f"Stopped at question {index+1}: {type(exc).__name__}", results
|
| 38 |
+
return
|
| 39 |
+
with open("challenge_results.json", "w") as handle:
|
| 40 |
+
json.dump(results, handle)
|
| 41 |
+
yield f"Processed {index+1}/{len(questions)} questions", results
|
| 42 |
+
finally:
|
| 43 |
+
lock.release()
|
| 44 |
+
|
| 45 |
+
def submit_challenge(profile: gr.OAuthProfile | None):
|
| 46 |
+
require_owner(profile)
|
| 47 |
+
if lock.locked():
|
| 48 |
+
raise gr.Error("Wait for evaluation to finish.")
|
| 49 |
+
with open("challenge_results.json") as handle:
|
| 50 |
+
results = json.load(handle)
|
| 51 |
+
payload = {"username": OWNER, "agent_code": "https://huggingface.co/spaces/SystemReset/alfred-agents-course/tree/main", "answers": [{"task_id": row["task_id"], "submitted_answer": row["submitted_answer"]} for row in results]}
|
| 52 |
+
response = requests.post("https://agents-course-unit4-scoring.hf.space/submit", json=payload, timeout=120)
|
| 53 |
+
response.raise_for_status()
|
| 54 |
+
return response.json()
|
| 55 |
+
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("# Alfred: Course Research Agent
|
| 58 |
+
Sign in as SystemReset. Code is public; model calls and challenge controls are restricted to the owner.")
|
| 59 |
+
gr.LoginButton()
|
| 60 |
+
prompt = gr.Textbox(label="Question")
|
| 61 |
+
reply = gr.Textbox(label="Answer")
|
| 62 |
+
gr.Button("Ask Alfred").click(respond, inputs=[prompt], outputs=[reply])
|
| 63 |
+
gr.Markdown("## Final challenge
|
| 64 |
+
Runs the real agent on benchmark questions. Attachment tasks are explicitly unsupported. Submit publishes generated answers and score to the course leaderboard.")
|
| 65 |
+
status = gr.Textbox(label="Progress")
|
| 66 |
+
results = gr.JSON(label="Generated answers")
|
| 67 |
+
gr.Button("Run final challenge").click(run_challenge, outputs=[status, results])
|
| 68 |
+
gr.Button("Submit generated answers").click(submit_challenge, outputs=[results])
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.queue().launch()
|