Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,51 +22,49 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 22 |
class GeneralAgent:
|
| 23 |
def __init__(self):
|
| 24 |
print("Initializing GPT-2 based QA agent...")
|
| 25 |
-
# Cargar modelo y tokenizador de GPT-2
|
| 26 |
self.model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 27 |
self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 28 |
|
| 29 |
def __call__(self, question: str, context: str = None) -> str:
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
-
Usa un prompt específico para guiar la respuesta del modelo GPT-2.
|
| 33 |
"""
|
| 34 |
if context is None:
|
| 35 |
-
|
| 36 |
|
| 37 |
-
#
|
| 38 |
prompt = f"""
|
| 39 |
-
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
|
|
|
|
|
|
|
|
|
| 40 |
Question: {question}
|
| 41 |
Context: {context}
|
| 42 |
"""
|
| 43 |
|
| 44 |
-
# Tokenizar el prompt
|
| 45 |
inputs = self.tokenizer.encode(prompt, return_tensors="pt")
|
| 46 |
|
| 47 |
-
#
|
| 48 |
outputs = self.model.generate(inputs, max_length=500, num_return_sequences=1, no_repeat_ngram_size=2, early_stopping=True)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 52 |
|
| 53 |
-
#
|
| 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 |
-
|
| 61 |
"""
|
| 62 |
-
# Buscar la sección que comienza con "FINAL ANSWER:"
|
| 63 |
final_answer_start = "FINAL ANSWER:"
|
| 64 |
start_idx = answer.find(final_answer_start)
|
| 65 |
|
| 66 |
if start_idx == -1:
|
| 67 |
return "Error processing question."
|
| 68 |
|
| 69 |
-
# Extraer la respuesta que sigue a "FINAL ANSWER:"
|
| 70 |
final_answer = answer[start_idx + len(final_answer_start):].strip()
|
| 71 |
return final_answer.strip()
|
| 72 |
|
|
@@ -76,6 +74,7 @@ Context: {context}
|
|
| 76 |
|
| 77 |
|
| 78 |
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 22 |
class GeneralAgent:
|
| 23 |
def __init__(self):
|
| 24 |
print("Initializing GPT-2 based QA agent...")
|
|
|
|
| 25 |
self.model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 26 |
self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 27 |
|
| 28 |
def __call__(self, question: str, context: str = None) -> str:
|
| 29 |
"""
|
| 30 |
+
Process the question and generate an answer based on the provided context.
|
|
|
|
| 31 |
"""
|
| 32 |
if context is None:
|
| 33 |
+
context = "No context provided." # Default context if none is given
|
| 34 |
|
| 35 |
+
# Create a clear, structured prompt
|
| 36 |
prompt = f"""
|
| 37 |
+
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 38 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings. If you are asked for a number, don't use commas to write your number nor use units such as $ or percent sign unless specified otherwise.
|
| 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 |
inputs = self.tokenizer.encode(prompt, return_tensors="pt")
|
| 46 |
|
| 47 |
+
# Generate the answer with GPT-2
|
| 48 |
outputs = self.model.generate(inputs, max_length=500, num_return_sequences=1, no_repeat_ngram_size=2, early_stopping=True)
|
| 49 |
|
| 50 |
+
# Decode the generated answer
|
| 51 |
answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 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 |
final_answer = answer[start_idx + len(final_answer_start):].strip()
|
| 69 |
return final_answer.strip()
|
| 70 |
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
+
|
| 78 |
|
| 79 |
|
| 80 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|