dennyaw commited on
Commit
ff68b4a
1 Parent(s): 83b29d9

create the file

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ model_name="fadliaulawi/polylm-1.7b-finetuned"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name)
8
+
9
+
10
+ def user(message, history):
11
+ return "", history + [[message, None]]
12
+
13
+
14
+ def bot(history,temperature, max_length, top_p,top_k):
15
+ user_message = history[-1][0]
16
+ new_user_input_ids = tokenizer.encode(
17
+ user_message + tokenizer.eos_token, return_tensors="pt"
18
+ )
19
+
20
+ # append the new user input tokens to the chat history
21
+ bot_input_ids = torch.cat([torch.LongTensor([]), new_user_input_ids], dim=-1)
22
+
23
+ # generate a response
24
+ response = model.generate(
25
+ bot_input_ids,
26
+ pad_token_id=tokenizer.eos_token_id,
27
+ temperature = float(temperature),
28
+ max_length=max_length,
29
+ top_p=float(top_p),
30
+ top_k=top_k,
31
+ do_sample=True
32
+ ).tolist()
33
+
34
+ # convert the tokens to text, and then split the responses into lines
35
+ response = tokenizer.decode(response[0]).split("<|endoftext|>")
36
+ response = [
37
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
38
+ ] # convert to tuples of list
39
+ history[-1] = response[0]
40
+ return history
41
+
42
+
43
+ with gr.Blocks() as demo:
44
+ temperature = gr.Slider(0, 5, value=0.8, step=0.1, label='Temperature')
45
+ max_length = gr.Slider(0, 8192, value=256, step=1, label='Max Length')
46
+ top_p = gr.Slider(0, 1, value=0.8, step=0.1, label='Top P')
47
+ top_k = gr.Slider(0, 50, value=50, step=1, label='Top K')
48
+
49
+ chatbot = gr.Chatbot()
50
+ msg = gr.Textbox()
51
+ submit = gr.Button("Submit")
52
+ clear = gr.Button("Clear")
53
+
54
+ examples = gr.Examples(examples=["Hi Doctor"],inputs=[msg])
55
+
56
+ #submit.click(bot,[msg,chatbot,temperature, max_length, top_p,top_k],chatbot)
57
+ submit.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
58
+ bot, [chatbot,temperature,max_length,top_p,top_k], chatbot
59
+ )
60
+ clear.click(lambda: None, None, chatbot, queue=False)
61
+
62
+ demo.launch()