Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,25 @@
|
|
1 |
import gradio as gr
|
2 |
import time
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
6 |
"name": "TechGadget Store",
|
|
|
7 |
"hours": "9AM-6PM Monday to Saturday",
|
8 |
"products": "smartphones, laptops, tablets, and accessories",
|
9 |
"address": "123 Main Street, Tech City",
|
10 |
"phone": "(555) 123-4567",
|
11 |
"email": "[email protected]",
|
12 |
"return_policy": "30-day money-back guarantee on all products",
|
13 |
-
"shipping": "Free shipping on orders over $50"
|
|
|
|
|
14 |
}
|
15 |
|
16 |
-
# Sample product database
|
17 |
PRODUCTS = {
|
18 |
"smartphone": {
|
19 |
"basic": {"name": "BasicPhone", "price": "$199", "features": "6.1\" display, 128GB storage"},
|
@@ -25,20 +31,89 @@ PRODUCTS = {
|
|
25 |
}
|
26 |
}
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
# Predefined responses for common queries
|
29 |
def get_business_response(intent):
|
30 |
responses = {
|
31 |
-
"hours": f"Our store hours are {
|
32 |
-
"location": f"We're located at {
|
33 |
-
"contact": f"You can reach us at {
|
34 |
-
"products": f"We offer a wide range of {
|
35 |
-
"returns": f"Our return policy: {
|
36 |
-
"shipping": f"Yes! We offer {
|
37 |
-
"greeting": "Hello! Welcome to
|
38 |
"thanks": "You're welcome! Is there anything else I can help you with?",
|
39 |
-
"goodbye": "Thank you for contacting
|
40 |
"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?",
|
41 |
-
"capabilities": "I'm your virtual assistant for
|
|
|
|
|
42 |
}
|
43 |
return responses.get(intent, "I'm not sure how to help with that. Can you please rephrase?")
|
44 |
|
@@ -60,11 +135,19 @@ def handle_product_inquiry(product_type, model=None):
|
|
60 |
response += f"\n- {model_name}: {details['name']} ({details['price']})"
|
61 |
return response
|
62 |
|
63 |
-
# Main chat function
|
64 |
def chat_with_bot(message, chat_history):
|
65 |
user_message = message.lower()
|
66 |
bot_response = ""
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
# Check for shipping first (before greetings to avoid "hi" in "shipping" triggering greeting)
|
69 |
if any(word in user_message for word in ["ship", "delivery", "deliver", "shipping", "free shipping"]):
|
70 |
bot_response = get_business_response("shipping")
|
@@ -89,6 +172,14 @@ def chat_with_bot(message, chat_history):
|
|
89 |
elif any(word in user_message for word in ["hour", "time", "open", "close"]):
|
90 |
bot_response = get_business_response("hours")
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
# Check for help or capabilities
|
93 |
elif any(word in user_message for word in ["help", "what can you do", "how can you help", "capabilities", "what do you know"]):
|
94 |
bot_response = get_business_response("capabilities")
|
@@ -122,9 +213,9 @@ def chat_with_bot(message, chat_history):
|
|
122 |
else:
|
123 |
bot_response = handle_product_inquiry("laptop")
|
124 |
|
125 |
-
#
|
126 |
else:
|
127 |
-
bot_response =
|
128 |
|
129 |
# Add a small delay to make it feel more natural
|
130 |
time.sleep(0.3)
|
@@ -135,7 +226,8 @@ def chat_with_bot(message, chat_history):
|
|
135 |
|
136 |
return "", chat_history
|
137 |
|
138 |
-
#
|
|
|
139 |
custom_css = """
|
140 |
#chatbot {
|
141 |
font-family: 'Arial', sans-serif;
|
@@ -172,15 +264,13 @@ h1 {
|
|
172 |
}
|
173 |
"""
|
174 |
|
175 |
-
# Create the Gradio interface
|
176 |
with gr.Blocks(css=custom_css, title="TechGadget Store Support") as demo:
|
177 |
with gr.Column(elem_id="container"):
|
178 |
-
gr.Markdown("# 🤖
|
179 |
gr.Markdown("### How can I help you with our products and services today?")
|
180 |
|
181 |
with gr.Row():
|
182 |
with gr.Column(scale=2):
|
183 |
-
# Updated to use the new Messages format
|
184 |
chatbot = gr.Chatbot(
|
185 |
label="Conversation",
|
186 |
elem_id="chatbot",
|
@@ -190,7 +280,7 @@ with gr.Blocks(css=custom_css, title="TechGadget Store Support") as demo:
|
|
190 |
with gr.Row():
|
191 |
msg = gr.Textbox(
|
192 |
label="Type your message here...",
|
193 |
-
placeholder="
|
194 |
lines=1,
|
195 |
scale=4
|
196 |
)
|
@@ -200,16 +290,15 @@ with gr.Blocks(css=custom_css, title="TechGadget Store Support") as demo:
|
|
200 |
with gr.Column(scale=1):
|
201 |
gr.Markdown("### 💡 Common Questions")
|
202 |
|
203 |
-
# Create example buttons with correct parameters
|
204 |
examples = gr.Examples(
|
205 |
examples=[
|
206 |
["Do you offer free shipping?"],
|
207 |
["What are your store hours?"],
|
208 |
-
["
|
209 |
-
["
|
210 |
-
["What
|
211 |
-
["
|
212 |
-
["
|
213 |
],
|
214 |
inputs=msg,
|
215 |
label="Click any question to try it!"
|
@@ -217,11 +306,11 @@ with gr.Blocks(css=custom_css, title="TechGadget Store Support") as demo:
|
|
217 |
|
218 |
gr.Markdown("### 🏪 Store Information")
|
219 |
gr.Markdown(f"""
|
220 |
-
- **Hours**: {
|
221 |
-
- **Address**: {
|
222 |
-
- **Phone**: {
|
223 |
-
- **Email**: {
|
224 |
-
- **Shipping**: {
|
225 |
""")
|
226 |
|
227 |
# Event handlers
|
@@ -229,6 +318,26 @@ with gr.Blocks(css=custom_css, title="TechGadget Store Support") as demo:
|
|
229 |
send_btn.click(chat_with_bot, [msg, chatbot], [msg, chatbot])
|
230 |
clear.click(lambda: None, None, chatbot, queue=False)
|
231 |
|
232 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
if __name__ == "__main__":
|
234 |
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
import time
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
|
6 |
+
# ==================== CONFIGURATION MODULE ====================
|
7 |
+
# Customize this section for your specific business
|
8 |
+
|
9 |
+
BUSINESS_CONFIG = {
|
10 |
"name": "TechGadget Store",
|
11 |
+
"industry": "technology retail",
|
12 |
"hours": "9AM-6PM Monday to Saturday",
|
13 |
"products": "smartphones, laptops, tablets, and accessories",
|
14 |
"address": "123 Main Street, Tech City",
|
15 |
"phone": "(555) 123-4567",
|
16 |
"email": "[email protected]",
|
17 |
"return_policy": "30-day money-back guarantee on all products",
|
18 |
+
"shipping": "Free shipping on orders over $50",
|
19 |
+
"values": "Quality products, excellent customer service, and competitive prices",
|
20 |
+
"key_people": "John Smith (CEO), Sarah Johnson (Head of Sales)"
|
21 |
}
|
22 |
|
|
|
23 |
PRODUCTS = {
|
24 |
"smartphone": {
|
25 |
"basic": {"name": "BasicPhone", "price": "$199", "features": "6.1\" display, 128GB storage"},
|
|
|
31 |
}
|
32 |
}
|
33 |
|
34 |
+
# ==================== AI MODULE ====================
|
35 |
+
# This module handles complex questions using business context
|
36 |
+
|
37 |
+
class BusinessAIAssistant:
|
38 |
+
def __init__(self, business_config):
|
39 |
+
self.business_config = business_config
|
40 |
+
self.context = self._create_context_prompt()
|
41 |
+
|
42 |
+
def _create_context_prompt(self):
|
43 |
+
"""Create a detailed context prompt for the AI"""
|
44 |
+
prompt = f"""You are a customer service representative for {self.business_config['name']},
|
45 |
+
a {self.business_config['industry']} business. Your role is to assist customers with questions
|
46 |
+
about products, policies, and services while maintaining a friendly and professional tone.
|
47 |
+
|
48 |
+
BUSINESS INFORMATION:
|
49 |
+
- Name: {self.business_config['name']}
|
50 |
+
- Industry: {self.business_config['industry']}
|
51 |
+
- Hours: {self.business_config['hours']}
|
52 |
+
- Address: {self.business_config['address']}
|
53 |
+
- Phone: {self.business_config['phone']}
|
54 |
+
- Email: {self.business_config['email']}
|
55 |
+
- Products: {self.business_config['products']}
|
56 |
+
- Return Policy: {self.business_config['return_policy']}
|
57 |
+
- Shipping: {self.business_config['shipping']}
|
58 |
+
- Values: {self.business_config['values']}
|
59 |
+
- Key People: {self.business_config['key_people']}
|
60 |
+
|
61 |
+
GUIDELINES:
|
62 |
+
1. Always be helpful, friendly, and professional
|
63 |
+
2. Stay on topic related to the business and its products/services
|
64 |
+
3. If asked about unrelated topics, politely steer back to business matters
|
65 |
+
4. For technical questions, provide accurate information based on product specs
|
66 |
+
5. If unsure about something, offer to connect the customer with a human representative
|
67 |
+
|
68 |
+
CURRENT CONVERSATION:
|
69 |
+
"""
|
70 |
+
return prompt
|
71 |
+
|
72 |
+
def generate_response(self, user_message, conversation_history=""):
|
73 |
+
"""
|
74 |
+
Generate a response to user message using business context
|
75 |
+
In a real implementation, this would connect to an AI API
|
76 |
+
"""
|
77 |
+
# Simulated AI response - in practice, you would call an AI API here
|
78 |
+
# For example: Hugging Face, OpenAI, or another LLM provider
|
79 |
+
|
80 |
+
# This is a simplified simulation - real implementation would use API calls
|
81 |
+
if "technical" in user_message.lower() or "spec" in user_message.lower():
|
82 |
+
return "I'd be happy to help with technical questions! Based on our product specifications, I can provide detailed information about features and compatibility."
|
83 |
+
|
84 |
+
elif "recommend" in user_message.lower() or "suggest" in user_message.lower():
|
85 |
+
return "Based on your needs, I'd recommend considering our premium lineup for the best performance, or our standard options for great value."
|
86 |
+
|
87 |
+
elif "compare" in user_message.lower():
|
88 |
+
return "I can help you compare our products! Our premium options generally offer better performance, more storage, and higher quality displays than our standard models."
|
89 |
+
|
90 |
+
elif "problem" in user_message.lower() or "issue" in user_message.lower():
|
91 |
+
return "I'm sorry to hear you're experiencing an issue. Let me help you troubleshoot, or I can connect you with our technical support team if needed."
|
92 |
+
|
93 |
+
else:
|
94 |
+
return "Thank you for your question. As a representative of {business_name}, I'll do my best to provide accurate information about our products and services. How else can I assist you today?".format(business_name=self.business_config['name'])
|
95 |
+
|
96 |
+
# Initialize the AI assistant
|
97 |
+
ai_assistant = BusinessAIAssistant(BUSINESS_CONFIG)
|
98 |
+
|
99 |
+
# ==================== CHATBOT LOGIC ====================
|
100 |
+
|
101 |
# Predefined responses for common queries
|
102 |
def get_business_response(intent):
|
103 |
responses = {
|
104 |
+
"hours": f"Our store hours are {BUSINESS_CONFIG['hours']}.",
|
105 |
+
"location": f"We're located at {BUSINESS_CONFIG['address']}. Come visit us!",
|
106 |
+
"contact": f"You can reach us at {BUSINESS_CONFIG['phone']} or email {BUSINESS_CONFIG['email']}.",
|
107 |
+
"products": f"We offer a wide range of {BUSINESS_CONFIG['products']}. What specifically are you interested in?",
|
108 |
+
"returns": f"Our return policy: {BUSINESS_CONFIG['return_policy']}.",
|
109 |
+
"shipping": f"Yes! We offer {BUSINESS_CONFIG['shipping'].lower()}",
|
110 |
+
"greeting": "Hello! Welcome to {name}. How can I help you today?".format(name=BUSINESS_CONFIG['name']),
|
111 |
"thanks": "You're welcome! Is there anything else I can help you with?",
|
112 |
+
"goodbye": "Thank you for contacting {name}. Have a great day!".format(name=BUSINESS_CONFIG['name']),
|
113 |
"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?",
|
114 |
+
"capabilities": "I'm your virtual assistant for {name}! I can: \n• Answer questions about our products \n• Provide store information and hours \n• Explain our shipping and return policies \n• Help you compare different tech products \n• Assist with basic technical advice \n\nWhat would you like to know?".format(name=BUSINESS_CONFIG['name']),
|
115 |
+
"values": f"Our company values: {BUSINESS_CONFIG['values']}",
|
116 |
+
"people": f"Key people at our company: {BUSINESS_CONFIG['key_people']}"
|
117 |
}
|
118 |
return responses.get(intent, "I'm not sure how to help with that. Can you please rephrase?")
|
119 |
|
|
|
135 |
response += f"\n- {model_name}: {details['name']} ({details['price']})"
|
136 |
return response
|
137 |
|
138 |
+
# Main chat function
|
139 |
def chat_with_bot(message, chat_history):
|
140 |
user_message = message.lower()
|
141 |
bot_response = ""
|
142 |
|
143 |
+
# Format conversation history for context
|
144 |
+
conversation_history = ""
|
145 |
+
for entry in chat_history:
|
146 |
+
if entry['role'] == 'user':
|
147 |
+
conversation_history += f"Customer: {entry['content']}\n"
|
148 |
+
else:
|
149 |
+
conversation_history += f"Assistant: {entry['content']}\n"
|
150 |
+
|
151 |
# Check for shipping first (before greetings to avoid "hi" in "shipping" triggering greeting)
|
152 |
if any(word in user_message for word in ["ship", "delivery", "deliver", "shipping", "free shipping"]):
|
153 |
bot_response = get_business_response("shipping")
|
|
|
172 |
elif any(word in user_message for word in ["hour", "time", "open", "close"]):
|
173 |
bot_response = get_business_response("hours")
|
174 |
|
175 |
+
# Check for company values
|
176 |
+
elif any(word in user_message for word in ["value", "mission", "believe"]):
|
177 |
+
bot_response = get_business_response("values")
|
178 |
+
|
179 |
+
# Check for people
|
180 |
+
elif any(word in user_message for word in ["who", "people", "team", "owner", "ceo"]):
|
181 |
+
bot_response = get_business_response("people")
|
182 |
+
|
183 |
# Check for help or capabilities
|
184 |
elif any(word in user_message for word in ["help", "what can you do", "how can you help", "capabilities", "what do you know"]):
|
185 |
bot_response = get_business_response("capabilities")
|
|
|
213 |
else:
|
214 |
bot_response = handle_product_inquiry("laptop")
|
215 |
|
216 |
+
# For all other questions, use the AI module
|
217 |
else:
|
218 |
+
bot_response = ai_assistant.generate_response(user_message, conversation_history)
|
219 |
|
220 |
# Add a small delay to make it feel more natural
|
221 |
time.sleep(0.3)
|
|
|
226 |
|
227 |
return "", chat_history
|
228 |
|
229 |
+
# ==================== GRADIO INTERFACE ====================
|
230 |
+
|
231 |
custom_css = """
|
232 |
#chatbot {
|
233 |
font-family: 'Arial', sans-serif;
|
|
|
264 |
}
|
265 |
"""
|
266 |
|
|
|
267 |
with gr.Blocks(css=custom_css, title="TechGadget Store Support") as demo:
|
268 |
with gr.Column(elem_id="container"):
|
269 |
+
gr.Markdown(f"# 🤖 {BUSINESS_CONFIG['name']} Support Chat")
|
270 |
gr.Markdown("### How can I help you with our products and services today?")
|
271 |
|
272 |
with gr.Row():
|
273 |
with gr.Column(scale=2):
|
|
|
274 |
chatbot = gr.Chatbot(
|
275 |
label="Conversation",
|
276 |
elem_id="chatbot",
|
|
|
280 |
with gr.Row():
|
281 |
msg = gr.Textbox(
|
282 |
label="Type your message here...",
|
283 |
+
placeholder="Ask me about products, shipping, or technical details...",
|
284 |
lines=1,
|
285 |
scale=4
|
286 |
)
|
|
|
290 |
with gr.Column(scale=1):
|
291 |
gr.Markdown("### 💡 Common Questions")
|
292 |
|
|
|
293 |
examples = gr.Examples(
|
294 |
examples=[
|
295 |
["Do you offer free shipping?"],
|
296 |
["What are your store hours?"],
|
297 |
+
["What's your return policy?"],
|
298 |
+
["Can you compare your laptop models?"],
|
299 |
+
["What technical specifications do your phones have?"],
|
300 |
+
["Who are the key people at your company?"],
|
301 |
+
["What values does your company uphold?"]
|
302 |
],
|
303 |
inputs=msg,
|
304 |
label="Click any question to try it!"
|
|
|
306 |
|
307 |
gr.Markdown("### 🏪 Store Information")
|
308 |
gr.Markdown(f"""
|
309 |
+
- **Hours**: {BUSINESS_CONFIG['hours']}
|
310 |
+
- **Address**: {BUSINESS_CONFIG['address']}
|
311 |
+
- **Phone**: {BUSINESS_CONFIG['phone']}
|
312 |
+
- **Email**: {BUSINESS_CONFIG['email']}
|
313 |
+
- **Shipping**: {BUSINESS_CONFIG['shipping']}
|
314 |
""")
|
315 |
|
316 |
# Event handlers
|
|
|
318 |
send_btn.click(chat_with_bot, [msg, chatbot], [msg, chatbot])
|
319 |
clear.click(lambda: None, None, chatbot, queue=False)
|
320 |
|
321 |
+
# ==================== INSTRUCTIONS ====================
|
322 |
+
|
323 |
+
gr.Markdown("""
|
324 |
+
### Implementation Notes:
|
325 |
+
|
326 |
+
1. **AI Integration**: The `BusinessAIAssistant` class is designed to connect to any AI API. Currently, it uses simulated responses, but you can easily extend it to use:
|
327 |
+
- Hugging Face Inference API
|
328 |
+
- OpenAI GPT models
|
329 |
+
- Anthropic Claude
|
330 |
+
- Or any other LLM provider
|
331 |
+
|
332 |
+
2. **Customization**: Modify the `BUSINESS_CONFIG` dictionary at the top to match any business. The AI module will automatically adapt to the new context.
|
333 |
+
|
334 |
+
3. **Extending Functionality**: To add more capabilities:
|
335 |
+
- Add new patterns to the response logic in `chat_with_bot()`
|
336 |
+
- Extend the `BusinessAIAssistant` class with specialized methods
|
337 |
+
- Add more products to the `PRODUCTS` dictionary
|
338 |
+
|
339 |
+
4. **API Integration**: To connect to a real AI API, replace the `generate_response` method with actual API calls to your preferred provider.
|
340 |
+
""")
|
341 |
+
|
342 |
if __name__ == "__main__":
|
343 |
demo.launch(share=True)
|