File size: 1,160 Bytes
003c3f8
cde1762
 
003c3f8
cde1762
 
 
 
 
 
 
 
 
 
 
19fd497
cde1762
 
 
 
 
 
 
 
 
 
 
 
 
 
19fd497
cde1762
 
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
import gradio as gr
import torch
from transformers import pipeline

pipe = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta", torch_dtype=torch.bfloat16, device_map="auto")

messages = [
    {
        "role": "system",
        "content": "You are a friendly chatbot who always responds in the style of a pirate",
    },
    # {"role": "user", "content": "How many helicopters can a human eat in one sitting?"},
]


def chat_response(message, history):
    msg = messages.copy()
    for m in history:
        q, a = m
        msg.append({"role": "user", "content": q})
        msg.append({"role": "assistant", "content": a})
    msg.append({"role": "user", "content": message})
    prompt = pipe.tokenizer.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
    outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
    output = outputs[0]["generated_text"]
    messages.append({"role": "assistant", "content": output})
    response_start = output.rfind('<|assistant|>')
    return output[response_start + len('<|assistant|>'):]


demo = gr.ChatInterface(chat_response)

demo.launch()