Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import pandas as pd
|
|
| 9 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
| 10 |
from smolagents.models import OpenAIServerModel
|
| 11 |
|
| 12 |
-
# System prompt
|
| 13 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
| 14 |
Report your thoughts, and finish your answer with the following template:
|
| 15 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
|
@@ -18,13 +18,11 @@ of numbers and/or strings. If you are asked for a number, don't use comma to wri
|
|
| 18 |
|
| 19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 20 |
|
| 21 |
-
# Extract FINAL ANSWER
|
| 22 |
-
|
| 23 |
def extract_final_answer(response: str) -> str:
|
|
|
|
| 24 |
match = re.search(r"FINAL ANSWER:\s*(.*)", response, re.IGNORECASE)
|
| 25 |
return match.group(1).strip() if match else response.strip()
|
| 26 |
|
| 27 |
-
# MyAgent class
|
| 28 |
class MyAgent:
|
| 29 |
def __init__(self):
|
| 30 |
self.model = OpenAIServerModel(model_id="gpt-4")
|
|
@@ -39,17 +37,17 @@ class MyAgent:
|
|
| 39 |
{"role": "user", "content": question}
|
| 40 |
]
|
| 41 |
try:
|
| 42 |
-
|
|
|
|
| 43 |
return extract_final_answer(response)
|
| 44 |
except Exception as e:
|
| 45 |
import traceback
|
| 46 |
traceback.print_exc()
|
| 47 |
return f"AGENT ERROR: {e}"
|
| 48 |
|
| 49 |
-
# Evaluation and submission
|
| 50 |
-
|
| 51 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 52 |
space_id = os.getenv("SPACE_ID")
|
|
|
|
| 53 |
if profile:
|
| 54 |
username = profile.username
|
| 55 |
print(f"User logged in: {username}")
|
|
@@ -79,6 +77,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 79 |
|
| 80 |
results_log = []
|
| 81 |
answers_payload = []
|
|
|
|
| 82 |
for item in questions_data:
|
| 83 |
task_id = item.get("task_id")
|
| 84 |
question_text = item.get("question")
|
|
@@ -121,20 +120,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 121 |
except Exception as e:
|
| 122 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
| 123 |
|
| 124 |
-
# Gradio UI
|
| 125 |
with gr.Blocks() as demo:
|
| 126 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 127 |
-
gr.Markdown(
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
gr.LoginButton()
|
| 137 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 138 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 139 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 140 |
|
|
@@ -164,4 +165,3 @@ if __name__ == "__main__":
|
|
| 164 |
demo.launch(debug=True, share=False)
|
| 165 |
|
| 166 |
|
| 167 |
-
|
|
|
|
| 9 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
| 10 |
from smolagents.models import OpenAIServerModel
|
| 11 |
|
| 12 |
+
# System prompt for GAIA evaluation
|
| 13 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
| 14 |
Report your thoughts, and finish your answer with the following template:
|
| 15 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
|
|
|
| 18 |
|
| 19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 20 |
|
|
|
|
|
|
|
| 21 |
def extract_final_answer(response: str) -> str:
|
| 22 |
+
"""Extract the FINAL ANSWER: portion from the model's response."""
|
| 23 |
match = re.search(r"FINAL ANSWER:\s*(.*)", response, re.IGNORECASE)
|
| 24 |
return match.group(1).strip() if match else response.strip()
|
| 25 |
|
|
|
|
| 26 |
class MyAgent:
|
| 27 |
def __init__(self):
|
| 28 |
self.model = OpenAIServerModel(model_id="gpt-4")
|
|
|
|
| 37 |
{"role": "user", "content": question}
|
| 38 |
]
|
| 39 |
try:
|
| 40 |
+
# Correct usage: model is callable, pass messages directly
|
| 41 |
+
response = self.model(messages)
|
| 42 |
return extract_final_answer(response)
|
| 43 |
except Exception as e:
|
| 44 |
import traceback
|
| 45 |
traceback.print_exc()
|
| 46 |
return f"AGENT ERROR: {e}"
|
| 47 |
|
|
|
|
|
|
|
| 48 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 49 |
space_id = os.getenv("SPACE_ID")
|
| 50 |
+
|
| 51 |
if profile:
|
| 52 |
username = profile.username
|
| 53 |
print(f"User logged in: {username}")
|
|
|
|
| 77 |
|
| 78 |
results_log = []
|
| 79 |
answers_payload = []
|
| 80 |
+
|
| 81 |
for item in questions_data:
|
| 82 |
task_id = item.get("task_id")
|
| 83 |
question_text = item.get("question")
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
| 122 |
|
|
|
|
| 123 |
with gr.Blocks() as demo:
|
| 124 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 125 |
+
gr.Markdown(
|
| 126 |
+
"""
|
| 127 |
+
**Instructions:**
|
| 128 |
+
1. Clone this space, modify code to define your agent's logic, tools, and packages.
|
| 129 |
+
2. Log in to your Hugging Face account using the button below.
|
| 130 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see your score.
|
| 131 |
|
| 132 |
+
**Note:** Submitting can take some time.
|
| 133 |
+
"""
|
| 134 |
+
)
|
| 135 |
|
| 136 |
gr.LoginButton()
|
| 137 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 138 |
+
|
| 139 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 140 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 141 |
|
|
|
|
| 165 |
demo.launch(debug=True, share=False)
|
| 166 |
|
| 167 |
|
|
|