Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,83 @@
|
|
1 |
-
|
|
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
<
|
8 |
-
|
9 |
-
|
10 |
-
</center>
|
11 |
</p>
|
12 |
"""
|
13 |
-
article = "<p style='text-align: center'><a href='https://medium.com/geekculture/discord-bot-using-dailogpt-and-huggingface-api-c71983422701' target='_blank'>Complete Tutorial</a></p><p style='text-align: center'><a href='https://dagshub.com/kingabzpro/DailoGPT-RickBot' target='_blank'>Project is Available at DAGsHub</a></p></center><center><img src='https://visitor-badge.glitch.me/badge?page_id=kingabzpro/Rick_and_Morty_Bot' alt='visitor badge'></center></p>"
|
14 |
-
examples = [["How are you Rick?"]]
|
15 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
16 |
-
import torch
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
tokenizer = AutoTokenizer.from_pretrained("ericzhou/DialoGPT-Medium-Rick_v2")
|
19 |
-
model
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
2 |
+
import torch
|
3 |
import gradio as gr
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
+
|
6 |
+
TITLE = "Talk To Me Morty"
|
7 |
|
8 |
+
DESCRIPTION = """
|
9 |
+
<p style='text-align:center'>
|
10 |
+
The bot was trained on a Rick & Morty dialogues dataset with DialoGPT.<br>
|
11 |
+
<img src="https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot/resolve/main/img/rick.png"
|
12 |
+
alt="Rick"
|
13 |
+
width="200">
|
|
|
14 |
</p>
|
15 |
"""
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
ARTICLE = """
|
18 |
+
<p style='text-align:center'>
|
19 |
+
<a href="https://medium.com/geekculture/discord-bot-using-dailogpt-and-huggingface-api-c71983422701"
|
20 |
+
target="_blank">Complete Tutorial</a> Β·
|
21 |
+
<a href="https://dagshub.com/kingabzpro/DailoGPT-RickBot"
|
22 |
+
target="_blank">Project on DAGsHub</a>
|
23 |
+
</p>
|
24 |
+
<p style='text-align:center'>
|
25 |
+
<img src="https://visitor-badge.glitch.me/badge?page_id=kingabzpro/Rick_and_Morty_Bot"
|
26 |
+
alt="visitor badge">
|
27 |
+
</p>
|
28 |
+
"""
|
29 |
+
|
30 |
+
# βββ Load model once at start ββββββββββββββββββββββββββββββββββββββββββββββββ
|
31 |
tokenizer = AutoTokenizer.from_pretrained("ericzhou/DialoGPT-Medium-Rick_v2")
|
32 |
+
model = AutoModelForCausalLM.from_pretrained("ericzhou/DialoGPT-Medium-Rick_v2")
|
33 |
+
|
34 |
+
# βββ Chat handler ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
35 |
+
def chat(user_msg: str, history_ids: list[int] | None):
|
36 |
+
if not user_msg:
|
37 |
+
return [], history_ids or []
|
38 |
+
|
39 |
+
new_ids = tokenizer.encode(user_msg + tokenizer.eos_token,
|
40 |
+
return_tensors="pt")
|
41 |
+
|
42 |
+
bot_input = (
|
43 |
+
torch.cat([torch.LongTensor(history_ids), new_ids], dim=-1)
|
44 |
+
if history_ids else new_ids
|
45 |
+
)
|
46 |
+
|
47 |
+
history_ids = model.generate(
|
48 |
+
bot_input,
|
49 |
+
max_length=min(4096, bot_input.shape[-1] + 200),
|
50 |
+
pad_token_id=tokenizer.eos_token_id,
|
51 |
+
do_sample=True,
|
52 |
+
temperature=0.7,
|
53 |
+
top_p=0.92,
|
54 |
+
top_k=50,
|
55 |
+
).tolist()
|
56 |
+
|
57 |
+
turns = tokenizer.decode(history_ids[0], skip_special_tokens=False) \
|
58 |
+
.split("<|endoftext|>")
|
59 |
+
# pack into (user, bot) pairs for Chatbot component
|
60 |
+
pairs = [(turns[i], turns[i + 1]) for i in range(0, len(turns) - 1, 2)]
|
61 |
+
return pairs, history_ids
|
62 |
+
|
63 |
+
# βββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
64 |
+
with gr.Blocks(theme=gr.themes.Seafoam()) as demo:
|
65 |
+
gr.Markdown(f"<h1 style='text-align:center'>{TITLE}</h1>")
|
66 |
+
gr.Markdown(DESCRIPTION)
|
67 |
+
|
68 |
+
chatbot = gr.Chatbot(height=450)
|
69 |
+
state = gr.State([])
|
70 |
+
|
71 |
+
with gr.Row():
|
72 |
+
prompt = gr.Textbox(placeholder="Ask Rick anythingβ¦", scale=4, show_label=False)
|
73 |
+
send = gr.Button("Send", variant="primary")
|
74 |
+
|
75 |
+
# send on click or β΅
|
76 |
+
send.click(chat, inputs=[prompt, state], outputs=[chatbot, state])
|
77 |
+
prompt.submit(chat, inputs=[prompt, state], outputs=[chatbot, state])
|
78 |
+
|
79 |
+
gr.Examples([["How are you, Rick?"], ["Tell me a joke!"]], inputs=prompt)
|
80 |
+
gr.Markdown(ARTICLE)
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
demo.launch()
|