Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,20 +4,24 @@ import os
|
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
|
|
|
| 7 |
|
| 8 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
| 9 |
from smolagents.models import OpenAIServerModel
|
| 10 |
|
| 11 |
-
# Define the system prompt
|
| 12 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
| 13 |
-
|
| 14 |
-
FINAL ANSWER: [YOUR FINAL ANSWER]
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 19 |
|
| 20 |
-
# Patched model to prepend system prompt correctly
|
| 21 |
class PatchedOpenAIServerModel(OpenAIServerModel):
|
| 22 |
def generate(self, messages, stop_sequences=None, **kwargs):
|
| 23 |
if isinstance(messages, list):
|
|
@@ -36,11 +40,21 @@ class MyAgent:
|
|
| 36 |
def __call__(self, question: str) -> str:
|
| 37 |
return self.agent.run(question)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 40 |
space_id = os.getenv("SPACE_ID")
|
| 41 |
|
| 42 |
if profile:
|
| 43 |
-
username = profile.username
|
| 44 |
print(f"User logged in: {username}")
|
| 45 |
else:
|
| 46 |
print("User not logged in.")
|
|
@@ -79,7 +93,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
continue
|
| 81 |
try:
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 84 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 85 |
except Exception as e:
|
|
@@ -89,7 +105,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 89 |
if not answers_payload:
|
| 90 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 91 |
|
| 92 |
-
submission_data = {"username": username
|
| 93 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 94 |
try:
|
| 95 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
@@ -115,7 +131,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 115 |
except Exception as e:
|
| 116 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
| 117 |
|
| 118 |
-
# Gradio UI setup
|
| 119 |
with gr.Blocks() as demo:
|
| 120 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 121 |
gr.Markdown("""
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
| 7 |
+
import re
|
| 8 |
|
| 9 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
| 10 |
from smolagents.models import OpenAIServerModel
|
| 11 |
|
|
|
|
| 12 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
| 13 |
+
Think step-by-step and finish your answer using the template:
|
| 14 |
+
FINAL ANSWER: [YOUR FINAL ANSWER]
|
| 15 |
+
|
| 16 |
+
Rules for FINAL ANSWER:
|
| 17 |
+
- A number: no commas, units, or extra words. Use plain digits only.
|
| 18 |
+
- A string: no articles or abbreviations. Use lowercase.
|
| 19 |
+
- A list: comma-separated values, formatted as above.
|
| 20 |
+
|
| 21 |
+
Only output the FINAL ANSWER line at the end. Do not explain the answer or repeat the question."""
|
| 22 |
|
| 23 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 24 |
|
|
|
|
| 25 |
class PatchedOpenAIServerModel(OpenAIServerModel):
|
| 26 |
def generate(self, messages, stop_sequences=None, **kwargs):
|
| 27 |
if isinstance(messages, list):
|
|
|
|
| 40 |
def __call__(self, question: str) -> str:
|
| 41 |
return self.agent.run(question)
|
| 42 |
|
| 43 |
+
def extract_final_answer(output: str) -> str:
|
| 44 |
+
if "FINAL ANSWER:" in output:
|
| 45 |
+
return output.split("FINAL ANSWER:")[-1].strip().rstrip('.')
|
| 46 |
+
return output.strip()
|
| 47 |
+
|
| 48 |
+
def sanitize_answer(ans: str) -> str:
|
| 49 |
+
ans = re.sub(r'\$|%|,', '', ans)
|
| 50 |
+
ans = ans.strip().rstrip('.')
|
| 51 |
+
return ans
|
| 52 |
+
|
| 53 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 54 |
space_id = os.getenv("SPACE_ID")
|
| 55 |
|
| 56 |
if profile:
|
| 57 |
+
username = profile.username.strip()
|
| 58 |
print(f"User logged in: {username}")
|
| 59 |
else:
|
| 60 |
print("User not logged in.")
|
|
|
|
| 93 |
if not task_id or question_text is None:
|
| 94 |
continue
|
| 95 |
try:
|
| 96 |
+
raw_output = agent(question_text)
|
| 97 |
+
extracted = extract_final_answer(raw_output)
|
| 98 |
+
submitted_answer = sanitize_answer(extracted)
|
| 99 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 100 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 101 |
except Exception as e:
|
|
|
|
| 105 |
if not answers_payload:
|
| 106 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 107 |
|
| 108 |
+
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
|
| 109 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 110 |
try:
|
| 111 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
|
| 131 |
except Exception as e:
|
| 132 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
| 133 |
|
|
|
|
| 134 |
with gr.Blocks() as demo:
|
| 135 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 136 |
gr.Markdown("""
|