Update app.py
Browse files
app.py
CHANGED
@@ -1,287 +1,32 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
if not api_url:
|
8 |
-
raise ValueError("Please set the environment variable OPENCHAT_API_URL.")
|
9 |
-
|
10 |
-
openchat_preprompt = "\n<human>: Zdravo!\n<bot>: \n"
|
11 |
-
|
12 |
-
def get_client(model: str):
|
13 |
-
if model == "togethercomputer/GPT-NeoXT-Chat-Base-20B":
|
14 |
-
return Client(api_url)
|
15 |
-
return InferenceAPIClient(model, token=os.getenv("HF_TOKEN", None))
|
16 |
-
|
17 |
-
def get_usernames(model: str):
|
18 |
-
if model in ("OpenAssistant/oasst-sft-1-pythia-12b", "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"):
|
19 |
-
return "", "", "", ""
|
20 |
-
if model == "togethercomputer/GPT-NeoXT-Chat-Base-20B":
|
21 |
-
return openchat_preprompt, "<human>: ", "<bot>: ", "\n"
|
22 |
-
return "", "User: ", "Assistant: ", "\n"
|
23 |
-
|
24 |
-
def reset_textbox():
|
25 |
-
inputs.value = "" # Reset the value of the inputs textbox
|
26 |
-
|
27 |
-
def predict(model: str, inputs: str, typical_p: float, top_p: float, temperature: float, top_k: int, repetition_penalty: float, watermark: bool, chatbot, history):
|
28 |
-
client = get_client(model)
|
29 |
-
preprompt, user_name, assistant_name, sep = get_usernames(model)
|
30 |
-
|
31 |
-
if inputs.lower() == "write a 5-sentence essay on the problem of suicide":
|
32 |
-
inputs = "The problem of suicide is a grave concern in today's society. It is a complex issue that affects individuals from all walks of life. One of the key factors contributing to suicide is mental health problems such as depression and anxiety. Social isolation and lack of support systems can also exacerbate the problem. Furthermore, societal stigma surrounding mental health often prevents individuals from seeking help. Addressing the problem of suicide requires a multi-faceted approach, including improved access to mental health services, destigmatization efforts, and fostering supportive communities."
|
33 |
-
|
34 |
-
if inputs.lower() == "write a 5-sentence essay on the problem of pollution":
|
35 |
-
inputs = "Pollution is a pressing issue that poses significant threats to the environment and human health. It encompasses various forms such as air, water, and land pollution. Industrial activities, improper waste disposal, and excessive use of fossil fuels contribute to the problem. Pollution leads to adverse effects on ecosystems, including biodiversity loss and climate change. Moreover, it has detrimental effects on human health, increasing the risk of respiratory diseases and other health complications. Tackling pollution requires concerted efforts, including stricter regulations, adoption of sustainable practices, and public awareness campaigns."
|
36 |
-
|
37 |
-
history.append(inputs)
|
38 |
-
|
39 |
-
past = []
|
40 |
-
for data in chatbot:
|
41 |
-
user_data, model_data = data
|
42 |
-
|
43 |
-
if not user_data.startswith(user_name):
|
44 |
-
user_data = user_name + user_data
|
45 |
-
if not model_data.startswith(sep + assistant_name):
|
46 |
-
model_data = sep + assistant_name + model_data
|
47 |
|
48 |
-
|
49 |
|
50 |
-
|
51 |
-
|
|
|
52 |
|
53 |
-
|
|
|
54 |
|
55 |
-
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
truncate=1000,
|
62 |
-
watermark=watermark,
|
63 |
-
max_new_tokens=500,
|
64 |
-
)
|
65 |
-
else:
|
66 |
-
iterator = client.generate_stream(
|
67 |
-
total_inputs,
|
68 |
-
top_p=top_p if top_p < 1.0 else None,
|
69 |
-
top_k=top_k,
|
70 |
-
truncate=1000,
|
71 |
-
repetition_penalty=repetition_penalty,
|
72 |
-
watermark=watermark,
|
73 |
-
temperature=temperature,
|
74 |
-
max_new_tokens=500,
|
75 |
-
stop_sequences=[user_name.rstrip(), assistant_name.rstrip()],
|
76 |
-
)
|
77 |
|
78 |
-
|
79 |
-
if response.token.special:
|
80 |
-
continue
|
81 |
|
82 |
-
|
83 |
-
if partial_words.endswith(user_name.rstrip()):
|
84 |
-
partial_words = partial_words.rstrip(user_name.rstrip())
|
85 |
-
if partial_words.endswith(assistant_name.rstrip()):
|
86 |
-
partial_words = partial_words.rstrip(assistant_name.rstrip())
|
87 |
-
|
88 |
-
if i == 0:
|
89 |
-
history.append(" " + partial_words)
|
90 |
-
elif response.token.text not in user_name:
|
91 |
-
history[-1] = partial_words
|
92 |
-
|
93 |
-
chat = [
|
94 |
-
(history[i].strip(), history[i + 1].strip())
|
95 |
-
for i in range(0, len(history) - 1, 2)
|
96 |
-
]
|
97 |
-
yield chat, history
|
98 |
-
|
99 |
-
def radio_on_change(
|
100 |
-
value: str,
|
101 |
-
disclaimer,
|
102 |
-
typical_p,
|
103 |
-
top_p,
|
104 |
-
top_k,
|
105 |
-
temperature,
|
106 |
-
repetition_penalty,
|
107 |
-
watermark,
|
108 |
-
):
|
109 |
-
if value in ("OpenAssistant/oasst-sft-1-pythia-12b", "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"):
|
110 |
-
typical_p = typical_p.update(value=0.2, visible=True)
|
111 |
-
top_p = top_p.update(visible=False)
|
112 |
-
top_k = top_k.update(visible=False)
|
113 |
-
temperature = temperature.update(visible=False)
|
114 |
-
disclaimer = disclaimer.update(visible=False)
|
115 |
-
repetition_penalty = repetition_penalty.update(visible=False)
|
116 |
-
watermark = watermark.update(False)
|
117 |
-
elif value == "togethercomputer/GPT-NeoXT-Chat-Base-20B":
|
118 |
-
typical_p = typical_p.update(visible=False)
|
119 |
-
top_p = top_p.update(value=0.25, visible=True)
|
120 |
-
top_k = top_k.update(value=50, visible=True)
|
121 |
-
temperature = temperature.update(value=0.6, visible=True)
|
122 |
-
repetition_penalty = repetition_penalty.update(value=1.01, visible=True)
|
123 |
-
watermark = watermark.update(False)
|
124 |
-
disclaimer = disclaimer.update(visible=True)
|
125 |
-
else:
|
126 |
-
typical_p = typical_p.update(visible=False)
|
127 |
-
top_p = top_p.update(value=0.95, visible=True)
|
128 |
-
top_k = top_k.update(value=4, visible=True)
|
129 |
-
temperature = temperature.update(value=0.5, visible=True)
|
130 |
-
repetition_penalty = repetition_penalty.update(value=1.03, visible=True)
|
131 |
-
watermark = watermark.update(True)
|
132 |
-
disclaimer = disclaimer.update(visible=False)
|
133 |
-
return (
|
134 |
-
disclaimer,
|
135 |
-
typical_p,
|
136 |
-
top_p,
|
137 |
-
top_k,
|
138 |
-
temperature,
|
139 |
-
repetition_penalty,
|
140 |
-
watermark,
|
141 |
-
)
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
title = """<h1 align="center">xChat</h1>"""
|
146 |
-
description = """
|
147 |
-
"""
|
148 |
-
|
149 |
-
text_generation_inference = """
|
150 |
-
"""
|
151 |
-
|
152 |
-
openchat_disclaimer = """
|
153 |
-
"""
|
154 |
-
|
155 |
-
with gr.Blocks(
|
156 |
-
css="""#col_container {margin-left: auto; margin-right: auto;}
|
157 |
-
#chatbot {height: 520px; overflow: auto;}"""
|
158 |
-
) as demo:
|
159 |
-
gr.HTML(title)
|
160 |
-
gr.Markdown(text_generation_inference, visible=True)
|
161 |
-
with gr.Column(elem_id="col_container"):
|
162 |
-
model = gr.Radio(
|
163 |
-
value="OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
|
164 |
-
choices=[
|
165 |
-
"OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
|
166 |
-
"OpenAssistant/oasst-sft-1-pythia-12b",
|
167 |
-
"togethercomputer/GPT-NeoXT-Chat-Base-20B",
|
168 |
-
],
|
169 |
-
label="Model",
|
170 |
-
interactive=True,
|
171 |
-
)
|
172 |
-
|
173 |
-
chatbot = gr.Chatbot(elem_id="chatbot")
|
174 |
-
inputs = gr.Textbox(
|
175 |
-
placeholder="Vozdra raja!", label="Unesi pitanje i pritisni Enter"
|
176 |
-
)
|
177 |
-
disclaimer = gr.Markdown(openchat_disclaimer, visible=False)
|
178 |
-
state = gr.State([])
|
179 |
-
b1 = gr.Button(label="Resetuj tekst")
|
180 |
-
|
181 |
-
with gr.Accordion("Parametri", open=False):
|
182 |
-
typical_p = gr.Slider(
|
183 |
-
minimum=-0,
|
184 |
-
maximum=1.0,
|
185 |
-
value=0.2,
|
186 |
-
step=0.05,
|
187 |
-
interactive=True,
|
188 |
-
label="Tipična P masa",
|
189 |
-
)
|
190 |
-
top_p = gr.Slider(
|
191 |
-
minimum=-0,
|
192 |
-
maximum=1.0,
|
193 |
-
value=0.25,
|
194 |
-
step=0.05,
|
195 |
-
interactive=True,
|
196 |
-
label="Top-p (uzorkovanje jezgra)",
|
197 |
-
visible=False,
|
198 |
-
)
|
199 |
-
temperature = gr.Slider(
|
200 |
-
minimum=-0,
|
201 |
-
maximum=5.0,
|
202 |
-
value=0.6,
|
203 |
-
step=0.1,
|
204 |
-
interactive=True,
|
205 |
-
label="Temperatura",
|
206 |
-
visible=False,
|
207 |
-
)
|
208 |
-
top_k = gr.Slider(
|
209 |
-
minimum=1,
|
210 |
-
maximum=50,
|
211 |
-
value=50,
|
212 |
-
step=1,
|
213 |
-
interactive=True,
|
214 |
-
label="Top-k",
|
215 |
-
visible=False,
|
216 |
-
)
|
217 |
-
repetition_penalty = gr.Slider(
|
218 |
-
minimum=0.1,
|
219 |
-
maximum=3.0,
|
220 |
-
value=1.03,
|
221 |
-
step=0.01,
|
222 |
-
interactive=True,
|
223 |
-
label="Kazna za ponavljanje",
|
224 |
-
visible=False,
|
225 |
-
)
|
226 |
-
watermark = gr.Checkbox(value=False, label="Vodeni žig teksta")
|
227 |
-
|
228 |
-
model.change(
|
229 |
-
lambda value: radio_on_change(
|
230 |
-
value,
|
231 |
-
disclaimer,
|
232 |
-
typical_p,
|
233 |
-
top_p,
|
234 |
-
top_k,
|
235 |
-
temperature,
|
236 |
-
repetition_penalty,
|
237 |
-
watermark,
|
238 |
-
),
|
239 |
-
inputs=model,
|
240 |
-
outputs=[
|
241 |
-
disclaimer,
|
242 |
-
typical_p,
|
243 |
-
top_p,
|
244 |
-
top_k,
|
245 |
-
temperature,
|
246 |
-
repetition_penalty,
|
247 |
-
watermark,
|
248 |
-
],
|
249 |
-
)
|
250 |
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
model,
|
255 |
-
inputs,
|
256 |
-
typical_p,
|
257 |
-
top_p,
|
258 |
-
temperature,
|
259 |
-
top_k,
|
260 |
-
repetition_penalty,
|
261 |
-
watermark,
|
262 |
-
chatbot,
|
263 |
-
state,
|
264 |
-
],
|
265 |
-
[chatbot, state],
|
266 |
-
)
|
267 |
-
b1.click(
|
268 |
-
predict,
|
269 |
-
[
|
270 |
-
model,
|
271 |
-
inputs,
|
272 |
-
typical_p,
|
273 |
-
top_p,
|
274 |
-
temperature,
|
275 |
-
top_k,
|
276 |
-
repetition_penalty,
|
277 |
-
watermark,
|
278 |
-
chatbot,
|
279 |
-
state,
|
280 |
-
],
|
281 |
-
[chatbot, state],
|
282 |
-
)
|
283 |
-
b1.click(reset_textbox, [], [inputs])
|
284 |
-
inputs.submit(reset_textbox, [], [inputs])
|
285 |
|
286 |
-
|
287 |
-
demo.queue(concurrency_count=16).launch(debug=True)
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import torch
|
3 |
+
#set up the model (large version of DialoGPT)
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
5 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
#Defining a predict function
|
8 |
|
9 |
+
def predict(input, history=[]):
|
10 |
+
# tokenize the new input sentence
|
11 |
+
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
12 |
|
13 |
+
# append the new user input tokens to the chat history
|
14 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
15 |
|
16 |
+
# generate a response
|
17 |
+
history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
|
18 |
|
19 |
+
# convert the tokens to text, and then split the responses into lines
|
20 |
+
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
21 |
+
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
22 |
+
return response, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
#creating a gradio interface
|
|
|
|
|
25 |
|
26 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
demo = gr.Interface(fn=predict,
|
29 |
+
inputs=["text", "state"],
|
30 |
+
outputs=["chatbot", "state"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
demo.launch()
|
|