Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,52 +21,37 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 21 |
# --- Basic Agent Definition ---
|
| 22 |
class GeneralAgent:
|
| 23 |
def __init__(self):
|
| 24 |
-
print("Initializing
|
| 25 |
-
|
| 26 |
-
self.
|
| 27 |
|
| 28 |
def __call__(self, question: str, context: str = None) -> str:
|
| 29 |
"""
|
| 30 |
-
|
|
|
|
| 31 |
"""
|
| 32 |
if context is None:
|
| 33 |
-
|
| 34 |
|
| 35 |
-
#
|
| 36 |
prompt = f"""
|
| 37 |
-
You are a general AI assistant. I will ask you a question
|
| 38 |
-
|
| 39 |
-
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 40 |
-
If you are asked for a comma-separated list, apply the above rules depending on whether the element to be put in the list is a number or a string.
|
| 41 |
Question: {question}
|
| 42 |
Context: {context}
|
|
|
|
| 43 |
"""
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Extract the relevant part of the answer
|
| 54 |
-
final_answer = self._extract_final_answer(answer)
|
| 55 |
-
|
| 56 |
-
return f"FINAL ANSWER: {final_answer}"
|
| 57 |
-
|
| 58 |
-
def _extract_final_answer(self, answer: str) -> str:
|
| 59 |
-
"""
|
| 60 |
-
Extract the relevant part of the generated answer.
|
| 61 |
-
"""
|
| 62 |
-
final_answer_start = "FINAL ANSWER:"
|
| 63 |
-
start_idx = answer.find(final_answer_start)
|
| 64 |
-
|
| 65 |
-
if start_idx == -1:
|
| 66 |
-
return "Error processing question."
|
| 67 |
|
| 68 |
-
|
| 69 |
-
return
|
| 70 |
|
| 71 |
|
| 72 |
|
|
|
|
| 21 |
# --- Basic Agent Definition ---
|
| 22 |
class GeneralAgent:
|
| 23 |
def __init__(self):
|
| 24 |
+
print("Initializing BERT-based QA agent...")
|
| 25 |
+
# Cargar el modelo BERT preentrenado en SQuAD para tareas de Pregunta y Respuesta
|
| 26 |
+
self.qa_pipeline = pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad")
|
| 27 |
|
| 28 |
def __call__(self, question: str, context: str = None) -> str:
|
| 29 |
"""
|
| 30 |
+
Procesa la pregunta y devuelve una respuesta basada en el contexto proporcionado.
|
| 31 |
+
Si no se proporciona contexto, devuelve un mensaje de error.
|
| 32 |
"""
|
| 33 |
if context is None:
|
| 34 |
+
return "FINAL ANSWER: No context provided."
|
| 35 |
|
| 36 |
+
# Crear un prompt dentro del contexto que estructure la tarea más explícitamente
|
| 37 |
prompt = f"""
|
| 38 |
+
You are a general AI assistant. I will ask you a question based on the provided context.
|
| 39 |
+
Please provide the answer in a clear and concise manner.
|
|
|
|
|
|
|
| 40 |
Question: {question}
|
| 41 |
Context: {context}
|
| 42 |
+
Answer:
|
| 43 |
"""
|
| 44 |
|
| 45 |
+
try:
|
| 46 |
+
# Usar el pipeline para obtener la respuesta de la pregunta con el contexto
|
| 47 |
+
result = self.qa_pipeline(question=question, context=prompt)
|
| 48 |
+
answer = result["answer"]
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error durante QA: {e}")
|
| 51 |
+
answer = "Error processing question."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Devuelve la respuesta final con el formato requerido
|
| 54 |
+
return f"FINAL ANSWER: {answer}"
|
| 55 |
|
| 56 |
|
| 57 |
|