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