Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
"""
|
| 2 |
app.py
|
| 3 |
This script provides the Gradio web interface to run the evaluation.
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
has been moved into the agent's 'multimodal_router'.
|
| 7 |
"""
|
| 8 |
|
| 9 |
import os
|
|
@@ -18,7 +17,7 @@ from agent import create_agent_executor
|
|
| 18 |
# --- Constants ---
|
| 19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 20 |
|
| 21 |
-
# --- Helper function to parse the agent's output
|
| 22 |
def parse_final_answer(agent_response: str) -> str:
|
| 23 |
match = re.search(r"FINAL ANSWER:\s*(.*)", agent_response, re.IGNORECASE | re.DOTALL)
|
| 24 |
if match: return match.group(1).strip()
|
|
@@ -26,13 +25,6 @@ def parse_final_answer(agent_response: str) -> str:
|
|
| 26 |
if lines: return lines[-1].strip()
|
| 27 |
return "Could not parse a final answer."
|
| 28 |
|
| 29 |
-
## MODIFICATION: The `detect_file_type` function has been removed.
|
| 30 |
-
## It is now redundant as this logic is handled inside the agent.
|
| 31 |
-
|
| 32 |
-
## MODIFICATION: The `create_enhanced_prompt` function has been removed.
|
| 33 |
-
## It was causing errors by trying to instruct the agent to use tools that no longer exist.
|
| 34 |
-
## The agent is now responsible for handling the raw input itself.
|
| 35 |
-
|
| 36 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 37 |
"""
|
| 38 |
Fetches all questions, runs the agent on them, submits all answers,
|
|
@@ -80,8 +72,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 80 |
|
| 81 |
file_url = item.get("file_url")
|
| 82 |
|
| 83 |
-
|
| 84 |
-
# We just combine the question and the URL into one string.
|
| 85 |
# The agent's multimodal_router will handle the rest.
|
| 86 |
if file_url:
|
| 87 |
full_question_text = f"{question_text}\n\nHere is the relevant file: {file_url}"
|
|
@@ -92,9 +83,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 92 |
print(f"Raw Prompt for Agent:\n{full_question_text}")
|
| 93 |
|
| 94 |
try:
|
| 95 |
-
# Pass the simple, raw question to the agent
|
| 96 |
result = agent_executor.invoke({"messages": [("user", full_question_text)]})
|
| 97 |
-
|
| 98 |
raw_answer = result['messages'][-1].content
|
| 99 |
submitted_answer = parse_final_answer(raw_answer)
|
| 100 |
|
|
@@ -102,6 +91,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 102 |
print(f"PARSED FINAL ANSWER: '{submitted_answer}'")
|
| 103 |
|
| 104 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
|
|
|
| 105 |
results_log.append({
|
| 106 |
"Task ID": task_id,
|
| 107 |
"Question": question_text,
|
|
@@ -123,7 +113,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 123 |
if not answers_payload:
|
| 124 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 125 |
|
| 126 |
-
# 4. Prepare and 5. Submit
|
| 127 |
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
|
| 128 |
print(f"\nSubmitting {len(answers_payload)} answers for user '{username}'...")
|
| 129 |
try:
|
|
@@ -139,30 +129,30 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 139 |
print(status_message)
|
| 140 |
return status_message, pd.DataFrame(results_log)
|
| 141 |
|
| 142 |
-
# --- Gradio UI
|
| 143 |
with gr.Blocks(title="Multimodal Agent Evaluation") as demo:
|
| 144 |
gr.Markdown("# Multimodal Agent Evaluation Runner")
|
| 145 |
gr.Markdown("This agent can process images, YouTube videos, audio files, and perform web searches.")
|
| 146 |
|
| 147 |
-
|
|
|
|
|
|
|
| 148 |
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 149 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=6, interactive=False)
|
| 150 |
results_table = gr.DataFrame(
|
| 151 |
label="Questions and Agent Answers",
|
| 152 |
wrap=True,
|
| 153 |
row_count=10,
|
| 154 |
-
|
| 155 |
-
column_widths=[80, 250, 200, 250]
|
| 156 |
)
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
run_button.click(fn=display_wrapper, outputs=[status_output, results_table])
|
| 166 |
|
| 167 |
if __name__ == "__main__":
|
| 168 |
print("\n" + "-"*30 + " Multimodal App Starting " + "-"*30)
|
|
|
|
| 1 |
"""
|
| 2 |
app.py
|
| 3 |
This script provides the Gradio web interface to run the evaluation.
|
| 4 |
+
This version is simplified to work with the new agent architecture and has
|
| 5 |
+
the correct Gradio wiring for the Hugging Face login functionality.
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
|
|
|
| 17 |
# --- Constants ---
|
| 18 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 19 |
|
| 20 |
+
# --- Helper function to parse the agent's output ---
|
| 21 |
def parse_final_answer(agent_response: str) -> str:
|
| 22 |
match = re.search(r"FINAL ANSWER:\s*(.*)", agent_response, re.IGNORECASE | re.DOTALL)
|
| 23 |
if match: return match.group(1).strip()
|
|
|
|
| 25 |
if lines: return lines[-1].strip()
|
| 26 |
return "Could not parse a final answer."
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 29 |
"""
|
| 30 |
Fetches all questions, runs the agent on them, submits all answers,
|
|
|
|
| 72 |
|
| 73 |
file_url = item.get("file_url")
|
| 74 |
|
| 75 |
+
# We simply combine the question and the URL into one string.
|
|
|
|
| 76 |
# The agent's multimodal_router will handle the rest.
|
| 77 |
if file_url:
|
| 78 |
full_question_text = f"{question_text}\n\nHere is the relevant file: {file_url}"
|
|
|
|
| 83 |
print(f"Raw Prompt for Agent:\n{full_question_text}")
|
| 84 |
|
| 85 |
try:
|
|
|
|
| 86 |
result = agent_executor.invoke({"messages": [("user", full_question_text)]})
|
|
|
|
| 87 |
raw_answer = result['messages'][-1].content
|
| 88 |
submitted_answer = parse_final_answer(raw_answer)
|
| 89 |
|
|
|
|
| 91 |
print(f"PARSED FINAL ANSWER: '{submitted_answer}'")
|
| 92 |
|
| 93 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 94 |
+
# The log for the DataFrame no longer includes a 'File Type' column
|
| 95 |
results_log.append({
|
| 96 |
"Task ID": task_id,
|
| 97 |
"Question": question_text,
|
|
|
|
| 113 |
if not answers_payload:
|
| 114 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 115 |
|
| 116 |
+
# 4. Prepare and 5. Submit
|
| 117 |
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
|
| 118 |
print(f"\nSubmitting {len(answers_payload)} answers for user '{username}'...")
|
| 119 |
try:
|
|
|
|
| 129 |
print(status_message)
|
| 130 |
return status_message, pd.DataFrame(results_log)
|
| 131 |
|
| 132 |
+
# --- Gradio UI ---
|
| 133 |
with gr.Blocks(title="Multimodal Agent Evaluation") as demo:
|
| 134 |
gr.Markdown("# Multimodal Agent Evaluation Runner")
|
| 135 |
gr.Markdown("This agent can process images, YouTube videos, audio files, and perform web searches.")
|
| 136 |
|
| 137 |
+
## MODIFICATION: Assign the LoginButton to a variable so we can use it as an input.
|
| 138 |
+
login_button = gr.LoginButton()
|
| 139 |
+
|
| 140 |
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 141 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=6, interactive=False)
|
| 142 |
results_table = gr.DataFrame(
|
| 143 |
label="Questions and Agent Answers",
|
| 144 |
wrap=True,
|
| 145 |
row_count=10,
|
| 146 |
+
column_widths=[80, 250, 200, 250]
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
+
## MODIFICATION: Wire the login_button as an input to the click event.
|
| 150 |
+
# This correctly passes the OAuth profile to the run_and_submit_all function.
|
| 151 |
+
run_button.click(
|
| 152 |
+
fn=run_and_submit_all,
|
| 153 |
+
inputs=login_button,
|
| 154 |
+
outputs=[status_output, results_table]
|
| 155 |
+
)
|
|
|
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
print("\n" + "-"*30 + " Multimodal App Starting " + "-"*30)
|