Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,63 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
3 |
|
4 |
-
# Load
|
5 |
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-1B-distill")
|
6 |
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-1B-distill")
|
7 |
|
8 |
-
#
|
9 |
-
user_memory = {"name": "
|
10 |
|
11 |
-
def
|
12 |
-
user_input = user_input.strip()
|
13 |
|
14 |
-
# Handle
|
15 |
-
if "my name is" in user_input
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
if "how old am i" in user_input.lower() and user_memory["age"]:
|
20 |
-
return f"You told me you're {user_memory['age']} years old, {user_memory['name']}! π"
|
21 |
-
|
22 |
-
if "where do i live" in user_input.lower() and user_memory["city"]:
|
23 |
-
return f"You live in {user_memory['city']}! π‘"
|
24 |
-
|
25 |
-
if "what do i like" in user_input.lower() and user_memory["like"]:
|
26 |
-
return f"You love {user_memory['like']}! Thatβs so cool! π"
|
27 |
-
|
28 |
-
if "what's my favorite" in user_input.lower() and user_memory["favorite"]:
|
29 |
-
return f"Your favorite thing is {user_memory['favorite']}! π₯°"
|
30 |
|
31 |
-
#
|
32 |
-
if "
|
33 |
-
|
34 |
-
return "Got it! I'll remember your age. π"
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
return f"
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
#
|
49 |
inputs = tokenizer(user_input, return_tensors="pt")
|
50 |
reply_ids = model.generate(**inputs, max_length=100)
|
51 |
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
.
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
3 |
|
4 |
+
# Load Model & Tokenizer
|
5 |
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-1B-distill")
|
6 |
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-1B-distill")
|
7 |
|
8 |
+
# Persistent user details
|
9 |
+
user_memory = {"name": "Buddy", "age": "unknown", "city": "unknown", "like": "unknown", "favorite": "unknown"}
|
10 |
|
11 |
+
def chat_with_ai(user_input):
|
12 |
+
user_input = user_input.strip().lower()
|
13 |
|
14 |
+
# Handle name change
|
15 |
+
if "call me" in user_input or "my name is" in user_input:
|
16 |
+
new_name = user_input.split("call me")[-1].strip(" ?.!") or user_input.split("my name is")[-1].strip(" ?.!")
|
17 |
+
user_memory["name"] = new_name.capitalize()
|
18 |
+
return f"Aww! I love that name, {user_memory['name']}! π"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Ask for name
|
21 |
+
if "what is my name" in user_input:
|
22 |
+
return f"Your name is {user_memory['name']}! π"
|
|
|
23 |
|
24 |
+
# Ask for AI's name
|
25 |
+
if "what is your name" in user_input:
|
26 |
+
return f"I'm your bestie, {user_memory['name']}! Call me Lan! π₯°"
|
27 |
|
28 |
+
# Handle personal details
|
29 |
+
for detail in ["age", "city", "like", "favorite"]:
|
30 |
+
if f"my {detail} is" in user_input:
|
31 |
+
user_memory[detail] = user_input.split(f"my {detail} is")[-1].strip(" ?.!")
|
32 |
+
return f"Got it! I'll remember that your {detail} is {user_memory[detail]}! π"
|
33 |
+
if f"what is my {detail}" in user_input:
|
34 |
+
return f"You told me your {detail} is {user_memory[detail]}! π"
|
35 |
+
|
36 |
+
# Tokenize and generate response
|
37 |
inputs = tokenizer(user_input, return_tensors="pt")
|
38 |
reply_ids = model.generate(**inputs, max_length=100)
|
39 |
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
40 |
+
|
41 |
+
return response
|
42 |
+
|
43 |
+
# Custom Gradio UI
|
44 |
+
theme = gr.themes.Base(primary_hue="pink", secondary_hue="purple")
|
45 |
|
46 |
+
demo = gr.Interface(
|
47 |
+
fn=chat_with_ai,
|
48 |
+
inputs=gr.Textbox(placeholder="Type your message...", label="Chat"),
|
49 |
+
outputs=gr.Textbox(label="Lan"),
|
50 |
+
title="π AI Friend Chatbot - Talk with Lan! π¬",
|
51 |
+
description="Your cutest AI friend! Let's chat and have fun together! ππ",
|
52 |
+
theme=theme,
|
53 |
+
live=True,
|
54 |
+
examples=[
|
55 |
+
["Hi!"],
|
56 |
+
["Call me Alex"],
|
57 |
+
["What is my name?"],
|
58 |
+
["My favorite food is pizza!"],
|
59 |
+
["What is my favorite food?"],
|
60 |
+
]
|
61 |
+
)
|
62 |
|
63 |
+
demo.launch()
|