File size: 904 Bytes
fddd482
b29974e
a4b631b
 
b29974e
fddd482
b29974e
 
a4b631b
b29974e
 
 
26579f6
b29974e
888f559
b29974e
03cb4ae
b29974e
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import spaces
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr

checkpoint = "WillHeld/soft-raccoon"
device = "cuda"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)

@spaces.GPU(duration=120)
def predict(message, history):
    history.append({"role": "user", "content": message})
    input_text = tokenizer.apply_chat_template(history, tokenize=False) + "<|start_header_id|>assistant<|end_header_id|>\n\n"
    inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)  
    outputs = model.generate(inputs, max_new_tokens=1024, temperature=0.7, top_p=0.9, do_sample=True)
    decoded = tokenizer.decode(outputs[0])
    response = decoded.split("<|start_header_id|>assistant<|end_header_id|>\n\n")[-1]
    return response

demo = gr.ChatInterface(predict, type="messages")

demo.launch()