Update app.py
Browse files
app.py
CHANGED
|
@@ -3,25 +3,49 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
class
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
-
Fetches all questions, runs the
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
@@ -40,7 +64,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
@@ -142,7 +166,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 142 |
|
| 143 |
# --- Build Gradio Interface using Blocks ---
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
-
gr.Markdown("#
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
|
@@ -190,5 +214,5 @@ if __name__ == "__main__":
|
|
| 190 |
|
| 191 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 192 |
|
| 193 |
-
print("Launching Gradio Interface for
|
| 194 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from transformers import pipeline # Added import [[1]]
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
+
# --- Enhanced Agent Definition ---
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
+
class EnhancedAgent:
|
| 15 |
def __init__(self):
|
| 16 |
+
print("EnhancedAgent initialized with QA pipeline")
|
| 17 |
+
self.qa_pipeline = pipeline(
|
| 18 |
+
"question-answering",
|
| 19 |
+
model="deepset/roberta-base-squad2", # Improved QA model [[1]]
|
| 20 |
+
tokenizer="deepset/roberta-base-squad2"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
def __call__(self, question: str) -> str:
|
| 24 |
+
print(f"Processing question: {question[:50]}...")
|
| 25 |
+
try:
|
| 26 |
+
# Handle yes/no questions explicitly
|
| 27 |
+
if "yes or no" in question.lower():
|
| 28 |
+
result = self.qa_pipeline(question)
|
| 29 |
+
answer = result[0]['answer'] if result else "No answer"
|
| 30 |
+
return "Yes" if "yes" in answer.lower() else "No"
|
| 31 |
+
|
| 32 |
+
# General QA processing
|
| 33 |
+
result = self.qa_pipeline(question)
|
| 34 |
+
answer = result[0]['answer'] if result else "No answer found"
|
| 35 |
+
|
| 36 |
+
# Post-processing for common GAIA patterns
|
| 37 |
+
if answer.startswith("The answer is"):
|
| 38 |
+
return answer.split("The answer is")[-1].strip()
|
| 39 |
+
|
| 40 |
+
return answer[:500] # Limit length to avoid overflow
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Error processing question: {e}")
|
| 44 |
+
return f"Error: {str(e)}"
|
| 45 |
|
| 46 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 47 |
"""
|
| 48 |
+
Fetches all questions, runs the EnhancedAgent on them, submits all answers,
|
| 49 |
and displays the results.
|
| 50 |
"""
|
| 51 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
|
|
| 64 |
|
| 65 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 66 |
try:
|
| 67 |
+
agent = EnhancedAgent() # Changed to new agent class
|
| 68 |
except Exception as e:
|
| 69 |
print(f"Error instantiating agent: {e}")
|
| 70 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 166 |
|
| 167 |
# --- Build Gradio Interface using Blocks ---
|
| 168 |
with gr.Blocks() as demo:
|
| 169 |
+
gr.Markdown("# Enhanced GAIA Agent Evaluation Runner")
|
| 170 |
gr.Markdown(
|
| 171 |
"""
|
| 172 |
**Instructions:**
|
|
|
|
| 214 |
|
| 215 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 216 |
|
| 217 |
+
print("Launching Gradio Interface for Enhanced GAIA Agent Evaluation...")
|
| 218 |
demo.launch(debug=True, share=False)
|