|
|
import spaces |
|
|
import torch |
|
|
import numpy as np |
|
|
from typing import Generator |
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
from config import MODEL_NAME, MAX_NEW_TOKENS, TEMPERATURE, DO_SAMPLE |
|
|
|
|
|
|
|
|
tokenizer = None |
|
|
model = None |
|
|
|
|
|
def initialize_model(): |
|
|
"""Initializes and loads the model and tokenizer once onto the GPU.""" |
|
|
global tokenizer, model |
|
|
if model is None: |
|
|
try: |
|
|
print(f"Loading model {MODEL_NAME}...") |
|
|
|
|
|
|
|
|
dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32 |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) |
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
|
MODEL_NAME, |
|
|
torch_dtype=dtype, |
|
|
device_map="auto" |
|
|
) |
|
|
model.eval() |
|
|
|
|
|
|
|
|
if tokenizer.pad_token_id is None: |
|
|
tokenizer.pad_token_id = tokenizer.eos_token_id |
|
|
|
|
|
print("Model loaded successfully.") |
|
|
except Exception as e: |
|
|
print(f"Failed to load model: {e}") |
|
|
raise |
|
|
return tokenizer, model |
|
|
|
|
|
|
|
|
try: |
|
|
initialize_model() |
|
|
except Exception as e: |
|
|
print(f"Warning: Global model initialization failed: {e}") |
|
|
|
|
|
@spaces.GPU(duration=120) |
|
|
def stream_generate_response(prompt: str, history: list) -> Generator[str, None, None]: |
|
|
""" |
|
|
Generates a response from the KAT model with proper streaming. |
|
|
""" |
|
|
global tokenizer, model |
|
|
|
|
|
|
|
|
if model is None or tokenizer is None: |
|
|
initialize_model() |
|
|
|
|
|
|
|
|
messages = [] |
|
|
for human, bot in history: |
|
|
if human: |
|
|
messages.append({"role": "user", "content": human}) |
|
|
if bot: |
|
|
messages.append({"role": "assistant", "content": bot}) |
|
|
|
|
|
|
|
|
messages.append({"role": "user", "content": prompt}) |
|
|
|
|
|
|
|
|
text = tokenizer.apply_chat_template( |
|
|
messages, |
|
|
tokenize=False, |
|
|
add_generation_prompt=True, |
|
|
) |
|
|
|
|
|
|
|
|
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True) |
|
|
input_ids = inputs.input_ids.to(model.device) |
|
|
attention_mask = inputs.attention_mask.to(model.device) |
|
|
|
|
|
|
|
|
initial_length = input_ids.shape[-1] |
|
|
|
|
|
|
|
|
accumulated_text = "" |
|
|
generated_tokens = 0 |
|
|
|
|
|
|
|
|
while generated_tokens < MAX_NEW_TOKENS: |
|
|
with torch.no_grad(): |
|
|
outputs = model( |
|
|
input_ids=input_ids, |
|
|
attention_mask=attention_mask, |
|
|
return_dict=True |
|
|
) |
|
|
|
|
|
|
|
|
next_token_logits = outputs.logits[:, -1, :] |
|
|
|
|
|
|
|
|
if TEMPERATURE > 0: |
|
|
next_token_logits = next_token_logits / TEMPERATURE |
|
|
|
|
|
|
|
|
probs = torch.softmax(next_token_logits, dim=-1) |
|
|
if DO_SAMPLE: |
|
|
next_token = torch.multinomial(probs, num_samples=1) |
|
|
else: |
|
|
next_token = torch.argmax(next_token_logits, dim=-1, keepdim=True) |
|
|
|
|
|
|
|
|
if next_token.item() == tokenizer.eos_token_id: |
|
|
break |
|
|
|
|
|
|
|
|
new_token_text = tokenizer.decode(next_token[0], skip_special_tokens=True) |
|
|
|
|
|
|
|
|
accumulated_text += new_token_text |
|
|
|
|
|
|
|
|
yield accumulated_text |
|
|
|
|
|
|
|
|
input_ids = torch.cat([input_ids, next_token], dim=-1) |
|
|
attention_mask = torch.cat([attention_mask, torch.ones_like(next_token)], dim=-1) |
|
|
|
|
|
|
|
|
generated_tokens += 1 |
|
|
|
|
|
|
|
|
if accumulated_text: |
|
|
yield accumulated_text.strip() |