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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -47
app.py CHANGED
@@ -1,63 +1,63 @@
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()
 
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()