dennyaw commited on
Commit
18999d8
1 Parent(s): 5e2182d

uploaded the py and requirements

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