Spaces:
Sleeping
Sleeping
Create Main.py
Browse filesChatbot business model.
Main.py
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
import time
|
4 |
+
|
5 |
+
# Business information - customize this for your business
|
6 |
+
BUSINESS_INFO = {
|
7 |
+
"name": "TechGadget Store",
|
8 |
+
"hours": "9AM-6PM Monday to Saturday",
|
9 |
+
"products": "smartphones, laptops, tablets, and accessories",
|
10 |
+
"address": "123 Main Street, Tech City",
|
11 |
+
"phone": "(555) 123-4567",
|
12 |
+
"email": "[email protected]",
|
13 |
+
"return_policy": "30-day money-back guarantee on all products",
|
14 |
+
"shipping": "Free shipping on orders over $50"
|
15 |
+
}
|
16 |
+
|
17 |
+
# Sample product database
|
18 |
+
PRODUCTS = {
|
19 |
+
"smartphone": {
|
20 |
+
"basic": {"name": "BasicPhone", "price": "$199", "features": "6.1\" display, 128GB storage"},
|
21 |
+
"pro": {"name": "ProPhone X", "price": "$899", "features": "6.7\" OLED, 512GB, 5G"}
|
22 |
+
},
|
23 |
+
"laptop": {
|
24 |
+
"standard": {"name": "WorkBook Lite", "price": "$699", "features": "15\" display, 8GB RAM, 256GB SSD"},
|
25 |
+
"premium": {"name": "WorkBook Pro", "price": "$1499", "features": "16\" display, 16GB RAM, 1TB SSD"}
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|
29 |
+
# Predefined responses for common queries
|
30 |
+
def get_business_response(intent):
|
31 |
+
responses = {
|
32 |
+
"hours": f"Our store hours are {BUSINESS_INFO['hours']}.",
|
33 |
+
"location": f"We're located at {BUSINESS_INFO['address']}. Come visit us!",
|
34 |
+
"contact": f"You can reach us at {BUSINESS_INFO['phone']} or email {BUSINESS_INFO['email']}.",
|
35 |
+
"products": f"We offer a wide range of {BUSINESS_INFO['products']}. What specifically are you interested in?",
|
36 |
+
"returns": f"Our return policy: {BUSINESS_INFO['return_policy']}.",
|
37 |
+
"shipping": f"Shipping info: {BUSINESS_INFO['shipping']}.",
|
38 |
+
"greeting": "Hello! Welcome to TechGadget Store. How can I help you today?",
|
39 |
+
"thanks": "You're welcome! Is there anything else I can help you with?",
|
40 |
+
"goodbye": "Thank you for contacting TechGadget Store. Have a great day!"
|
41 |
+
}
|
42 |
+
return responses.get(intent, "I'm not sure how to help with that. Can you please rephrase?")
|
43 |
+
|
44 |
+
# Function to handle product inquiries
|
45 |
+
def handle_product_inquiry(product_type, model=None):
|
46 |
+
if product_type not in PRODUCTS:
|
47 |
+
return f"We don't carry that product. We mainly offer {', '.join(PRODUCTS.keys())}."
|
48 |
+
|
49 |
+
if model:
|
50 |
+
if model in PRODUCTS[product_type]:
|
51 |
+
product = PRODUCTS[product_type][model]
|
52 |
+
return f"Our {model} {product_type} is the {product['name']}, priced at {product['price']}. Features: {product['features']}"
|
53 |
+
else:
|
54 |
+
return f"We don't have a {model} model for {product_type}. We have {', '.join(PRODUCTS[product_type].keys())}."
|
55 |
+
else:
|
56 |
+
models = PRODUCTS[product_type]
|
57 |
+
response = f"For {product_type}, we have: "
|
58 |
+
for model_name, details in models.items():
|
59 |
+
response += f"\n- {model_name}: {details['name']} ({details['price']})"
|
60 |
+
return response
|
61 |
+
|
62 |
+
# Main chat function
|
63 |
+
def chat_with_bot(message, chat_history):
|
64 |
+
user_message = message.lower()
|
65 |
+
bot_response = ""
|
66 |
+
|
67 |
+
# Check for greetings
|
68 |
+
if any(word in user_message for word in ["hi", "hello", "hey", "greetings"]):
|
69 |
+
bot_response = get_business_response("greeting")
|
70 |
+
|
71 |
+
# Check for thanks
|
72 |
+
elif any(word in user_message for word in ["thank", "appreciate", "thanks"]):
|
73 |
+
bot_response = get_business_response("thanks")
|
74 |
+
|
75 |
+
# Check for goodbye
|
76 |
+
elif any(word in user_message for word in ["bye", "goodbye", "see you"]):
|
77 |
+
bot_response = get_business_response("goodbye")
|
78 |
+
|
79 |
+
# Check for business hours
|
80 |
+
elif any(word in user_message for word in ["hour", "time", "open", "close"]):
|
81 |
+
bot_response = get_business_response("hours")
|
82 |
+
|
83 |
+
# Check for location
|
84 |
+
elif any(word in user_message for word in ["where", "location", "address", "find"]):
|
85 |
+
bot_response = get_business_response("location")
|
86 |
+
|
87 |
+
# Check for contact info
|
88 |
+
elif any(word in user_message for word in ["contact", "phone", "call", "email"]):
|
89 |
+
bot_response = get_business_response("contact")
|
90 |
+
|
91 |
+
# Check for products
|
92 |
+
elif any(word in user_message for word in ["product", "sell", "offer", "item"]):
|
93 |
+
bot_response = get_business_response("products")
|
94 |
+
|
95 |
+
# Check for returns
|
96 |
+
elif any(word in user_message for word in ["return", "exchange", "refund"]):
|
97 |
+
bot_response = get_business_response("returns")
|
98 |
+
|
99 |
+
# Check for shipping
|
100 |
+
elif any(word in user_message for word in ["ship", "delivery", "deliver"]):
|
101 |
+
bot_response = get_business_response("shipping")
|
102 |
+
|
103 |
+
# Handle product inquiries
|
104 |
+
elif any(word in user_message for word in ["smartphone", "phone"]):
|
105 |
+
if "pro" in user_message or "premium" in user_message:
|
106 |
+
bot_response = handle_product_inquiry("smartphone", "pro")
|
107 |
+
elif "basic" in user_message or "cheap" in user_message:
|
108 |
+
bot_response = handle_product_inquiry("smartphone", "basic")
|
109 |
+
else:
|
110 |
+
bot_response = handle_product_inquiry("smartphone")
|
111 |
+
|
112 |
+
elif any(word in user_message for word in ["laptop", "computer"]):
|
113 |
+
if "pro" in user_message or "premium" in user_message:
|
114 |
+
bot_response = handle_product_inquiry("laptop", "premium")
|
115 |
+
elif "standard" in user_message or "basic" in user_message:
|
116 |
+
bot_response = handle_product_inquiry("laptop", "standard")
|
117 |
+
else:
|
118 |
+
bot_response = handle_product_inquiry("laptop")
|
119 |
+
|
120 |
+
# Default response for unrecognized queries
|
121 |
+
else:
|
122 |
+
bot_response = "I'm here to help with information about our products, store hours, location, and policies. How can I assist you today?"
|
123 |
+
|
124 |
+
# Add a small delay to make it feel more natural
|
125 |
+
time.sleep(0.5)
|
126 |
+
|
127 |
+
# Append to chat history
|
128 |
+
chat_history.append((message, bot_response))
|
129 |
+
|
130 |
+
return "", chat_history
|
131 |
+
|
132 |
+
# Custom CSS for styling
|
133 |
+
custom_css = """
|
134 |
+
#chatbot {
|
135 |
+
font-family: 'Arial', sans-serif;
|
136 |
+
}
|
137 |
+
.gr-button {
|
138 |
+
background-color: #4CAF50 !important;
|
139 |
+
color: white !important;
|
140 |
+
}
|
141 |
+
h1 {
|
142 |
+
color: #2C3E50;
|
143 |
+
text-align: center;
|
144 |
+
}
|
145 |
+
"""
|
146 |
+
|
147 |
+
# Create the Gradio interface
|
148 |
+
with gr.Blocks(css=custom_css, title="TechGadget Store Support") as demo:
|
149 |
+
gr.Markdown("# 🤖 TechGadget Store Support Chat")
|
150 |
+
gr.Markdown("### How can I help you with our products and services today?")
|
151 |
+
|
152 |
+
with gr.Row():
|
153 |
+
with gr.Column(scale=2):
|
154 |
+
chatbot = gr.Chatbot(label="Conversation", elem_id="chatbot")
|
155 |
+
msg = gr.Textbox(
|
156 |
+
label="Type your message here...",
|
157 |
+
placeholder="What are your store hours?",
|
158 |
+
lines=1
|
159 |
+
)
|
160 |
+
clear = gr.Button("Clear Conversation")
|
161 |
+
|
162 |
+
with gr.Column(scale=1):
|
163 |
+
gr.Markdown("### 💡 Common Questions")
|
164 |
+
gr.Examples(
|
165 |
+
examples=[
|
166 |
+
["What are your store hours?"],
|
167 |
+
["Where are you located?"],
|
168 |
+
["What smartphones do you sell?"],
|
169 |
+
["What is your return policy?"],
|
170 |
+
["Do you offer free shipping?"]
|
171 |
+
],
|
172 |
+
inputs=msg
|
173 |
+
)
|
174 |
+
gr.Markdown("### 🏪 Store Information")
|
175 |
+
gr.Markdown(f"""
|
176 |
+
- **Hours**: {BUSINESS_INFO['hours']}
|
177 |
+
- **Address**: {BUSINESS_INFO['address']}
|
178 |
+
- **Phone**: {BUSINESS_INFO['phone']}
|
179 |
+
- **Email**: {BUSINESS_INFO['email']}
|
180 |
+
""")
|
181 |
+
|
182 |
+
# Event handlers
|
183 |
+
msg.submit(chat_with_bot, [msg, chatbot], [msg, chatbot])
|
184 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
185 |
+
|
186 |
+
# Launch the application
|
187 |
+
if __name__ == "__main__":
|
188 |
+
demo.launch(share=True)
|