dawood HF staff commited on
Commit
6a49fbe
1 Parent(s): add606e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -26
app.py CHANGED
@@ -1,11 +1,15 @@
 
1
  from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import torch
3
 
4
  tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
5
  model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
6
 
 
7
  def predict(input, history=[]):
8
  # tokenize the new input sentence
 
 
9
  new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
10
 
11
  # append the new user input tokens to the chat history
@@ -16,30 +20,17 @@ def predict(input, history=[]):
16
 
17
  # convert the tokens to text, and then split the responses into lines
18
  response = tokenizer.decode(history[0]).split("<|endoftext|>")
19
- response.remove("")
20
-
21
- # write some HTML
22
- html = "<div class='chatbot'>"
23
- for m, msg in enumerate(response):
24
- cls = "user" if m%2 == 0 else "bot"
25
- html += "<div class='msg {}'> {}</div>".format(cls, msg)
26
- html += "</div>"
27
-
28
- return html, history
29
 
30
- import gradio as gr
31
-
32
- css = """
33
- .chatbox {display:flex;flex-direction:column}
34
- .msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
35
- .msg.user {background-color:cornflowerblue;color:white}
36
- .msg.bot {background-color:lightgray;align-self:self-end}
37
- .footer {display:none !important}
38
- """
39
-
40
- gr.Interface(fn=predict,
41
- theme="default",
42
- inputs=[gr.inputs.Textbox(placeholder="How are you?"), "state"],
43
- outputs=["html", "state"],
44
- css=css).launch()
45
-
 
1
+ import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
  tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
6
  model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
7
 
8
+
9
  def predict(input, history=[]):
10
  # tokenize the new input sentence
11
+ print(input)
12
+ print(tokenizer.eos_token)
13
  new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
14
 
15
  # append the new user input tokens to the chat history
 
20
 
21
  # convert the tokens to text, and then split the responses into lines
22
  response = tokenizer.decode(history[0]).split("<|endoftext|>")
23
+ response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
24
+ return response, history
 
 
 
 
 
 
 
 
25
 
26
+ with gr.Blocks() as demo:
27
+ chatbot = gr.Chatbot()
28
+ state = gr.State([])
29
+
30
+ with gr.Row():
31
+ txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
32
+
33
+ txt.submit(predict, [txt, state], [chatbot, state])
34
+
35
+ if __name__ == "__main__":
36
+ demo.launch()