Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,23 +6,65 @@ 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
|
10 |
import time
|
11 |
from waitress import serve
|
12 |
from simple_salesforce import Salesforce
|
13 |
-
import requests
|
14 |
-
import datetime
|
15 |
|
16 |
app = Flask(__name__, template_folder="templates")
|
17 |
app.secret_key = os.urandom(24)
|
18 |
|
19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
try:
|
21 |
print("Attempting to connect to Salesforce...")
|
22 |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
23 |
print("β
Connected to Salesforce successfully!")
|
24 |
except Exception as e:
|
25 |
-
print(f"
|
26 |
|
27 |
# β
HOME ROUTE (Loads `index.html`)
|
28 |
@app.route("/", methods=["GET"])
|
@@ -34,7 +76,7 @@ def index():
|
|
34 |
def dashboard():
|
35 |
return render_template("dashboard.html")
|
36 |
|
37 |
-
# β
MENU PAGE ROUTE
|
38 |
@app.route("/menu_page", methods=["GET"])
|
39 |
def menu_page():
|
40 |
try:
|
@@ -54,6 +96,11 @@ def menu_page():
|
|
54 |
except Exception as e:
|
55 |
return jsonify({"error": f"Failed to fetch menu: {str(e)}"}), 500
|
56 |
|
|
|
|
|
|
|
|
|
|
|
57 |
# β
LOGIN API
|
58 |
@app.route('/login', methods=['POST'])
|
59 |
def login():
|
@@ -96,42 +143,30 @@ def submit():
|
|
96 |
except Exception as e:
|
97 |
return jsonify({'error': str(e)}), 500
|
98 |
|
99 |
-
# β
|
100 |
-
@app.route("/
|
101 |
-
def
|
102 |
-
data = request.json
|
103 |
-
order_items = data.get('order_items') # List of items added to the cart
|
104 |
-
total_price = data.get('total_price') # Total price of the order
|
105 |
-
customer_name = data.get('customer_name') # Customer details
|
106 |
-
customer_email = data.get('customer_email')
|
107 |
-
|
108 |
-
if not order_items or not total_price or not customer_name or not customer_email:
|
109 |
-
return jsonify({'error': 'Missing required fields'}), 400
|
110 |
-
|
111 |
-
# Creating order record in Salesforce
|
112 |
try:
|
113 |
-
|
114 |
-
|
115 |
-
'Customer_Name__c': customer_name,
|
116 |
-
'Customer_Email__c': customer_email,
|
117 |
-
'Total_Price__c': total_price,
|
118 |
-
'Order_Date__c': str(datetime.datetime.now()), # Date when the order was placed
|
119 |
-
'Status__c': 'New'
|
120 |
-
})
|
121 |
|
122 |
-
|
123 |
-
for item in
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
'Total_Price__c': item['price'] * item['quantity']
|
130 |
})
|
131 |
|
132 |
-
return jsonify({
|
133 |
except Exception as e:
|
134 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
# β
START PRODUCTION SERVER
|
137 |
if __name__ == "__main__":
|
|
|
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 |
+
import requests # Import requests for exception handling
|
|
|
14 |
|
15 |
app = Flask(__name__, template_folder="templates")
|
16 |
app.secret_key = os.urandom(24)
|
17 |
|
18 |
+
# Use whisper-small for faster processing and better speed
|
19 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
20 |
+
|
21 |
+
# Create config object to set timeout and other parameters
|
22 |
+
config = AutoConfig.from_pretrained("openai/whisper-small")
|
23 |
+
config.update({"timeout": 60}) # Set timeout to 60 seconds
|
24 |
+
|
25 |
+
# Function to generate audio prompts
|
26 |
+
def generate_audio_prompt(text, filename):
|
27 |
+
try:
|
28 |
+
tts = gTTS(text)
|
29 |
+
tts.save(os.path.join("static", filename))
|
30 |
+
except Exception as e:
|
31 |
+
print(f"Error: {e}")
|
32 |
+
time.sleep(5) # Wait before retrying
|
33 |
+
generate_audio_prompt(text, filename)
|
34 |
+
|
35 |
+
# Generate required voice prompts
|
36 |
+
prompts = {
|
37 |
+
"welcome": "Welcome to Biryani Hub.",
|
38 |
+
"ask_name": "Tell me your name.",
|
39 |
+
"ask_email": "Please provide your email address.",
|
40 |
+
"thank_you": "Thank you for registration."
|
41 |
+
}
|
42 |
+
|
43 |
+
for key, text in prompts.items():
|
44 |
+
generate_audio_prompt(text, f"{key}.mp3")
|
45 |
+
|
46 |
+
# Function to convert audio to WAV format
|
47 |
+
def convert_to_wav(input_path, output_path):
|
48 |
+
try:
|
49 |
+
audio = AudioSegment.from_file(input_path)
|
50 |
+
audio = audio.set_frame_rate(16000).set_channels(1) # Convert to 16kHz, mono
|
51 |
+
audio.export(output_path, format="wav")
|
52 |
+
except Exception as e:
|
53 |
+
raise Exception(f"Audio conversion failed: {str(e)}")
|
54 |
+
|
55 |
+
# Function to check if audio contains actual speech
|
56 |
+
def is_silent_audio(audio_path):
|
57 |
+
audio = AudioSegment.from_wav(audio_path)
|
58 |
+
nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
|
59 |
+
return len(nonsilent_parts) == 0
|
60 |
+
|
61 |
+
# Salesforce connection details
|
62 |
try:
|
63 |
print("Attempting to connect to Salesforce...")
|
64 |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
65 |
print("β
Connected to Salesforce successfully!")
|
66 |
except Exception as e:
|
67 |
+
print(f"Failed to connect to Salesforce: {str(e)}")
|
68 |
|
69 |
# β
HOME ROUTE (Loads `index.html`)
|
70 |
@app.route("/", methods=["GET"])
|
|
|
76 |
def dashboard():
|
77 |
return render_template("dashboard.html")
|
78 |
|
79 |
+
# β
MENU PAGE ROUTE
|
80 |
@app.route("/menu_page", methods=["GET"])
|
81 |
def menu_page():
|
82 |
try:
|
|
|
96 |
except Exception as e:
|
97 |
return jsonify({"error": f"Failed to fetch menu: {str(e)}"}), 500
|
98 |
|
99 |
+
# β
CART PAGE ROUTE (NEWLY ADDED)
|
100 |
+
@app.route("/cart_page", methods=["GET"])
|
101 |
+
def cart_page():
|
102 |
+
return render_template("cart_page.html")
|
103 |
+
|
104 |
# β
LOGIN API
|
105 |
@app.route('/login', methods=['POST'])
|
106 |
def login():
|
|
|
143 |
except Exception as e:
|
144 |
return jsonify({'error': str(e)}), 500
|
145 |
|
146 |
+
# β
MENU API
|
147 |
+
@app.route("/menu", methods=["GET"])
|
148 |
+
def get_menu():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
try:
|
150 |
+
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
151 |
+
result = sf.query(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
+
menu_items = []
|
154 |
+
for item in result["records"]:
|
155 |
+
menu_items.append({
|
156 |
+
"name": item["Name"],
|
157 |
+
"price": item["Price__c"],
|
158 |
+
"ingredients": item["Ingredients__c"],
|
159 |
+
"category": item["Category__c"]
|
|
|
160 |
})
|
161 |
|
162 |
+
return jsonify({"success": True, "menu": menu_items})
|
163 |
except Exception as e:
|
164 |
+
return jsonify({"error": f"Failed to fetch menu: {str(e)}"}), 500
|
165 |
+
|
166 |
+
# β
STATIC IMAGES ROUTE
|
167 |
+
@app.route("/static/images/<path:filename>")
|
168 |
+
def static_images(filename):
|
169 |
+
return send_from_directory(os.path.join(app.root_path, 'static/images'), filename)
|
170 |
|
171 |
# β
START PRODUCTION SERVER
|
172 |
if __name__ == "__main__":
|