voicemenu1433 / app.py
lokesh341's picture
Update app.py
ee62e21 verified
raw
history blame
4.29 kB
import torch
from flask import Flask, render_template, request, jsonify, redirect
from simple_salesforce import Salesforce
from transformers import AutoConfig
from waitress import serve
app = Flask(__name__)
# Initialize the device for processing
device = "cuda" if torch.cuda.is_available() else "cpu"
# Initialize the Whisper model configuration
config = AutoConfig.from_pretrained("openai/whisper-small")
config.update({"timeout": 60})
# Connect to Salesforce
def connect_to_salesforce():
try:
# Salesforce login credentials
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
print("Connected to Salesforce!")
return sf
except Exception as e:
print(f"Failed to connect to Salesforce: {str(e)}")
return None
# Initialize Salesforce connection
sf = connect_to_salesforce()
# Function to create a record in Salesforce (Customer Login)
def create_salesforce_record(sf, name, email, phone_number):
if not sf:
raise Exception("Salesforce connection is not established.")
try:
# Creating the Customer_Login__c record in Salesforce
customer_login = sf.Customer_Login__c.create({
'Name': name,
'Email__c': email,
'Phone_Number__c': phone_number
})
print(f"Customer record created with ID: {customer_login['id']}")
return customer_login
except Exception as e:
print(f"Failed to create record in Salesforce: {str(e)}")
raise Exception(f"Failed to create record in Salesforce: {str(e)}")
# Function to fetch menu items from Salesforce
def get_menu_items(sf):
if not sf:
raise Exception("Salesforce connection is not established.")
try:
# Querying Salesforce for menu items
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
result = sf.query(query)
return result['records']
except Exception as e:
print(f"Failed to fetch menu items from Salesforce: {str(e)}")
raise Exception(f"Failed to fetch menu items from Salesforce: {str(e)}")
# Route to the index page
@app.route("/")
def index():
return render_template("index.html")
# Route to dashboard
@app.route("/dashboard", methods=["GET"])
def dashboard():
return render_template("dashboard.html")
# Route for login (registration) - creates a new customer record in Salesforce
@app.route('/login', methods=['POST'])
def login():
data = request.json
name = data.get('name')
email = data.get('email')
phone_number = data.get('phone_number')
if not name or not email or not phone_number:
return jsonify({'error': 'Missing required fields'}), 400
try:
# Check if Salesforce connection is valid
if not sf:
return jsonify({'error': 'Salesforce connection failed, try again later.'}), 500
# Create the customer record in Salesforce
create_salesforce_record(sf, name, email, phone_number)
return redirect("/menu")
except Exception as e:
return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
# Route to the menu page - fetches menu items from Salesforce
@app.route("/menu", methods=["GET"])
def menu_page():
try:
# Fetch the menu items from Salesforce
menu_items = get_menu_items(sf)
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
return render_template("menu_page.html", menu_items=menu_data)
except Exception as e:
return jsonify({'error': f'Failed to fetch menu items: {str(e)}'}), 500
# Route to the cart page
@app.route("/cart", methods=["GET"])
def cart():
return render_template("cart_page.html")
# Route for the order summary page
@app.route("/order-summary", methods=["GET"])
def order_summary():
return render_template("order_summary.html")
# Route for final order page
@app.route("/final_order", methods=["GET"])
def final_order():
return render_template("final_order.html")
# Starting the app with Waitress server
if __name__ == "__main__":
serve(app, host="0.0.0.0", port=7860)