Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -136,4 +136,196 @@ html_code = """
|
|
| 136 |
function showMenu() {
|
| 137 |
document.getElementById('menuPage').style.display = 'block';
|
| 138 |
const categories = {{ MENU | tojson }};
|
| 139 |
-
let menuText = "We
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
function showMenu() {
|
| 137 |
document.getElementById('menuPage').style.display = 'block';
|
| 138 |
const categories = {{ MENU | tojson }};
|
| 139 |
+
let menuText = "We have the following categories: ";
|
| 140 |
+
for (let category in categories) {
|
| 141 |
+
menuText += category + " ";
|
| 142 |
+
}
|
| 143 |
+
document.getElementById('status').innerText = menuText;
|
| 144 |
+
}
|
| 145 |
+
</script>
|
| 146 |
+
</body>
|
| 147 |
+
</html>
|
| 148 |
+
"""
|
| 149 |
+
|
| 150 |
+
@app.route("/")
|
| 151 |
+
def index():
|
| 152 |
+
return render_template_string(html_code)
|
| 153 |
+
|
| 154 |
+
# Register Page
|
| 155 |
+
@app.route("/register", methods=["GET", "POST"])
|
| 156 |
+
def register():
|
| 157 |
+
if request.method == "POST":
|
| 158 |
+
name = request.form.get('name')
|
| 159 |
+
email = request.form.get('email')
|
| 160 |
+
phone = request.form.get('phone')
|
| 161 |
+
if not name or not email or not phone:
|
| 162 |
+
logging.error("Missing required fields: Name, Email, Phone.")
|
| 163 |
+
return jsonify({"error": "All fields are required."}), 400
|
| 164 |
+
|
| 165 |
+
if not sf:
|
| 166 |
+
logging.error("Salesforce connection failed.")
|
| 167 |
+
return jsonify({"error": "Salesforce connection failed."}), 500
|
| 168 |
+
|
| 169 |
+
# Create a record in Salesforce
|
| 170 |
+
try:
|
| 171 |
+
customer_login = sf.Customer_Login__c.create({
|
| 172 |
+
'Name': name,
|
| 173 |
+
'Email__c': email,
|
| 174 |
+
'Phone_Number__c': phone
|
| 175 |
+
})
|
| 176 |
+
logging.info(f"Customer registered successfully with ID: {customer_login['id']}")
|
| 177 |
+
return redirect(url_for('menu')) # Redirect to menu after successful registration
|
| 178 |
+
except Exception as e:
|
| 179 |
+
logging.error(f"Error during registration: {str(e)}")
|
| 180 |
+
return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
|
| 181 |
+
|
| 182 |
+
return render_template_string(html_code)
|
| 183 |
+
|
| 184 |
+
# Login Page
|
| 185 |
+
@app.route("/login", methods=["GET", "POST"])
|
| 186 |
+
def login():
|
| 187 |
+
if request.method == "POST":
|
| 188 |
+
email = request.form.get('email')
|
| 189 |
+
phone = request.form.get('phone')
|
| 190 |
+
|
| 191 |
+
if not email or not phone:
|
| 192 |
+
logging.error("Missing required fields: Email, Phone.")
|
| 193 |
+
return jsonify({"error": "Email and phone are required."}), 400
|
| 194 |
+
|
| 195 |
+
if not sf:
|
| 196 |
+
logging.error("Salesforce connection failed.")
|
| 197 |
+
return jsonify({"error": "Salesforce connection failed."}), 500
|
| 198 |
+
|
| 199 |
+
# Check if the customer exists in Salesforce
|
| 200 |
+
try:
|
| 201 |
+
customer_query = sf.query(f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone}'")
|
| 202 |
+
if customer_query['totalSize'] == 0:
|
| 203 |
+
logging.error("Customer not found.")
|
| 204 |
+
return jsonify({"error": "Customer not found."}), 404
|
| 205 |
+
logging.info("Customer logged in successfully")
|
| 206 |
+
return redirect(url_for('menu')) # Redirect to menu after successful login
|
| 207 |
+
except Exception as e:
|
| 208 |
+
logging.error(f"Error during login: {str(e)}")
|
| 209 |
+
return jsonify({'error': f'Failed to query Salesforce: {str(e)}'}), 500
|
| 210 |
+
|
| 211 |
+
return render_template_string(html_code)
|
| 212 |
+
|
| 213 |
+
# Menu Page
|
| 214 |
+
@app.route("/menu")
|
| 215 |
+
def menu():
|
| 216 |
+
return render_template_string(html_code)
|
| 217 |
+
|
| 218 |
+
@app.route("/process-audio", methods=["POST"])
|
| 219 |
+
def process_audio():
|
| 220 |
+
global current_category, current_item, awaiting_quantity
|
| 221 |
+
try:
|
| 222 |
+
audio_file = request.files.get("audio")
|
| 223 |
+
if not audio_file:
|
| 224 |
+
raise BadRequest("No audio file provided.")
|
| 225 |
+
|
| 226 |
+
temp_file = NamedTemporaryFile(delete=False, suffix=".webm")
|
| 227 |
+
audio_file.save(temp_file.name)
|
| 228 |
+
|
| 229 |
+
if os.path.getsize(temp_file.name) == 0:
|
| 230 |
+
raise BadRequest("Uploaded audio file is empty.")
|
| 231 |
+
|
| 232 |
+
converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
|
| 233 |
+
ffmpeg.input(temp_file.name).output(
|
| 234 |
+
converted_file.name, acodec="pcm_s16le", ac=1, ar="16000"
|
| 235 |
+
).run(overwrite_output=True)
|
| 236 |
+
|
| 237 |
+
recognizer = sr.Recognizer()
|
| 238 |
+
with sr.AudioFile(converted_file.name) as source:
|
| 239 |
+
audio_data = recognizer.record(source)
|
| 240 |
+
try:
|
| 241 |
+
command = recognizer.recognize_google(audio_data)
|
| 242 |
+
logging.info(f"Recognized command: {command}")
|
| 243 |
+
response = process_command(command)
|
| 244 |
+
except sr.UnknownValueError:
|
| 245 |
+
response = "Sorry, I couldn't understand your command. Could you please repeat?"
|
| 246 |
+
except sr.RequestError as e:
|
| 247 |
+
response = f"Error with the speech recognition service: {e}"
|
| 248 |
+
|
| 249 |
+
return jsonify({"response": response})
|
| 250 |
+
|
| 251 |
+
except BadRequest as br:
|
| 252 |
+
return jsonify({"response": f"Bad Request: {str(br)}"}), 400
|
| 253 |
+
except Exception as e:
|
| 254 |
+
logging.error(f"Error processing audio: {str(e)}")
|
| 255 |
+
return jsonify({"response": f"An error occurred: {str(e)}"}), 500
|
| 256 |
+
finally:
|
| 257 |
+
os.unlink(temp_file.name)
|
| 258 |
+
os.unlink(converted_file.name)
|
| 259 |
+
|
| 260 |
+
def process_command(command):
|
| 261 |
+
global cart, MENU, current_category, current_item, awaiting_quantity
|
| 262 |
+
command = command.lower()
|
| 263 |
+
|
| 264 |
+
# Handle quantity input
|
| 265 |
+
if awaiting_quantity:
|
| 266 |
+
quantity = extract_quantity(command)
|
| 267 |
+
if quantity:
|
| 268 |
+
cart.append((current_item, MENU[current_category][current_item], quantity))
|
| 269 |
+
awaiting_quantity = False
|
| 270 |
+
item = current_item
|
| 271 |
+
current_item = None
|
| 272 |
+
total = sum(i[1] * i[2] for i in cart)
|
| 273 |
+
cart_summary = ", ".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
|
| 274 |
+
return f"Added {quantity} x {item} to your cart. Your current cart: {cart_summary}. Total: ₹{total}. Would you like to add more items?"
|
| 275 |
+
else:
|
| 276 |
+
return "Sorry, I couldn't understand the quantity. Please provide a valid quantity."
|
| 277 |
+
|
| 278 |
+
# Handle category selection
|
| 279 |
+
for category, items in MENU.items():
|
| 280 |
+
if category.lower() in command:
|
| 281 |
+
current_category = category
|
| 282 |
+
item_list = ", ".join([f"{item} (₹{price})" for item, price in items.items()])
|
| 283 |
+
return f"{category} menu: {item_list}. What would you like to order?"
|
| 284 |
+
|
| 285 |
+
# Handle item selection with dynamic matching
|
| 286 |
+
if current_category:
|
| 287 |
+
for item in MENU[current_category].keys():
|
| 288 |
+
if item.lower().startswith(command) or command in item.lower():
|
| 289 |
+
current_item = item
|
| 290 |
+
awaiting_quantity = True
|
| 291 |
+
return f"How many quantities of {current_item} would you like?"
|
| 292 |
+
|
| 293 |
+
# Handle item removal
|
| 294 |
+
if "remove" in command:
|
| 295 |
+
for item in cart:
|
| 296 |
+
if item[0].lower() in command:
|
| 297 |
+
cart.remove(item)
|
| 298 |
+
total = sum(i[1] * i[2] for i in cart)
|
| 299 |
+
cart_summary = ", ".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
|
| 300 |
+
return f"Removed {item[0]} from your cart. Updated cart: {cart_summary}. Total: ₹{total}."
|
| 301 |
+
return "The item you are trying to remove is not in your cart."
|
| 302 |
+
|
| 303 |
+
# Handle final order
|
| 304 |
+
if "final order" in command or "submit" in command or "Proceed" in command or "place the order" in command:
|
| 305 |
+
if cart:
|
| 306 |
+
order_details = ", ".join([f"{item[0]} x{item[2]} (₹{item[1] * item[2]})" for item in cart])
|
| 307 |
+
total = sum(item[1] * item[2] for item in cart)
|
| 308 |
+
cart.clear()
|
| 309 |
+
return f"Your final order is: {order_details}. Total price: ₹{total}. Your order will arrive soon, Thank you for visiting Biryani Hub!"
|
| 310 |
+
else:
|
| 311 |
+
return "Your cart is empty. Please add items before placing the final order."
|
| 312 |
+
|
| 313 |
+
# Handle cart details
|
| 314 |
+
if "cart details" in command:
|
| 315 |
+
if cart:
|
| 316 |
+
cart_summary = "\n".join([f"{i[0]} x{i[2]} (₹{i[1] * i[2]})" for i in cart])
|
| 317 |
+
total = sum(i[1] * i[2] for i in cart)
|
| 318 |
+
return f"Your cart contains:\n{cart_summary}\nTotal: ₹{total}."
|
| 319 |
+
else:
|
| 320 |
+
return "Your cart is empty."
|
| 321 |
+
|
| 322 |
+
# Handle menu request
|
| 323 |
+
if "menu" in command or "yes" in command or "yeah" in command:
|
| 324 |
+
categories = ", ".join(MENU.keys())
|
| 325 |
+
return f"We have the following categories: {categories}. Please select a category to proceed."
|
| 326 |
+
|
| 327 |
+
# Default response
|
| 328 |
+
return "Sorry, I didn't understand that. Please try again."
|
| 329 |
+
|
| 330 |
+
if __name__ == "__main__":
|
| 331 |
+
app.run(host="0.0.0.0", port=7860)
|