File size: 1,410 Bytes
87d9314
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import random
import torch
from utils import *


with gr.Blocks() as demo:

  with gr.Row():
    show_label = True
    gr.HTML(value=generate_html, show_label=show_label)

  with gr.Row():
    temp = gr.Slider(0, 1, value=0.2, label="Temperature", info="Choose between 0 and 1")
    seed = gr.Slider(0, 1000, value=42, label="Seed", info="Select Random Seed")
    max_tokens = gr.Slider(100, 1000, value=200, label="Max Tokens", info="Choose Max Tokens")

  with gr.Row():
    with gr.Column():
      chatbot = gr.Chatbot()
      msg = gr.Textbox(label='Message AI Assistant')
      clear = gr.ClearButton([msg, chatbot])

    def respond(message, chat_history, temp, seed, max_tokens):

        torch.manual_seed(seed)
        model_inputs = tokenizer(
                    [f"[INST] {message} [/INST]"], 
                    return_tensors="pt", padding=True)
        generated_ids = model.generate(**model_inputs, max_new_tokens=max_tokens)
        result = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
        bot_message = extract_responses(result[0])
        chat_history.append((message, bot_message))
    
        return "", chat_history

    msg.submit(respond, [msg, chatbot, temp, seed, max_tokens], [msg, chatbot])

  with gr.Row():
    show_label = True
    gr.HTML(value=generate_footer, show_label=show_label)

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