Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,23 +12,34 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
from transformers import pipeline
|
| 14 |
|
|
|
|
|
|
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
print("BasicAgent initialized.")
|
| 18 |
self.generator = pipeline(
|
| 19 |
"text2text-generation",
|
| 20 |
model="google/flan-t5-base",
|
| 21 |
-
max_new_tokens=
|
| 22 |
)
|
| 23 |
|
| 24 |
def __call__(self, question: str) -> str:
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
result = self.generator(prompt)[0]["generated_text"]
|
|
|
|
|
|
|
| 28 |
answer = result.strip()
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
return answer
|
| 31 |
|
|
|
|
| 32 |
|
| 33 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 34 |
"""
|
|
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
from transformers import pipeline
|
| 14 |
|
| 15 |
+
import re
|
| 16 |
+
|
| 17 |
class BasicAgent:
|
| 18 |
def __init__(self):
|
| 19 |
print("BasicAgent initialized.")
|
| 20 |
self.generator = pipeline(
|
| 21 |
"text2text-generation",
|
| 22 |
model="google/flan-t5-base",
|
| 23 |
+
max_new_tokens=32
|
| 24 |
)
|
| 25 |
|
| 26 |
def __call__(self, question: str) -> str:
|
| 27 |
+
prompt = (
|
| 28 |
+
"Answer the question with ONLY the final answer. "
|
| 29 |
+
"Do not add any extra words.\n"
|
| 30 |
+
f"Question: {question}"
|
| 31 |
+
)
|
| 32 |
result = self.generator(prompt)[0]["generated_text"]
|
| 33 |
+
|
| 34 |
+
# Post-process: keep only short answer
|
| 35 |
answer = result.strip()
|
| 36 |
+
answer = re.split(r"[.\n]", answer)[0] # cut after first sentence
|
| 37 |
+
answer = answer.strip()
|
| 38 |
+
|
| 39 |
+
print(f"Final answer: {answer}")
|
| 40 |
return answer
|
| 41 |
|
| 42 |
+
|
| 43 |
|
| 44 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 45 |
"""
|