mukilan-k commited on
Commit
334e0a2
Β·
verified Β·
1 Parent(s): 30c2137

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -68
app.py CHANGED
@@ -1,76 +1,63 @@
1
  import gradio as gr
2
- import random
3
  from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
4
 
5
- # Load the model and tokenizer (optimized for speed)
6
- tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
7
- model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
8
 
9
- # Cute chatbot name
10
- ai_name = "Lan πŸ’–"
11
 
12
- # List of cute responses
13
- cute_replies = [
14
- "Aww, that's so sweet! 😘",
15
- "You make my heart flutter! πŸ’“",
16
- "Hehe, you’re adorable! πŸ₯°",
17
- "I could talk to you forever! 😍",
18
- "Tell me more, cutie! 😊",
19
- ]
20
-
21
- # List of questions to make the chat fun
22
- cute_questions = [
23
- "What’s the sweetest thing someone has ever said to you? πŸ’•",
24
- "If we went on a dream date, where would it be? 🌸",
25
- "Tell me, do you like hugs or kisses more? 😘",
26
- "What’s one thing that always makes you smile? 😊",
27
- "What’s your biggest dream, love? ✨",
28
- ]
29
-
30
- # Chat function
31
- def chat(user_input, history=[]):
32
- global ai_name
33
-
34
- # Name-changing logic
35
- if "call you" in user_input.lower() or "your name is" in user_input.lower():
36
- new_name = user_input.split("call you")[-1].strip(" ?.!") or user_input.split("your name is")[-1].strip(" ?.!")
37
- ai_name = new_name.capitalize() + " πŸ’•"
38
- return f"Wow! I love my new name: {ai_name}! 😍"
39
-
40
- if "what is your name" in user_input.lower():
41
- return f"My name is {ai_name}! 😊"
42
-
43
- # Tokenize input and generate response
 
 
 
 
 
 
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
- # Add random cute responses
49
- response += " " + random.choice(cute_replies)
50
-
51
- # Occasionally ask a cute question
52
- if random.random() < 0.4:
53
- response += " " + random.choice(cute_questions)
54
-
55
- return response
56
-
57
- # Gradio Interface with Cute Theme
58
- with gr.Blocks(theme=gr.themes.Soft()) as iface:
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()