szzzzz commited on
Commit
1af67d0
·
1 Parent(s): 429403f

Create app.py

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 chatbot import Bot
3
+ model = Bot()
4
+ model.load("szzzzz/chatbot_bloom_560m",low_disk_usage=True)
5
+
6
+
7
+ def add_text(history, text):
8
+ history = history + [(text, None)]
9
+ return history, ""
10
+
11
+
12
+ def bot(history):
13
+ prompt = ""
14
+ for i, h in enumerate(history):
15
+ prompt = prompt + "\nHuman: " + h[0]
16
+ if i != len(history) - 1:
17
+ prompt = prompt + "\nAssistant: " + h[1]
18
+ else:
19
+ prompt = prompt + "\nAssistant: "
20
+
21
+ response = model.generate(prompt)
22
+ history[-1][1] = response
23
+ return history
24
+
25
+ def regenerate(history):
26
+ prompt = ""
27
+ for i, h in enumerate(history):
28
+ prompt = prompt + "\nHuman: " + h[0]
29
+ if i != len(history) - 1:
30
+ prompt = prompt + "\nAssistant: " + h[1]
31
+ else:
32
+ prompt = prompt + "\nAssistant: "
33
+
34
+ response = model.generate(prompt)
35
+ history[-1][1] = response
36
+ return history
37
+
38
+
39
+ with gr.Blocks() as demo:
40
+ gr.Markdown("""chatbot of szzzzz.""")
41
+
42
+ with gr.Tab("chatbot"):
43
+ gr_chatbot = gr.Chatbot([]).style(height=300)
44
+
45
+ txt = gr.Textbox(
46
+ show_label=False,
47
+ placeholder="Enter text and press enter",
48
+ ).style(container=False)
49
+ with gr.Row():
50
+ clear = gr.Button("Restart")
51
+ regen = gr.Button("Regenerate response")
52
+
53
+ # func
54
+ txt.submit(add_text, [gr_chatbot, txt], [gr_chatbot, txt]).then(
55
+ bot, gr_chatbot, gr_chatbot
56
+ )
57
+
58
+ clear.click(lambda: None, None, gr_chatbot, queue=False)
59
+ regen.click(regenerate, [gr_chatbot], [gr_chatbot])
60
+
61
+
62
+ demo.launch(server_name="0.0.0.0")