File size: 1,984 Bytes
bd5df33
0348877
bd5df33
c18814e
e44990b
0348877
c18814e
0348877
 
bd5df33
0348877
 
e44990b
0348877
 
 
 
 
 
 
 
 
 
 
e44990b
0348877
 
 
 
e44990b
0348877
e44990b
 
0348877
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import gradio as gr
from transformers import AutoModel, AutoTokenizer

# Load the model
model_name = "wop/kosmox-gguf"
model = AutoModel.from_pretrained(model_name)

# Assuming we need to load a corresponding tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Define the chat template
chat_template = "{{ bos_token }}{% for message in messages %}{% if message['from'] == 'human' %}{{' ' + message['value'] + ' '}}{% elif message['from'] == 'gpt' %}{{' ' + message['value'] + ' '}}{% else %}{{'<|' + message['from'] + '|> ' + message['value'] + ' '}}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ ' ' }}{% endif %}"

def chat(messages):
    # Prepare the input
    context = chat_template.format(
        bos_token=tokenizer.bos_token,
        messages=messages,
        add_generation_prompt=True
    )
    
    # Tokenize the input
    inputs = tokenizer(context, return_tensors='pt')
    
    # Generate response
    outputs = model.generate(**inputs)
    
    # Decode the response
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    return response

# Define the Gradio interface
with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    with gr.Row():
        with gr.Column():
            user_input = gr.Textbox(
                placeholder="Type your message here...",
                label="Your message"
            )
            send_button = gr.Button("Send")
        with gr.Column():
            chat_output = gr.Textbox(
                label="Chatbot response",
                interactive=False
            )

    def respond(message, history):
        history = history or []
        history.append({"from": "human", "value": message})
        
        response = chat(history)
        history.append({"from": "gpt", "value": response})
        
        return history, history[-1]['value']

    send_button.click(respond, [user_input, chatbot], [chatbot, chat_output])

# Launch the Gradio interface
demo.launch()