Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -10,14 +11,39 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
class
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import transfomers
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
|
|
| 11 |
|
| 12 |
# --- Basic Agent Definition ---
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
+
class GeneralAgent:
|
| 15 |
def __init__(self):
|
| 16 |
+
print("Initializing BERT-based QA agent...")
|
| 17 |
+
# Cargar el modelo BERT preentrenado en SQuAD para tareas de Pregunta y Respuesta
|
| 18 |
+
self.qa_pipeline = pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad")
|
| 19 |
+
|
| 20 |
+
def __call__(self, question: str, context: str = None) -> str:
|
| 21 |
+
"""
|
| 22 |
+
Procesa la pregunta y devuelve una respuesta basada en el contexto proporcionado.
|
| 23 |
+
Si no se proporciona contexto, devuelve un mensaje de error.
|
| 24 |
+
"""
|
| 25 |
+
if context is None:
|
| 26 |
+
return "FINAL ANSWER: No context provided."
|
| 27 |
+
|
| 28 |
+
# Crear un prompt dentro del contexto que estructure la tarea más explícitamente
|
| 29 |
+
prompt = f"""
|
| 30 |
+
You are a general AI assistant. I will ask you a question based on the provided context.
|
| 31 |
+
Please provide the answer in a clear and concise manner.
|
| 32 |
+
Question: {question}
|
| 33 |
+
Context: {context}
|
| 34 |
+
Answer:
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
# Usar el pipeline para obtener la respuesta de la pregunta con el contexto
|
| 39 |
+
result = self.qa_pipeline(question=question, context=prompt)
|
| 40 |
+
answer = result["answer"]
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"Error durante QA: {e}")
|
| 43 |
+
answer = "Error processing question."
|
| 44 |
+
|
| 45 |
+
# Devuelve la respuesta final con el formato requerido
|
| 46 |
+
return f"FINAL ANSWER: {answer}"
|
| 47 |
|
| 48 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 49 |
"""
|