Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,23 +13,36 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
def __call__(self, question: str) -> str:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 35 |
"""
|
|
|
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
+
token = os.getenv("HF_TOKEN")
|
| 17 |
+
if not token:
|
| 18 |
+
raise ValueError("HF_TOKEN secret missing in Space settings")
|
| 19 |
+
|
| 20 |
+
# load your **ONLY** model
|
| 21 |
+
self.llm = HfApiModel(
|
| 22 |
+
token=token,
|
| 23 |
+
model="microsoft/phi-2", # <— change this line for any other model
|
| 24 |
+
stop_sequences=["ANSWER:"], # stop as soon as it produces an answer
|
| 25 |
+
)
|
| 26 |
|
| 27 |
+
# single system prompt once
|
| 28 |
+
self.system = (
|
| 29 |
+
"You are a helpful assistant. "
|
| 30 |
+
"Answer each question in ONE line that starts with “ANSWER: ” "
|
| 31 |
+
"and nothing else."
|
| 32 |
)
|
| 33 |
|
| 34 |
def __call__(self, question: str) -> str:
|
| 35 |
+
messages = [
|
| 36 |
+
{"role": "system", "content": self.system},
|
| 37 |
+
{"role": "user", "content": question},
|
| 38 |
+
]
|
| 39 |
+
reply = self.llm(messages) # one HTTP request
|
| 40 |
+
text = reply["content"].strip()
|
| 41 |
+
# ensure we only return the line after “ANSWER:”
|
| 42 |
+
if "ANSWER" in text:
|
| 43 |
+
text = text.split("ANSWER", 1)[-1].lstrip(": ").splitlines()[0]
|
| 44 |
+
text = f"ANSWER: {text}"
|
| 45 |
+
return text
|
| 46 |
|
| 47 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 48 |
"""
|