Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,73 @@
|
|
1 |
import torch
|
2 |
from flask import Flask, render_template, request, jsonify, redirect
|
3 |
-
import json
|
4 |
-
import os
|
5 |
-
from transformers import pipeline
|
6 |
-
from gtts import gTTS
|
7 |
-
from pydub import AudioSegment
|
8 |
-
from pydub.silence import detect_nonsilent
|
9 |
-
from transformers import AutoConfig # Import AutoConfig for the config object
|
10 |
-
import time
|
11 |
-
from waitress import serve
|
12 |
from simple_salesforce import Salesforce
|
13 |
-
|
|
|
14 |
|
15 |
app = Flask(__name__)
|
16 |
|
|
|
17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
|
|
|
19 |
config = AutoConfig.from_pretrained("openai/whisper-small")
|
20 |
config.update({"timeout": 60})
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
|
|
27 |
def create_salesforce_record(sf, name, email, phone_number):
|
|
|
|
|
28 |
try:
|
|
|
29 |
customer_login = sf.Customer_Login__c.create({
|
30 |
'Name': name,
|
31 |
'Email__c': email,
|
32 |
'Phone_Number__c': phone_number
|
33 |
})
|
|
|
34 |
return customer_login
|
35 |
except Exception as e:
|
36 |
-
|
|
|
37 |
|
|
|
38 |
def get_menu_items(sf):
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
43 |
@app.route("/")
|
44 |
def index():
|
45 |
return render_template("index.html")
|
46 |
|
|
|
47 |
@app.route("/dashboard", methods=["GET"])
|
48 |
def dashboard():
|
49 |
return render_template("dashboard.html")
|
50 |
|
|
|
51 |
@app.route('/login', methods=['POST'])
|
52 |
def login():
|
53 |
data = request.json
|
@@ -59,28 +79,42 @@ def login():
|
|
59 |
return jsonify({'error': 'Missing required fields'}), 400
|
60 |
|
61 |
try:
|
|
|
|
|
|
|
|
|
|
|
62 |
create_salesforce_record(sf, name, email, phone_number)
|
63 |
return redirect("/menu")
|
64 |
except Exception as e:
|
65 |
return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
|
66 |
|
|
|
67 |
@app.route("/menu", methods=["GET"])
|
68 |
def menu_page():
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
72 |
|
|
|
73 |
@app.route("/cart", methods=["GET"])
|
74 |
def cart():
|
75 |
return render_template("cart_page.html")
|
76 |
|
|
|
77 |
@app.route("/order-summary", methods=["GET"])
|
78 |
def order_summary():
|
79 |
return render_template("order_summary.html")
|
80 |
|
|
|
81 |
@app.route("/final_order", methods=["GET"])
|
82 |
def final_order():
|
83 |
return render_template("final_order.html")
|
84 |
|
|
|
85 |
if __name__ == "__main__":
|
86 |
serve(app, host="0.0.0.0", port=7860)
|
|
|
1 |
import torch
|
2 |
from flask import Flask, render_template, request, jsonify, redirect
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from simple_salesforce import Salesforce
|
4 |
+
from transformers import AutoConfig
|
5 |
+
from waitress import serve
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
+
# Initialize the device for processing
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
|
12 |
+
# Initialize the Whisper model configuration
|
13 |
config = AutoConfig.from_pretrained("openai/whisper-small")
|
14 |
config.update({"timeout": 60})
|
15 |
|
16 |
+
# Connect to Salesforce
|
17 |
+
def connect_to_salesforce():
|
18 |
+
try:
|
19 |
+
# Salesforce login credentials
|
20 |
+
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
21 |
+
print("Connected to Salesforce!")
|
22 |
+
return sf
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Failed to connect to Salesforce: {str(e)}")
|
25 |
+
return None
|
26 |
+
|
27 |
+
# Initialize Salesforce connection
|
28 |
+
sf = connect_to_salesforce()
|
29 |
|
30 |
+
# Function to create a record in Salesforce (Customer Login)
|
31 |
def create_salesforce_record(sf, name, email, phone_number):
|
32 |
+
if not sf:
|
33 |
+
raise Exception("Salesforce connection is not established.")
|
34 |
try:
|
35 |
+
# Creating the Customer_Login__c record in Salesforce
|
36 |
customer_login = sf.Customer_Login__c.create({
|
37 |
'Name': name,
|
38 |
'Email__c': email,
|
39 |
'Phone_Number__c': phone_number
|
40 |
})
|
41 |
+
print(f"Customer record created with ID: {customer_login['id']}")
|
42 |
return customer_login
|
43 |
except Exception as e:
|
44 |
+
print(f"Failed to create record in Salesforce: {str(e)}")
|
45 |
+
raise Exception(f"Failed to create record in Salesforce: {str(e)}")
|
46 |
|
47 |
+
# Function to fetch menu items from Salesforce
|
48 |
def get_menu_items(sf):
|
49 |
+
if not sf:
|
50 |
+
raise Exception("Salesforce connection is not established.")
|
51 |
+
try:
|
52 |
+
# Querying Salesforce for menu items
|
53 |
+
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
54 |
+
result = sf.query(query)
|
55 |
+
return result['records']
|
56 |
+
except Exception as e:
|
57 |
+
print(f"Failed to fetch menu items from Salesforce: {str(e)}")
|
58 |
+
raise Exception(f"Failed to fetch menu items from Salesforce: {str(e)}")
|
59 |
|
60 |
+
# Route to the index page
|
61 |
@app.route("/")
|
62 |
def index():
|
63 |
return render_template("index.html")
|
64 |
|
65 |
+
# Route to dashboard
|
66 |
@app.route("/dashboard", methods=["GET"])
|
67 |
def dashboard():
|
68 |
return render_template("dashboard.html")
|
69 |
|
70 |
+
# Route for login (registration) - creates a new customer record in Salesforce
|
71 |
@app.route('/login', methods=['POST'])
|
72 |
def login():
|
73 |
data = request.json
|
|
|
79 |
return jsonify({'error': 'Missing required fields'}), 400
|
80 |
|
81 |
try:
|
82 |
+
# Check if Salesforce connection is valid
|
83 |
+
if not sf:
|
84 |
+
return jsonify({'error': 'Salesforce connection failed, try again later.'}), 500
|
85 |
+
|
86 |
+
# Create the customer record in Salesforce
|
87 |
create_salesforce_record(sf, name, email, phone_number)
|
88 |
return redirect("/menu")
|
89 |
except Exception as e:
|
90 |
return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
|
91 |
|
92 |
+
# Route to the menu page - fetches menu items from Salesforce
|
93 |
@app.route("/menu", methods=["GET"])
|
94 |
def menu_page():
|
95 |
+
try:
|
96 |
+
# Fetch the menu items from Salesforce
|
97 |
+
menu_items = get_menu_items(sf)
|
98 |
+
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
|
99 |
+
return render_template("menu_page.html", menu_items=menu_data)
|
100 |
+
except Exception as e:
|
101 |
+
return jsonify({'error': f'Failed to fetch menu items: {str(e)}'}), 500
|
102 |
|
103 |
+
# Route to the cart page
|
104 |
@app.route("/cart", methods=["GET"])
|
105 |
def cart():
|
106 |
return render_template("cart_page.html")
|
107 |
|
108 |
+
# Route for the order summary page
|
109 |
@app.route("/order-summary", methods=["GET"])
|
110 |
def order_summary():
|
111 |
return render_template("order_summary.html")
|
112 |
|
113 |
+
# Route for final order page
|
114 |
@app.route("/final_order", methods=["GET"])
|
115 |
def final_order():
|
116 |
return render_template("final_order.html")
|
117 |
|
118 |
+
# Starting the app with Waitress server
|
119 |
if __name__ == "__main__":
|
120 |
serve(app, host="0.0.0.0", port=7860)
|