aboutwaleed commited on
Commit
38fc00b
·
verified ·
1 Parent(s): c89d6b1

Update handler.py

Browse files
Files changed (1) hide show
  1. 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
- # 2. This runs every time your website sends a new message
21
-
22
- # Extract the user's message from the incoming API request
23
  user_input = data.pop("inputs", "")
24
 
25
- # Convert text to numbers for the AI
26
- input_ids = self.tokenizer(user_input, return_tensors="pt").input_ids.to(self.model.device)
 
 
 
 
27
 
28
- # Generate the response!
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
- # Convert the numbers back into Ormuri text
36
- full_response = self.tokenizer.decode(output_ids[0], skip_special_tokens=True)
 
 
 
37
 
38
- # Send it back to your website exactly how Hugging Face expects it
39
- return [{"generated_text": full_response}]
 
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()}]