from flask import Flask, render_template_string, request, jsonify
import speech_recognition as sr
from tempfile import NamedTemporaryFile
import os
import ffmpeg
import logging
from werkzeug.exceptions import BadRequest
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
# Global cart to store items
cart = []
html_code = """
AI Dining Assistant
AI Dining Assistant
Press the mic button to start the conversation...
Response will appear here...
"""
@app.route('/')
def index():
return render_template_string(html_code)
@app.route('/process-audio', methods=['POST'])
def process_audio():
try:
audio_file = request.files.get('audio')
if not audio_file:
raise BadRequest("No audio file provided.")
temp_file = NamedTemporaryFile(delete=False, suffix=".webm")
audio_file.save(temp_file.name)
if os.path.getsize(temp_file.name) == 0:
raise BadRequest("Uploaded audio file is empty.")
converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
ffmpeg.input(temp_file.name).output(
converted_file.name, acodec='pcm_s16le', ac=1, ar='16000'
).run(overwrite_output=True)
recognizer = sr.Recognizer()
with sr.AudioFile(converted_file.name) as source:
audio_data = recognizer.record(source)
command = recognizer.recognize_google(audio_data)
response = process_command(command)
return jsonify({"response": response})
except BadRequest as br:
return jsonify({"response": f"Bad Request: {str(br)}"}), 400
except Exception as e:
return jsonify({"response": f"An error occurred: {str(e)}"}), 500
finally:
os.unlink(temp_file.name)
os.unlink(converted_file.name)
def process_command(command):
global cart
command = command.lower()
menu_items = ["idli", "dosa", "vada", "pongal", "biryani"]
if "menu" in command:
return "Here is our menu: Idli, Dosa, Vada, Pongal, Biryani."
elif any(item in command for item in menu_items):
item = next((item for item in menu_items if item in command), None)
if item:
cart.append(item)
return f"{item.capitalize()} added to your cart. Your cart now has: {', '.join(cart)}."
elif "order" in command:
if cart:
return f"Your order has been placed for: {', '.join(cart)}. Would you like anything else?"
else:
return "Your cart is empty. Please add items to your cart first."
elif "no" in command or "nothing" in command:
return "Goodbye! Thank you for using AI Dining Assistant."
return "Sorry, I didn't understand your request."
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)