Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,44 @@
|
|
1 |
-
from transformers import
|
2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
model = GPTNeoForCausalLM.from_pretrained("EleutherAI/gpt-neo-125M")
|
5 |
-
tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
|
6 |
-
|
7 |
-
prompt = """This is a discussion between a person and Hassan Kane, an entrepreneur.
|
8 |
-
person: What are you working on?
|
9 |
-
Hassan: This new AI community building the future of Africa
|
10 |
-
person: Where are you?
|
11 |
-
Hassan: In Lagos for a week, then Paris or London.
|
12 |
-
person: How's it going?
|
13 |
-
Hassan: Not bad.. Just trying to hit EV (escape velocity) with my startup
|
14 |
-
person: """
|
15 |
-
|
16 |
-
def my_split(s, seps):
|
17 |
-
res = [s]
|
18 |
-
for sep in seps:
|
19 |
-
s, res = res, []
|
20 |
-
for seq in s:
|
21 |
-
res += seq.split(sep)
|
22 |
-
return res
|
23 |
-
|
24 |
-
# input = "Who are you?"
|
25 |
-
def chat_base(input):
|
26 |
-
p = prompt + input
|
27 |
-
input_ids = tokenizer(p, return_tensors="pt").input_ids
|
28 |
-
gen_tokens = model.generate(input_ids, do_sample=True, temperature=0.7, max_length=150,)
|
29 |
-
gen_text = tokenizer.batch_decode(gen_tokens)[0]
|
30 |
-
# print(gen_text)
|
31 |
-
result = gen_text[len(p):]
|
32 |
-
# print(">", result)
|
33 |
-
result = my_split(result, [']', '\n'])[1]
|
34 |
-
# print(">>", result)
|
35 |
-
if "Hassan: " in result:
|
36 |
-
result = result.split("Hassan: ")[-1]
|
37 |
-
# print(">>>", result)
|
38 |
-
return result
|
39 |
-
|
40 |
import gradio as gr
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import torch
|
3 |
+
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
5 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
6 |
+
|
7 |
+
def predict(input, history=[]):
|
8 |
+
# tokenize the new input sentence
|
9 |
+
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
10 |
+
|
11 |
+
# append the new user input tokens to the chat history
|
12 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
13 |
+
|
14 |
+
# generate a response
|
15 |
+
history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
|
16 |
+
|
17 |
+
# convert the tokens to text, and then split the responses into lines
|
18 |
+
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
19 |
+
response.remove("")
|
20 |
+
|
21 |
+
# write some HTML
|
22 |
+
html = "<div class='chatbot'>"
|
23 |
+
for m, msg in enumerate(response):
|
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 |
import gradio as gr
|
31 |
|
32 |
+
css = """
|
33 |
+
.chatbox {display:flex;flex-direction:column}
|
34 |
+
.msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
|
35 |
+
.msg.user {background-color:cornflowerblue;color:white}
|
36 |
+
.msg.bot {background-color:lightgray;align-self:self-end}
|
37 |
+
.footer {display:none !important}
|
38 |
+
"""
|
39 |
+
|
40 |
+
gr.Interface(fn=predict,
|
41 |
+
theme="default",
|
42 |
+
inputs=[gr.inputs.Textbox(placeholder="How are you?"), "state"],
|
43 |
+
outputs=["html", "state"],
|
44 |
+
css=css).launch()
|