Update model_utils.py
Browse files- model_utils.py +16 -4
model_utils.py
CHANGED
|
@@ -124,8 +124,15 @@ def generate_answer(question: str) -> str:
|
|
| 124 |
)
|
| 125 |
|
| 126 |
generated_ids = outputs[0][inputs["input_ids"].shape[1]:]
|
| 127 |
-
answer = tokenizer.decode(generated_ids, skip_special_tokens=True)
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
def answer_from_qa(question: str) -> Optional[str]:
|
|
@@ -172,11 +179,16 @@ def laos_history_bot(message: str, history: List) -> str:
|
|
| 172 |
|
| 173 |
direct = answer_from_qa(message)
|
| 174 |
if direct:
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
try:
|
| 178 |
answer = generate_answer(message)
|
| 179 |
except Exception as e: # noqa: BLE001
|
| 180 |
return f"ລະບົບມີບັນຫາ: {e}"
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
generated_ids = outputs[0][inputs["input_ids"].shape[1]:]
|
| 127 |
+
answer = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
|
| 128 |
+
|
| 129 |
+
# ✅ Enforce 2–3 short sentences
|
| 130 |
+
# `re` is already imported at the top of this file
|
| 131 |
+
sentences = re.split(r"(?<=[\.?!…])\s+", answer)
|
| 132 |
+
short_answer = " ".join(sentences[:3]).strip()
|
| 133 |
+
|
| 134 |
+
return short_answer if short_answer else answer
|
| 135 |
+
|
| 136 |
|
| 137 |
|
| 138 |
def answer_from_qa(question: str) -> Optional[str]:
|
|
|
|
| 179 |
|
| 180 |
direct = answer_from_qa(message)
|
| 181 |
if direct:
|
| 182 |
+
# later you can make this dynamic from the dataset
|
| 183 |
+
meta = "[ຊັ້ນ M1, ບົດ 1]"
|
| 184 |
+
return f"{meta} {direct}"
|
| 185 |
+
|
| 186 |
|
| 187 |
try:
|
| 188 |
answer = generate_answer(message)
|
| 189 |
except Exception as e: # noqa: BLE001
|
| 190 |
return f"ລະບົບມີບັນຫາ: {e}"
|
| 191 |
|
| 192 |
+
meta = "[ຊັ້ນ M1, ບົດ 1]" # placeholder, later make dynamic
|
| 193 |
+
return f"{meta} {answer}"
|
| 194 |
+
|