mukilan-k commited on
Commit
63fe53b
Β·
verified Β·
1 Parent(s): 580d45d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -32
app.py CHANGED
@@ -1,44 +1,31 @@
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
- ai_name = "Lan" # Default AI name
9
- def chat_with_ai(user_input, history=[]):
 
 
10
  global ai_name
11
-
12
- # Handle name change
13
  if "call you" in user_input.lower() or "your name is" in user_input.lower():
14
  new_name = user_input.split("call you")[-1].strip(" ?.!") or user_input.split("your name is")[-1].strip(" ?.!")
15
  ai_name = new_name.capitalize()
16
- return f"{ai_name}: Wow! I love my new name: {ai_name}! 😍"
17
-
18
- # Handle name inquiry
19
  if "what is your name" in user_input.lower():
20
- return f"{ai_name}: My name is {ai_name}! 😊"
21
-
22
- # Generate response
23
  inputs = tokenizer(user_input, return_tensors="pt")
24
  reply_ids = model.generate(**inputs, max_length=100)
25
  response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
26
-
27
- return f"{ai_name}: {response}"
28
-
29
- # Gradio UI
30
- def chat_interface():
31
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
32
- gr.Markdown("## πŸ€– AI Friend Chatbot - Talk with Lan!")
33
- with gr.Row():
34
- user_input = gr.Textbox(placeholder="Type your message...", label="You:")
35
- chat_output = gr.Textbox(label="Lan:", interactive=False)
36
- with gr.Row():
37
- submit_btn = gr.Button("Send", variant="primary")
38
- clear_btn = gr.Button("Clear")
39
-
40
- submit_btn.click(chat_with_ai, inputs=[user_input], outputs=[chat_output])
41
- clear_btn.click(lambda: "", inputs=[], outputs=[chat_output])
42
- return demo
43
-
44
- chat_interface().launch()
 
1
  import gradio as gr
2
  from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
3
 
4
+ # Load the model and tokenizer
5
+ tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill") # Faster model
6
+ model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
7
 
8
+ ai_name = "Lan" # Default name
9
+
10
+ # Chat function for Gradio
11
+ def chat(user_input, history=[]):
12
  global ai_name
13
+
 
14
  if "call you" in user_input.lower() or "your name is" in user_input.lower():
15
  new_name = user_input.split("call you")[-1].strip(" ?.!") or user_input.split("your name is")[-1].strip(" ?.!")
16
  ai_name = new_name.capitalize()
17
+ return [(user_input, f"Wow! I love my new name: {ai_name}! 😍")]
18
+
 
19
  if "what is your name" in user_input.lower():
20
+ return [(user_input, f"My name is {ai_name}! 😊")]
21
+
22
+ # Tokenize input and generate response
23
  inputs = tokenizer(user_input, return_tensors="pt")
24
  reply_ids = model.generate(**inputs, max_length=100)
25
  response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
26
+
27
+ return [(user_input, response)]
28
+
29
+ # Gradio Interface
30
+ iface = gr.ChatInterface(fn=chat, title="AI Friend Chatbot - Talk with Lan!", theme="dark")
31
+ iface.launch()