zachlopez commited on
Commit
0f17027
1 Parent(s): 67a8171

Added parallel workaround

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -8,10 +8,13 @@ tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
8
  model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
9
  model.to(device)
10
 
11
- def get_reply(response, history = []):
 
 
12
  history.append(response)
13
  if response.endswith(("bye", "Bye", "bye.", "Bye.")):
14
- return "<div class='chatbot'>Chatbot restarted</div>", []
 
15
  if len(history) > 4: history = history[-4:]
16
  inputs = tokenizer(" ".join(history), return_tensors="pt")
17
  inputs.to(device)
@@ -24,8 +27,8 @@ def get_reply(response, history = []):
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
  css = """
31
  .chatbox {display:flex;flex-direction:column}
@@ -37,7 +40,8 @@ css = """
37
 
38
  gr.Interface(fn=get_reply,
39
  theme="default",
40
- inputs=[gr.inputs.Textbox(placeholder="How are you?"),
 
41
  "state"],
42
  outputs=["html", "state"],
43
- css=css).launch(debug=True, enable_queue=True, share=True)
 
8
  model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
9
  model.to(device)
10
 
11
+ def get_reply(response, username = None, histories = {}):
12
+ if username == None or username == "": return "<div class='chatbot'>Enter a username</div>", histories
13
+ history = histories.get(username, [])
14
  history.append(response)
15
  if response.endswith(("bye", "Bye", "bye.", "Bye.")):
16
+ history[username] = []
17
+ return "<div class='chatbot'>Chatbot restarted</div>", histories
18
  if len(history) > 4: history = history[-4:]
19
  inputs = tokenizer(" ".join(history), return_tensors="pt")
20
  inputs.to(device)
 
27
  cls = "user" if m%2 == 0 else "bot"
28
  html += "<div class='msg {}'> {}</div>".format(cls, msg)
29
  html += "</div>"
30
+ histories[username] = history
31
+ return html, histories
32
 
33
  css = """
34
  .chatbox {display:flex;flex-direction:column}
 
40
 
41
  gr.Interface(fn=get_reply,
42
  theme="default",
43
+ inputs=[gr.inputs.Textbox(placeholder="How are you?"),
44
+ gr.inputs.Textbox(label="Username"),
45
  "state"],
46
  outputs=["html", "state"],
47
+ css=css).launch(debug=True, enable_queue=True)