Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import time
|
4 |
-
import requests
|
5 |
-
import json
|
6 |
|
7 |
# Business information - customize this for your business
|
8 |
BUSINESS_INFO = {
|
@@ -28,19 +26,6 @@ PRODUCTS = {
|
|
28 |
}
|
29 |
}
|
30 |
|
31 |
-
# Hugging Face model API settings (for more complex queries)
|
32 |
-
API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
|
33 |
-
API_TOKEN = "your_hugging_face_api_token_here" # You'll need to get this from Hugging Face
|
34 |
-
|
35 |
-
def query_ai_model(payload):
|
36 |
-
"""Query the Hugging Face model for more complex questions"""
|
37 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
38 |
-
try:
|
39 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
40 |
-
return response.json()
|
41 |
-
except:
|
42 |
-
return {"error": "Model is currently loading, please try again in a few seconds."}
|
43 |
-
|
44 |
# Predefined responses for common queries
|
45 |
def get_business_response(intent):
|
46 |
responses = {
|
@@ -49,11 +34,12 @@ def get_business_response(intent):
|
|
49 |
"contact": f"You can reach us at {BUSINESS_INFO['phone']} or email {BUSINESS_INFO['email']}.",
|
50 |
"products": f"We offer a wide range of {BUSINESS_INFO['products']}. What specifically are you interested in?",
|
51 |
"returns": f"Our return policy: {BUSINESS_INFO['return_policy']}.",
|
52 |
-
"shipping": f"Shipping info: {BUSINESS_INFO['shipping']}.",
|
53 |
"greeting": "Hello! Welcome to TechGadget Store. How can I help you today?",
|
54 |
"thanks": "You're welcome! Is there anything else I can help you with?",
|
55 |
"goodbye": "Thank you for contacting TechGadget Store. Have a great day!",
|
56 |
-
"help": "I can help you with
|
|
|
57 |
}
|
58 |
return responses.get(intent, "I'm not sure how to help with that. Can you please rephrase?")
|
59 |
|
@@ -113,12 +99,12 @@ def chat_with_bot(message, chat_history):
|
|
113 |
bot_response = get_business_response("returns")
|
114 |
|
115 |
# Check for shipping
|
116 |
-
elif any(word in user_message for word in ["ship", "delivery", "deliver"]):
|
117 |
bot_response = get_business_response("shipping")
|
118 |
|
119 |
-
# Check for help
|
120 |
-
elif any(word in user_message for word in ["help", "what can you do"]):
|
121 |
-
bot_response = get_business_response("
|
122 |
|
123 |
# Handle product inquiries
|
124 |
elif any(word in user_message for word in ["smartphone", "phone"]):
|
@@ -137,34 +123,9 @@ def chat_with_bot(message, chat_history):
|
|
137 |
else:
|
138 |
bot_response = handle_product_inquiry("laptop")
|
139 |
|
140 |
-
#
|
141 |
else:
|
142 |
-
|
143 |
-
context = f"You are a customer service representative for {BUSINESS_INFO['name']}, " \
|
144 |
-
f"a store that sells {BUSINESS_INFO['products']}. " \
|
145 |
-
f"Be helpful and friendly in your responses. " \
|
146 |
-
f"If asked about something not related to the business, " \
|
147 |
-
f"politely steer the conversation back to how you can help with technology products."
|
148 |
-
|
149 |
-
# Prepare the conversation history for the model
|
150 |
-
formatted_history = ""
|
151 |
-
for i, (human, ai) in enumerate(chat_history[-5:]): # Use last 5 exchanges for context
|
152 |
-
formatted_history += f"Human: {human}\nAI: {ai}\n"
|
153 |
-
|
154 |
-
full_prompt = f"{context}\n\n{formatted_history}Human: {message}\nAI:"
|
155 |
-
|
156 |
-
# Query the AI model
|
157 |
-
try:
|
158 |
-
model_response = query_ai_model({"inputs": full_prompt})
|
159 |
-
|
160 |
-
if "generated_text" in model_response:
|
161 |
-
bot_response = model_response["generated_text"]
|
162 |
-
elif "error" in model_response:
|
163 |
-
bot_response = "I'm currently processing your question. For complex technical questions, I might need a moment to think. In the meantime, is there anything else about our products I can help with?"
|
164 |
-
else:
|
165 |
-
bot_response = "That's an interesting question! As a technology store assistant, I'd recommend checking our product specifications for detailed technical information."
|
166 |
-
except:
|
167 |
-
bot_response = "I'm here to help with information about our products, store hours, location, and policies. How can I assist you today?"
|
168 |
|
169 |
# Add a small delay to make it feel more natural
|
170 |
time.sleep(0.5)
|
@@ -174,6 +135,10 @@ def chat_with_bot(message, chat_history):
|
|
174 |
|
175 |
return "", chat_history
|
176 |
|
|
|
|
|
|
|
|
|
177 |
# Custom CSS for styling
|
178 |
custom_css = """
|
179 |
#chatbot {
|
@@ -209,6 +174,17 @@ h1 {
|
|
209 |
margin: auto;
|
210 |
padding: 20px;
|
211 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
"""
|
213 |
|
214 |
# Create the Gradio interface
|
@@ -232,25 +208,31 @@ with gr.Blocks(css=custom_css, title="TechGadget Store Support") as demo:
|
|
232 |
|
233 |
with gr.Column(scale=1):
|
234 |
gr.Markdown("### 💡 Common Questions")
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
[
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
248 |
gr.Markdown("### 🏪 Store Information")
|
249 |
gr.Markdown(f"""
|
250 |
- **Hours**: {BUSINESS_INFO['hours']}
|
251 |
- **Address**: {BUSINESS_INFO['address']}
|
252 |
- **Phone**: {BUSINESS_INFO['phone']}
|
253 |
- **Email**: {BUSINESS_INFO['email']}
|
|
|
254 |
""")
|
255 |
|
256 |
# Event handlers
|
@@ -258,16 +240,6 @@ with gr.Blocks(css=custom_css, title="TechGadget Store Support") as demo:
|
|
258 |
send_btn.click(chat_with_bot, [msg, chatbot], [msg, chatbot])
|
259 |
clear.click(lambda: None, None, chatbot, queue=False)
|
260 |
|
261 |
-
# Instructions for setting up the AI model
|
262 |
-
gr.Markdown("""
|
263 |
-
### To enable the AI model for complex questions:
|
264 |
-
|
265 |
-
1. Create a Hugging Face account at [huggingface.co](https://huggingface.co/)
|
266 |
-
2. Get your API token from [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
|
267 |
-
3. Replace `your_hugging_face_api_token_here` in the code with your actual token
|
268 |
-
4. The model will handle complex technical questions beyond the predefined responses
|
269 |
-
""")
|
270 |
-
|
271 |
# Launch the application
|
272 |
if __name__ == "__main__":
|
273 |
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import time
|
|
|
|
|
4 |
|
5 |
# Business information - customize this for your business
|
6 |
BUSINESS_INFO = {
|
|
|
26 |
}
|
27 |
}
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Predefined responses for common queries
|
30 |
def get_business_response(intent):
|
31 |
responses = {
|
|
|
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']}. We offer free shipping on orders over $50!",
|
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 |
+
"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?",
|
42 |
+
"capabilities": "I'm your virtual assistant for TechGadget Store! 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?"
|
43 |
}
|
44 |
return responses.get(intent, "I'm not sure how to help with that. Can you please rephrase?")
|
45 |
|
|
|
99 |
bot_response = get_business_response("returns")
|
100 |
|
101 |
# Check for shipping
|
102 |
+
elif any(word in user_message for word in ["ship", "delivery", "deliver", "shipping", "free shipping"]):
|
103 |
bot_response = get_business_response("shipping")
|
104 |
|
105 |
+
# Check for help or capabilities
|
106 |
+
elif any(word in user_message for word in ["help", "what can you do", "how can you help", "capabilities", "what do you know"]):
|
107 |
+
bot_response = get_business_response("capabilities")
|
108 |
|
109 |
# Handle product inquiries
|
110 |
elif any(word in user_message for word in ["smartphone", "phone"]):
|
|
|
123 |
else:
|
124 |
bot_response = handle_product_inquiry("laptop")
|
125 |
|
126 |
+
# Default response for unrecognized queries
|
127 |
else:
|
128 |
+
bot_response = "I'm here to help with information about our products, store hours, location, and policies. How can I assist you today?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
# Add a small delay to make it feel more natural
|
131 |
time.sleep(0.5)
|
|
|
135 |
|
136 |
return "", chat_history
|
137 |
|
138 |
+
# Function to handle example clicks
|
139 |
+
def example_click(example):
|
140 |
+
return example
|
141 |
+
|
142 |
# Custom CSS for styling
|
143 |
custom_css = """
|
144 |
#chatbot {
|
|
|
174 |
margin: auto;
|
175 |
padding: 20px;
|
176 |
}
|
177 |
+
.example-btn {
|
178 |
+
margin: 5px;
|
179 |
+
padding: 8px 12px;
|
180 |
+
border-radius: 5px;
|
181 |
+
background-color: #f0f0f0;
|
182 |
+
border: 1px solid #ddd;
|
183 |
+
cursor: pointer;
|
184 |
+
}
|
185 |
+
.example-btn:hover {
|
186 |
+
background-color: #e0e0e0;
|
187 |
+
}
|
188 |
"""
|
189 |
|
190 |
# Create the Gradio interface
|
|
|
208 |
|
209 |
with gr.Column(scale=1):
|
210 |
gr.Markdown("### 💡 Common Questions")
|
211 |
+
|
212 |
+
# Create example buttons that properly update the message box
|
213 |
+
with gr.Row():
|
214 |
+
gr.Examples(
|
215 |
+
examples=[
|
216 |
+
["What are your store hours?"],
|
217 |
+
["Where are you located?"],
|
218 |
+
["What smartphones do you sell?"],
|
219 |
+
["What is your return policy?"],
|
220 |
+
["Do you offer free shipping?"],
|
221 |
+
["What can you help with?"],
|
222 |
+
["Tell me about your laptops"]
|
223 |
+
],
|
224 |
+
inputs=msg,
|
225 |
+
label="Click any question to try it!",
|
226 |
+
elem_classes="example-btn"
|
227 |
+
)
|
228 |
+
|
229 |
gr.Markdown("### 🏪 Store Information")
|
230 |
gr.Markdown(f"""
|
231 |
- **Hours**: {BUSINESS_INFO['hours']}
|
232 |
- **Address**: {BUSINESS_INFO['address']}
|
233 |
- **Phone**: {BUSINESS_INFO['phone']}
|
234 |
- **Email**: {BUSINESS_INFO['email']}
|
235 |
+
- **Shipping**: {BUSINESS_INFO['shipping']}
|
236 |
""")
|
237 |
|
238 |
# Event handlers
|
|
|
240 |
send_btn.click(chat_with_bot, [msg, chatbot], [msg, chatbot])
|
241 |
clear.click(lambda: None, None, chatbot, queue=False)
|
242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
# Launch the application
|
244 |
if __name__ == "__main__":
|
245 |
demo.launch(share=True)
|