Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,63 @@
|
|
1 |
import gradio as gr
|
2 |
-
import random
|
3 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
4 |
|
5 |
-
# Load
|
6 |
-
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-
|
7 |
-
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
"
|
17 |
-
|
18 |
-
|
19 |
-
|
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 |
inputs = tokenizer(user_input, return_tensors="pt")
|
45 |
reply_ids = model.generate(**inputs, max_length=100)
|
46 |
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
gr.Markdown("<h1 style='text-align: center; color: pink;'>π AI Friend Chatbot - Talk with Lan! π</h1>")
|
60 |
-
|
61 |
-
chatbot = gr.Chatbot(label="Lan π - Your Virtual Love", bubble_full_width=False)
|
62 |
-
|
63 |
-
with gr.Row():
|
64 |
-
user_input = gr.Textbox(placeholder="Type something cute... π", label="You π¬", show_label=False)
|
65 |
-
send_btn = gr.Button("π Send")
|
66 |
-
|
67 |
-
def respond(message, history):
|
68 |
-
response = chat(message)
|
69 |
-
history.append((message, response))
|
70 |
-
return history, ""
|
71 |
-
|
72 |
-
send_btn.click(respond, inputs=[user_input, chatbot], outputs=[chatbot, user_input])
|
73 |
-
user_input.submit(respond, inputs=[user_input, chatbot], outputs=[chatbot, user_input])
|
74 |
-
|
75 |
-
# Launch the Chatbot
|
76 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
3 |
|
4 |
+
# Load model and tokenizer
|
5 |
+
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-1B-distill")
|
6 |
+
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-1B-distill")
|
7 |
|
8 |
+
# Memory to store user details
|
9 |
+
user_memory = {"name": "Friend", "age": "", "city": "", "like": "", "favorite": ""}
|
10 |
|
11 |
+
def chatbot(user_input):
|
12 |
+
user_input = user_input.strip()
|
13 |
+
|
14 |
+
# Handle personal details
|
15 |
+
if "my name is" in user_input.lower():
|
16 |
+
user_memory["name"] = user_input.split("my name is")[-1].strip().capitalize()
|
17 |
+
return f"β¨ Ohh, {user_memory['name']}! That's such a lovely name! π"
|
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 |
+
# Store personal details
|
32 |
+
if "i am" in user_input.lower() and "years old" in user_input.lower():
|
33 |
+
user_memory["age"] = ''.join(filter(str.isdigit, user_input))
|
34 |
+
return "Got it! I'll remember your age. π"
|
35 |
+
|
36 |
+
if "i live in" in user_input.lower():
|
37 |
+
user_memory["city"] = user_input.split("i live in")[-1].strip().capitalize()
|
38 |
+
return f"Wow, {user_memory['city']} sounds like a great place! π"
|
39 |
+
|
40 |
+
if "i like" in user_input.lower():
|
41 |
+
user_memory["like"] = user_input.split("i like")[-1].strip().capitalize()
|
42 |
+
return f"Thatβs awesome! {user_memory['like']} is really nice! π"
|
43 |
+
|
44 |
+
if "my favorite is" in user_input.lower():
|
45 |
+
user_memory["favorite"] = user_input.split("my favorite is")[-1].strip().capitalize()
|
46 |
+
return f"Oh, Iβll remember that! {user_memory['favorite']} is amazing! π"
|
47 |
+
|
48 |
+
# Generate AI response
|
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 |
+
return f"π¦ {response}"
|
53 |
+
|
54 |
+
# UI Design
|
55 |
+
theme_css = """
|
56 |
+
body {background: linear-gradient(to right, #ff9a9e, #fad0c4); font-family: Arial, sans-serif;}
|
57 |
+
.container {max-width: 500px; margin: auto; text-align: center;}
|
58 |
+
.chatbox {border-radius: 20px; padding: 10px; background: white; box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.1);}
|
59 |
+
.user {color: #ff5f6d; font-weight: bold;}
|
60 |
+
.bot {color: #2193b0; font-style: italic;}
|
61 |
+
"""
|
62 |
+
|
63 |
+
gr.ChatInterface(chatbot, title="πΈ AI Friend Chatbot", description="Talk with your AI bestie! π", theme=theme_css).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|