Base app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from ctransformers import AutoModelForCausalLM
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
def generate_prompt(history):
|
9 |
+
prompt = start_message
|
10 |
+
for chain in history[:-1]:
|
11 |
+
prompt += f"<|im_start|>user\n{chain[0]}<|im_end|>" +\
|
12 |
+
f"<|im_start|>assistant\n{chain[1]}<|im_end|>"
|
13 |
+
prompt += f"<|im_start|>user\n{history[-1][0]}<|im_end|>" +\
|
14 |
+
"<|im_start|>assistant\n"
|
15 |
+
return prompt
|
16 |
+
|
17 |
+
def generate(history):
|
18 |
+
prompt = generate_prompt(history)
|
19 |
+
|
20 |
+
streamer = llm(prompt, stop = stop_tokens, stream=True, threads=2)
|
21 |
+
return streamer
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
llm = AutoModelForCausalLM.from_pretrained("model/ggml-model-q8_0.bin", model_type='mpt')
|
26 |
+
stop_tokens = ["<|im_end|>", "<|endoftext|>"]
|
27 |
+
|
28 |
+
start_message = """<|im_start|>system
|
29 |
+
You are a helpful assistant chatbot.<|im_end|>
|
30 |
+
"""
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
chatbot = gr.Chatbot()
|
36 |
+
msg = gr.Textbox()
|
37 |
+
clear = gr.Button("Clear")
|
38 |
+
|
39 |
+
def user(user_message, history):
|
40 |
+
return "", history + [[user_message, ""]]
|
41 |
+
|
42 |
+
def bot(history):
|
43 |
+
streamer = generate(history)
|
44 |
+
|
45 |
+
for token in streamer:
|
46 |
+
history[-1][1] += token
|
47 |
+
yield history
|
48 |
+
|
49 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
50 |
+
bot, chatbot, chatbot
|
51 |
+
)
|
52 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
53 |
+
|
54 |
+
demo.queue()
|
55 |
+
if __name__ == "__main__":
|
56 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|