File size: 1,251 Bytes
e270157
3a217fb
d1190ff
3a217fb
 
 
 
 
 
 
 
e270157
3a217fb
e270157
d1190ff
 
 
e270157
3a217fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32f2bf4
e270157
 
3a217fb
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
import gradio as gr
import torch
import spaces
from transformers import AutoModelForCausalLM, AutoTokenizer

REPO = "prathamkode/particle-1.0"
tok = AutoTokenizer.from_pretrained(REPO)
model = AutoModelForCausalLM.from_pretrained(
    REPO,
    torch_dtype=torch.float32,
    device_map="cpu",
)
model.eval()

@spaces.GPU
def _zero_gpu_placeholder():
    return None

def chat(message, history):
    messages = [{"role": m["role"], "content": m["content"]} for m in history]
    messages.append({"role": "user", "content": message})
    prompt = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    ids = tok(prompt, return_tensors="pt")
    ids.pop("token_type_ids", None)
    out = model.generate(
        **ids,
        max_new_tokens=64,
        do_sample=True,
        temperature=0.7,
        top_k=50,
        pad_token_id=tok.pad_token_id,
        eos_token_id=tok.eos_token_id,
    )
    text = tok.decode(out[0], skip_special_tokens=False)
    if "<|assistant|>" in text:
        text = text.split("<|assistant|>")[-1]
    return text.replace("<|endoftext|>", "").replace("<|padding|>", "").strip()


demo = gr.ChatInterface(chat, title="particle-1.0")

if __name__ == "__main__":
    demo.launch(ssr_mode=False)