Update handler.py
Browse files- handler.py +18 -11
handler.py
CHANGED
|
@@ -17,23 +17,30 @@ class EndpointHandler:
|
|
| 17 |
print("Model Ready!")
|
| 18 |
|
| 19 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
# Extract the user's message from the incoming API request
|
| 23 |
user_input = data.pop("inputs", "")
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
output_ids = self.model.generate(
|
| 30 |
input_ids,
|
| 31 |
max_new_tokens=150,
|
| 32 |
-
pad_token_id=self.tokenizer.eos_token_id
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# Send
|
| 39 |
-
return [{"generated_text":
|
|
|
|
| 17 |
print("Model Ready!")
|
| 18 |
|
| 19 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 20 |
+
# Get the raw message from the website
|
|
|
|
|
|
|
| 21 |
user_input = data.pop("inputs", "")
|
| 22 |
|
| 23 |
+
# 1. THE FIX: Format it EXACTLY like your university training data!
|
| 24 |
+
system_prompt = "You are a helpful, accurate, and friendly Ormuri language assistant. Only provide the exact translation. Do not invent words."
|
| 25 |
+
formatted_prompt = f"### Instruction:\n{system_prompt}\n\nUser Question: {user_input}\n\n### Response:\n"
|
| 26 |
+
|
| 27 |
+
# Convert text to numbers
|
| 28 |
+
input_ids = self.tokenizer(formatted_prompt, return_tensors="pt").input_ids.to(self.model.device)
|
| 29 |
|
| 30 |
+
# 2. THE FIX: Turn down the temperature so it stops hallucinating fake words!
|
| 31 |
output_ids = self.model.generate(
|
| 32 |
input_ids,
|
| 33 |
max_new_tokens=150,
|
| 34 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 35 |
+
temperature=0.1, # Strictly factual, no guessing
|
| 36 |
+
do_sample=True
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Slice the output to ONLY grab the brand new words after "### Response:\n"
|
| 40 |
+
new_tokens = output_ids[0][input_ids.shape[1]:]
|
| 41 |
+
|
| 42 |
+
# Decode back to text
|
| 43 |
+
final_answer = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 44 |
|
| 45 |
+
# Send ONLY the clean answer back
|
| 46 |
+
return [{"generated_text": final_answer.strip()}]
|