File size: 12,751 Bytes
7a64969
 
d3ad66a
 
4f84f4f
 
d3ad66a
4f84f4f
7a64969
 
 
4f84f4f
 
 
 
d3ad66a
4f84f4f
 
d3ad66a
7a64969
d3ad66a
1e1854c
d3ad66a
4f84f4f
d3ad66a
 
7a64969
d3ad66a
7a64969
 
 
 
d3ad66a
 
7a64969
 
 
 
 
 
d3ad66a
 
7a64969
 
d3ad66a
7a64969
 
 
 
4f84f4f
7a64969
 
 
 
4f84f4f
 
 
 
 
 
 
 
808a5bf
 
 
7a64969
808a5bf
 
 
7a64969
808a5bf
d3ad66a
808a5bf
7a64969
d3ad66a
 
 
 
7a64969
d3ad66a
7a64969
 
808a5bf
d3ad66a
808a5bf
7a64969
808a5bf
d3ad66a
808a5bf
7a64969
4f84f4f
d3ad66a
4f84f4f
 
 
d3ad66a
4f84f4f
 
d3ad66a
 
 
 
1e1854c
 
 
775f7ff
808a5bf
 
 
 
 
d3ad66a
808a5bf
 
 
d3ad66a
808a5bf
 
7a64969
d3ad66a
 
7a64969
d3ad66a
7a64969
 
 
 
d3ad66a
 
7a64969
d3ad66a
7a64969
 
 
 
d3ad66a
 
 
 
 
 
4f84f4f
7a64969
4f84f4f
d3ad66a
 
 
 
 
 
7a64969
 
808a5bf
7a64969
be0447d
 
 
7a64969
 
 
9598d17
7a64969
 
775f7ff
 
8c68ece
 
 
7a64969
 
 
 
775f7ff
 
 
 
 
 
 
7a64969
 
d3ad66a
9598d17
4f84f4f
8c68ece
9598d17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c68ece
 
 
 
 
 
 
 
 
 
 
 
775f7ff
 
 
be0447d
 
 
 
 
 
775f7ff
 
 
d3ad66a
775f7ff
 
 
 
 
 
 
 
1e1854c
be0447d
 
d3ad66a
808a5bf
be0447d
4f84f4f
 
d3ad66a
 
be0447d
 
8c68ece
 
be0447d
775f7ff
 
 
 
 
 
7a64969
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import gradio as gr
import time
from business_ai import BusinessAIAssistant
from business_config import BUSINESS_CONFIG, PRODUCTS, SERVICES

# Initialize the AI assistant
ai_assistant = BusinessAIAssistant(BUSINESS_CONFIG, PRODUCTS, SERVICES)

# Predefined responses for common queries
def get_business_response(intent):
    responses = {
        "hours": f"Our store hours are {BUSINESS_CONFIG['hours']}.",
        "location": f"We're located at {BUSINESS_CONFIG['address']}. Come visit us!",
        "contact": f"You can reach us at {BUSINESS_CONFIG['phone']} or email {BUSINESS_CONFIG['email']}.",
        "products": f"We offer a wide range of {BUSINESS_CONFIG['products']}. What specifically are you interested in?",
        "services": f"We provide these services: {BUSINESS_CONFIG['services']}.",
        "returns": f"Our return policy: {BUSINESS_CONFIG['return_policy']}.",
        "shipping": f"Yes! We offer {BUSINESS_CONFIG['shipping'].lower()}",
        "greeting": f"Hello! Welcome to {BUSINESS_CONFIG['name']}. How can I help you today?",
        "thanks": "You're welcome! Is there anything else I can help you with?",
        "goodbye": f"Thank you for contacting {BUSINESS_CONFIG['name']}. Have a great day!",
        "help": "I can help you with: \n• Store hours and location \n• Product information and pricing \n• Return and shipping policies \n• Technical comparisons between products \n• And much more! What do you need help with today?",
        "capabilities": f"I'm your virtual assistant for {BUSINESS_CONFIG['name']}! I can answer questions about our products, services, policies, and more. What would you like to know?",
        "values": f"Our company values: {BUSINESS_CONFIG['values']}",
        "people": f"Key people at our company: {BUSINESS_CONFIG['key_people']}",
        "payment": f"We accept these payment methods: {BUSINESS_CONFIG['payment_methods']}."
    }
    return responses.get(intent, "")

# Function to handle product inquiries
def handle_product_inquiry(product_type, model=None):
    if product_type not in PRODUCTS:
        available_products = ", ".join(PRODUCTS.keys())
        return f"We don't carry that product. We mainly offer {available_products}."
    
    if model:
        if model in PRODUCTS[product_type]:
            product = PRODUCTS[product_type][model]
            return f"Our {model} {product_type} is the {product['name']}, priced at {product['price']}. Features: {product['features']}"
        else:
            available_models = ", ".join(PRODUCTS[product_type].keys())
            return f"We don't have a {model} model for {product_type}. We have {available_models}."
    else:
        models = PRODUCTS[product_type]
        response = f"For {product_type}, we have these models:"
        for model_name, details in models.items():
            response += f"\n- {model_name}: {details['name']} ({details['price']})"
        return response

# Main chat function
def chat_with_bot(message, chat_history):
    user_message = message.lower()
    bot_response = ""

    # Format conversation history for context
    conversation_history = ""
    for entry in chat_history:
        if entry['role'] == 'user':
            conversation_history += f"Customer: {entry['content']}\n"
        else:
            conversation_history += f"Assistant: {entry['content']}\n"

    # Check for shipping first (before greetings to avoid "hi" in "shipping" triggering greeting)
    if any(word in user_message for word in ["ship", "delivery", "deliver", "shipping", "free shipping"]):
        bot_response = get_business_response("shipping")
    
    # Check for returns
    elif any(word in user_message for word in ["return", "exchange", "refund"]):
        bot_response = get_business_response("returns")
    
    # Check for products
    elif any(word in user_message for word in ["product", "sell", "offer", "item", "inventory", "have", "stock"]):
        bot_response = get_business_response("products")
    
    # Check for services
    elif any(word in user_message for word in ["service", "support", "repair", "maintenance", "warranty"]):
        bot_response = get_business_response("services")
    
    # Check for contact info
    elif any(word in user_message for word in ["contact", "phone", "call", "email", "reach"]):
        bot_response = get_business_response("contact")
    
    # Check for location
    elif any(word in user_message for word in ["where", "location", "address", "find", "directions"]):
        bot_response = get_business_response("location")
    
    # Check for business hours
    elif any(word in user_message for word in ["hour", "time", "open", "close", "when"]):
        bot_response = get_business_response("hours")
    
    # Check for company values
    elif any(word in user_message for word in ["value", "mission", "believe", "principle"]):
        bot_response = get_business_response("values")
    
    # Check for people
    elif any(word in user_message for word in ["who", "people", "team", "owner", "ceo", "manager"]):
        bot_response = get_business_response("people")
    
    # Check for payment methods
    elif any(word in user_message for word in ["pay", "payment", "credit", "debit", "cash", "finance"]):
        bot_response = get_business_response("payment")
    
    # Check for help or capabilities
    elif any(word in user_message for word in ["help", "what can you do", "how can you help", "capabilities", "what do you know"]):
        bot_response = get_business_response("capabilities")
    
    # Check for thanks
    elif any(word in user_message for word in ["thank", "appreciate", "thanks"]):
        bot_response = get_business_response("thanks")
    
    # Check for goodbye
    elif any(word in user_message for word in ["bye", "goodbye", "see you", "farewell"]):
        bot_response = get_business_response("goodbye")
    
    # Check for greetings (moved to end to avoid conflicts)
    elif any(word in user_message for word in ["hi", "hello", "hey", "greetings", "howdy"]):
        bot_response = get_business_response("greeting")
    
    # Handle product inquiries
    elif any(word in user_message for word in ["smartphone", "phone", "iphone", "android"]):
        if "pro" in user_message or "premium" in user_message or "high-end" in user_message:
            bot_response = handle_product_inquiry("smartphone", "pro")
        elif "basic" in user_message or "cheap" in user_message or "budget" in user_message:
            bot_response = handle_product_inquiry("smartphone", "basic")
        else:
            bot_response = handle_product_inquiry("smartphone")
    
    elif any(word in user_message for word in ["laptop", "computer", "notebook", "macbook", "pc"]):
        if "pro" in user_message or "premium" in user_message or "high-end" in user_message:
            bot_response = handle_product_inquiry("laptop", "premium")
        elif "standard" in user_message or "basic" in user_message or "budget" in user_message:
            bot_response = handle_product_inquiry("laptop", "standard")
        else:
            bot_response = handle_product_inquiry("laptop")
    
    elif any(word in user_message for word in ["tablet", "ipad", "android tablet"]):
        bot_response = "We offer a variety of tablets from leading brands. Would you like information about Apple iPads or Android tablets?"
    
    elif any(word in user_message for word in ["accessory", "accessories", "case", "charger", "headphone"]):
        bot_response = "We carry a wide range of accessories including cases, chargers, headphones, and more. Is there a specific accessory you're interested in?"
    
    # For all other questions, use the AI module
    else:
        bot_response = ai_assistant.generate_response(user_message, conversation_history)
        # If the AI module returns a generic response, try to provide something more specific
        if "thank you for your question" in bot_response.lower():
            if "?" in user_message or "what" in user_message or "how" in user_message:
                bot_response = f"I'd be happy to help! At {BUSINESS_CONFIG['name']}, we offer {BUSINESS_CONFIG['products']}. Is there a specific product or service you're interested in?"
            else:
                bot_response = "How can I assist you today? I can help with product information, store details, or technical support."

    # Add a small delay to make it feel more natural
    time.sleep(0.3)
    
    # Append to chat history using the new Messages format
    chat_history.append({"role": "user", "content": message})
    chat_history.append({"role": "assistant", "content": bot_response})
    
    return "", chat_history

# Simple CSS for basic styling
custom_css = """
#chatbot {
    height: 400px;
    overflow-y: auto;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 15px;
}
.gr-button {
    background-color: #4CAF50 !important;
    color: white !important;
}
.send-button {
    background-color: #2196F3 !important;
}
.clear-button {
    background-color: #f44336 !important;
}
"""

with gr.Blocks(css=custom_css, title=f"{BUSINESS_CONFIG['name']} Support") as demo:
    with gr.Column():
        gr.Markdown(f"# 🤖 {BUSINESS_CONFIG['name']} Support Chat")
        
        # Plain text instructions - no boxes
        gr.Markdown("### 💡 How to Use This Chatbot")
        gr.Markdown("This AI assistant can help you with product information, store policies, technical support, and more. Just type your question in the chat box or click on any of the example questions to get started.")
        
        gr.Markdown("### ✅ What I Can Help With")
        gr.Markdown("""
        • Product information, specifications, and pricing
        • Store hours, location, and contact information
        • Shipping options and return policies
        • Technical support and troubleshooting
        • Product comparisons and recommendations
        • Service information and repair options
        """)
        
        gr.Markdown("### ⚠️ Limitations")
        gr.Markdown("""
        • I can't process orders or payments directly
        • I don't have access to your personal account information
        • For complex issues, I may connect you with a human specialist
        • My knowledge is based on information available up to my last training
        """)
        
        with gr.Row():
            with gr.Column(scale=2):
                gr.Markdown("### 🏪 Store Information")
                gr.Markdown(f"""
                **Hours**: {BUSINESS_CONFIG['hours']}  
                **Address**: {BUSINESS_CONFIG['address']}  
                **Phone**: {BUSINESS_CONFIG['phone']}  
                **Email**: {BUSINESS_CONFIG['email']}  
                **Shipping**: {BUSINESS_CONFIG['shipping']}
                """)
        
        gr.Markdown("---")
        
        with gr.Row():
            with gr.Column(scale=2):
                chatbot = gr.Chatbot(
                    label="Conversation", 
                    elem_id="chatbot",
                    type="messages",
                    show_copy_button=True
                )
                with gr.Row():
                    msg = gr.Textbox(
                        label="Type your message here...",
                        placeholder="Ask me about products, services, or anything else...",
                        lines=1,
                        scale=4
                    )
                    send_btn = gr.Button("Send", variant="primary", elem_classes="send-button", scale=1)
                clear = gr.Button("Clear Conversation", variant="stop", elem_classes="clear-button")
                
            with gr.Column(scale=1):
                gr.Markdown("### 💡 Common Questions")
                
                examples = gr.Examples(
                    examples=[
                        ["What products do you have?"],
                        ["Do you offer free shipping?"],
                        ["What are your store hours?"],
                        ["What's your return policy?"],
                        ["Can you compare your laptop models?"],
                        ["What services do you provide?"],
                        ["What payment methods do you accept?"]
                    ],
                    inputs=msg,
                    label="Click any question to try it!",
                    examples_per_page=7
                )
        
        # Event handlers
        msg.submit(chat_with_bot, [msg, chatbot], [msg, chatbot])
        send_btn.click(chat_with_bot, [msg, chatbot], [msg, chatbot])
        clear.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    demo.launch(share=True)