File size: 2,001 Bytes
765bd6d 38fc00b 765bd6d 38fc00b 765bd6d 38fc00b 765bd6d 38fc00b 765bd6d 38fc00b 765bd6d 38fc00b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | from typing import Dict, List, Any
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
class EndpointHandler:
def __init__(self, path=""):
# 1. This runs only ONCE when the Endpoint boots up
print("Loading Ormuri AI into the Cloud GPU...")
self.tokenizer = AutoTokenizer.from_pretrained(path)
# We load in float16 to save memory and make it run much faster
self.model = AutoModelForCausalLM.from_pretrained(
path,
device_map="auto",
torch_dtype=torch.float16
)
print("Model Ready!")
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
# Get the raw message from the website
user_input = data.pop("inputs", "")
# 1. THE FIX: Format it EXACTLY like your university training data!
system_prompt = "You are a helpful, accurate, and friendly Ormuri language assistant. Only provide the exact translation. Do not invent words."
formatted_prompt = f"### Instruction:\n{system_prompt}\n\nUser Question: {user_input}\n\n### Response:\n"
# Convert text to numbers
input_ids = self.tokenizer(formatted_prompt, return_tensors="pt").input_ids.to(self.model.device)
# 2. THE FIX: Turn down the temperature so it stops hallucinating fake words!
output_ids = self.model.generate(
input_ids,
max_new_tokens=150,
pad_token_id=self.tokenizer.eos_token_id,
temperature=0.1, # Strictly factual, no guessing
do_sample=True
)
# Slice the output to ONLY grab the brand new words after "### Response:\n"
new_tokens = output_ids[0][input_ids.shape[1]:]
# Decode back to text
final_answer = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
# Send ONLY the clean answer back
return [{"generated_text": final_answer.strip()}] |