Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,44 @@
|
|
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 |
ai_name = "Lan" # Default AI name
|
9 |
-
|
10 |
-
def chatbot_response(user_input):
|
11 |
global ai_name
|
12 |
-
|
13 |
# Handle name change
|
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 f"Wow! I love my new name: {ai_name}! π"
|
18 |
-
|
19 |
# Handle name inquiry
|
20 |
if "what is your name" in user_input.lower():
|
21 |
-
return f"My name is {ai_name}! π"
|
22 |
-
|
23 |
# Generate response
|
24 |
inputs = tokenizer(user_input, return_tensors="pt")
|
25 |
reply_ids = model.generate(**inputs, max_length=100)
|
26 |
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
27 |
-
|
28 |
-
return response
|
29 |
-
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
)
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
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()
|