Update app.py
Browse files
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-
|
6 |
-
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-
|
7 |
|
8 |
-
ai_name = "Lan" # Default
|
9 |
-
|
|
|
|
|
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"
|
17 |
-
|
18 |
-
# Handle name inquiry
|
19 |
if "what is your name" in user_input.lower():
|
20 |
-
return f"
|
21 |
-
|
22 |
-
#
|
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
|
28 |
-
|
29 |
-
# Gradio
|
30 |
-
|
31 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|