amritsolar commited on
Commit
417d000
Β·
1 Parent(s): 53e4086

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import logging
4
+ import gradio as gr
5
+ import openai
6
+
7
+ print(os.environ)
8
+ openai.api_base = os.environ.get("OPENAI_API_BASE")
9
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
10
+
11
+ BASE_SYSTEM_MESSAGE = """I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning.
12
+ I am an assistant who thinks through their answers step-by-step to be sure I always get the right answer.
13
+ I think more clearly if I write out my thought process in a scratchpad manner first; therefore, I always explain background context, assumptions, and step-by-step thinking BEFORE trying to answer or solve anything."""
14
+
15
+ def make_prediction(prompt, max_tokens=None, temperature=None, top_p=None, top_k=None, repetition_penalty=None):
16
+ completion = openai.Completion.create(model="Open-Orca/Mistral-7B-OpenOrca", prompt=prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty, stream=True, stop=["</s>", "<|im_end|>"])
17
+ for chunk in completion:
18
+ yield chunk["choices"][0]["text"]
19
+
20
+
21
+ def clear_chat(chat_history_state, chat_message):
22
+ chat_history_state = []
23
+ chat_message = ''
24
+ return chat_history_state, chat_message
25
+
26
+
27
+ def user(message, history):
28
+ history = history or []
29
+ # Append the user's message to the conversation history
30
+ history.append([message, ""])
31
+ return "", history
32
+
33
+
34
+ def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
35
+ history = history or []
36
+
37
+ if system_message.strip():
38
+ messages = "<|im_start|> "+"system\n" + system_message.strip() + "<|im_end|>\n" + \
39
+ "\n".join(["\n".join(["<|im_start|> "+"user\n"+item[0]+"<|im_end|>", "<|im_start|> assistant\n"+item[1]+"<|im_end|>"])
40
+ for item in history])
41
+ else:
42
+ messages = "<|im_start|> "+"system\n" + BASE_SYSTEM_MESSAGE + "<|im_end|>\n" + \
43
+ "\n".join(["\n".join(["<|im_start|> "+"user\n"+item[0]+"<|im_end|>", "<|im_start|> assistant\n"+item[1]+"<|im_end|>"])
44
+ for item in history])
45
+ # strip the last `<|end_of_turn|>` from the messages
46
+ messages = messages.rstrip("<|im_end|>")
47
+ # remove last space from assistant, some models output a ZWSP if you leave a space
48
+ messages = messages.rstrip()
49
+
50
+ # If temperature is set to 0, force Top P to 1 and Top K to -1
51
+ if temperature == 0:
52
+ top_p = 1
53
+ top_k = -1
54
+
55
+ prediction = make_prediction(
56
+ messages,
57
+ max_tokens=max_tokens,
58
+ temperature=temperature,
59
+ top_p=top_p,
60
+ top_k=top_k,
61
+ repetition_penalty=repetition_penalty,
62
+ )
63
+ for tokens in prediction:
64
+ tokens = re.findall(r'(.*?)(\s|$)', tokens)
65
+ for subtoken in tokens:
66
+ subtoken = "".join(subtoken)
67
+ answer = subtoken
68
+ history[-1][1] += answer
69
+ # stream the response
70
+ yield history, history, ""
71
+
72
+
73
+ start_message = ""
74
+
75
+ CSS ="""
76
+ .contain { display: flex; flex-direction: column; }
77
+ .gradio-container { height: 100vh !important; }
78
+ #component-0 { height: 100%; }
79
+ #chatbot { flex-grow: 1; overflow: auto; resize: vertical; }
80
+ """
81
+
82
+ #with gr.Blocks() as demo:
83
+ with gr.Blocks(css=CSS) as demo:
84
+ with gr.Row():
85
+ with gr.Column():
86
+ gr.Markdown(f"""
87
+ ## This demo is an unquantized GPU chatbot of [Mistral-7B-OpenOrca](https://huggingface.co/Open-Orca/Mistral-7B-OpenOrca)
88
+ Brought to you by your friends at Alignment Lab AI, OpenChat, and Open Access AI Collective!
89
+ """)
90
+ with gr.Row():
91
+ gr.Markdown("# πŸ‹ Mistral-7B-OpenOrca Playground Space! πŸ‹")
92
+ with gr.Row():
93
+ #chatbot = gr.Chatbot().style(height=500)
94
+ chatbot = gr.Chatbot(elem_id="chatbot")
95
+ with gr.Row():
96
+ message = gr.Textbox(
97
+ label="What do you want to chat about?",
98
+ placeholder="Ask me anything.",
99
+ lines=3,
100
+ )
101
+ with gr.Row():
102
+ submit = gr.Button(value="Send message", variant="secondary").style(full_width=True)
103
+ clear = gr.Button(value="New topic", variant="secondary").style(full_width=False)
104
+ stop = gr.Button(value="Stop", variant="secondary").style(full_width=False)
105
+ with gr.Accordion("Show Model Parameters", open=False):
106
+ with gr.Row():
107
+ with gr.Column():
108
+ max_tokens = gr.Slider(20, 2500, label="Max Tokens", step=20, value=500)
109
+ temperature = gr.Slider(0.0, 2.0, label="Temperature", step=0.1, value=0.4)
110
+ top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.95)
111
+ top_k = gr.Slider(1, 100, label="Top K", step=1, value=40)
112
+ repetition_penalty = gr.Slider(1.0, 2.0, label="Repetition Penalty", step=0.1, value=1.1)
113
+
114
+ system_msg = gr.Textbox(
115
+ start_message, label="System Message", interactive=True, visible=True, placeholder="System prompt. Provide instructions which you want the model to remember.", lines=5)
116
+
117
+ chat_history_state = gr.State()
118
+ clear.click(clear_chat, inputs=[chat_history_state, message], outputs=[chat_history_state, message], queue=False)
119
+ clear.click(lambda: None, None, chatbot, queue=False)
120
+
121
+ submit_click_event = submit.click(
122
+ fn=user, inputs=[message, chat_history_state], outputs=[message, chat_history_state], queue=True
123
+ ).then(
124
+ fn=chat, inputs=[chat_history_state, system_msg, max_tokens, temperature, top_p, top_k, repetition_penalty], outputs=[chatbot, chat_history_state, message], queue=True
125
+ )
126
+ stop.click(fn=None, inputs=None, outputs=None, cancels=[submit_click_event], queue=False)
127
+
128
+ demo.queue(max_size=128, concurrency_count=48).launch(debug=True, server_name="0.0.0.0", server_port=7860)