Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,51 @@
|
|
| 1 |
-
from sentence_transformers import SentenceTransformer, util
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
# Load a pre-trained sentence-transformer model
|
| 6 |
-
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 7 |
-
|
| 8 |
-
# Define your dataset
|
| 9 |
-
conversations = [
|
| 10 |
-
{"user": "What are your store hours?", "bot": "Our store is open from 9 AM to 9 PM, Monday to Saturday."},
|
| 11 |
-
{"user": "Do you sell laptops?", "bot": "Yes, we offer a range of laptops from brands like Dell, HP, and Lenovo."},
|
| 12 |
-
{"user": "What is the price of the iPhone 14?", "bot": "The iPhone 14 starts at $799."},
|
| 13 |
-
{"user": "Can I return a product I bought last week?", "bot": "You can return products within 30 days of purchase with a valid receipt."},
|
| 14 |
-
{"user": "Do you have any discounts available?", "bot": "Yes, we currently have a 10% discount on selected electronics."},
|
| 15 |
-
{"user": "What is your exchange policy?", "bot": "You can exchange items within 14 days of purchase, as long as they are in original condition with a receipt."},
|
| 16 |
-
{"user": "How can I track my order?", "bot": "You can track your order by logging into your account and clicking 'Track Order' under 'My Orders'."},
|
| 17 |
-
{"user": "Do you offer home delivery?", "bot": "Yes, we offer home delivery for most items. Delivery charges may apply based on your location."},
|
| 18 |
-
{"user": "Can I cancel my order?", "bot": "Yes, you can cancel your order within 24 hours of placing it by going to your account and selecting the cancel option."},
|
| 19 |
-
{"user": "Do you have any new arrivals in smartphones?", "bot": "Yes, we have the latest models from Apple, Samsung, and OnePlus available in store and online."}
|
| 20 |
-
]
|
| 21 |
-
|
| 22 |
-
# Precompute embeddings for the dataset
|
| 23 |
-
conversation_texts = [conv['user'] for conv in conversations]
|
| 24 |
-
conversation_embeddings = model.encode(conversation_texts, convert_to_tensor=True)
|
| 25 |
-
|
| 26 |
-
def chatbot_response(user_input):
|
| 27 |
-
# Compute embedding for the user input
|
| 28 |
-
user_embedding = model.encode(user_input, convert_to_tensor=True)
|
| 29 |
-
|
| 30 |
-
# Compute cosine similarity between the user input and all predefined conversations
|
| 31 |
-
similarities = util.pytorch_cos_sim(user_embedding, conversation_embeddings)
|
| 32 |
-
|
| 33 |
-
# Find the conversation with the highest similarity
|
| 34 |
-
best_match_idx = torch.argmax(similarities)
|
| 35 |
-
|
| 36 |
-
# Return the bot response from the best matching conversation
|
| 37 |
-
return conversations[best_match_idx]['bot']
|
| 38 |
-
|
| 39 |
-
# Create Gradio interface
|
| 40 |
-
iface = gr.Interface(fn=chatbot_response, inputs="text", outputs="text", title="Retail Store Chatbot"
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer, util
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load a pre-trained sentence-transformer model
|
| 6 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 7 |
+
|
| 8 |
+
# Define your dataset
|
| 9 |
+
conversations = [
|
| 10 |
+
{"user": "What are your store hours?", "bot": "Our store is open from 9 AM to 9 PM, Monday to Saturday."},
|
| 11 |
+
{"user": "Do you sell laptops?", "bot": "Yes, we offer a range of laptops from brands like Dell, HP, and Lenovo."},
|
| 12 |
+
{"user": "What is the price of the iPhone 14?", "bot": "The iPhone 14 starts at $799."},
|
| 13 |
+
{"user": "Can I return a product I bought last week?", "bot": "You can return products within 30 days of purchase with a valid receipt."},
|
| 14 |
+
{"user": "Do you have any discounts available?", "bot": "Yes, we currently have a 10% discount on selected electronics."},
|
| 15 |
+
{"user": "What is your exchange policy?", "bot": "You can exchange items within 14 days of purchase, as long as they are in original condition with a receipt."},
|
| 16 |
+
{"user": "How can I track my order?", "bot": "You can track your order by logging into your account and clicking 'Track Order' under 'My Orders'."},
|
| 17 |
+
{"user": "Do you offer home delivery?", "bot": "Yes, we offer home delivery for most items. Delivery charges may apply based on your location."},
|
| 18 |
+
{"user": "Can I cancel my order?", "bot": "Yes, you can cancel your order within 24 hours of placing it by going to your account and selecting the cancel option."},
|
| 19 |
+
{"user": "Do you have any new arrivals in smartphones?", "bot": "Yes, we have the latest models from Apple, Samsung, and OnePlus available in store and online."}
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
# Precompute embeddings for the dataset
|
| 23 |
+
conversation_texts = [conv['user'] for conv in conversations]
|
| 24 |
+
conversation_embeddings = model.encode(conversation_texts, convert_to_tensor=True)
|
| 25 |
+
|
| 26 |
+
def chatbot_response(user_input):
|
| 27 |
+
# Compute embedding for the user input
|
| 28 |
+
user_embedding = model.encode(user_input, convert_to_tensor=True)
|
| 29 |
+
|
| 30 |
+
# Compute cosine similarity between the user input and all predefined conversations
|
| 31 |
+
similarities = util.pytorch_cos_sim(user_embedding, conversation_embeddings)
|
| 32 |
+
|
| 33 |
+
# Find the conversation with the highest similarity
|
| 34 |
+
best_match_idx = torch.argmax(similarities)
|
| 35 |
+
|
| 36 |
+
# Return the bot response from the best matching conversation
|
| 37 |
+
return conversations[best_match_idx]['bot']
|
| 38 |
+
|
| 39 |
+
# Create Gradio interface
|
| 40 |
+
iface = gr.Interface(fn=chatbot_response, inputs="text", outputs="text", title="Retail Store Chatbot",description="Ask me anything about our retail store! I can provide information about store hours, product availability, return policies, and more.",
|
| 41 |
+
examples=[
|
| 42 |
+
["What are your store hours?"],
|
| 43 |
+
["Do you sell laptops?"],
|
| 44 |
+
["What is the price of the iPhone 14?"],
|
| 45 |
+
["Can I return a product I bought last week?"],
|
| 46 |
+
["Do you have any discounts available?"]
|
| 47 |
+
]
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Launch the chatbot
|
| 51 |
+
iface.launch()
|