File size: 889 Bytes
d41f38e
93009f8
58d5252
93009f8
796991d
 
52642dd
58d5252
796991d
 
 
 
 
 
93009f8
d41f38e
 
58d5252
 
 
796991d
 
 
 
58d5252
d41f38e
 
796991d
 
 
 
 
 
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
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import gradio as gr
import torch

model_id = "tiiuae/falcon-rw-1b"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)
pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    device=0 if torch.cuda.is_available() else -1,
)

def chat(user_input, history):
    prompt = ""
    for user, bot in history:
        prompt += f"User: {user}\nBot: {bot}\n"
    prompt += f"User: {user_input}\nBot:"

    result = pipe(prompt, max_new_tokens=64, do_sample=False)
    output = result[0]["generated_text"]
    reply = output.split("Bot:")[-1].strip()
    history.append((user_input, reply))
    return history, history

gr.ChatInterface(
    fn=chat,
    chatbot=gr.Chatbot(),
    title="Tiny Falcon Chatbot",
    theme="default",
).launch()