Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,16 @@
|
|
1 |
import torch
|
2 |
-
from flask import Flask, render_template, request, jsonify
|
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
|
10 |
import time
|
11 |
from waitress import serve
|
12 |
from simple_salesforce import Salesforce
|
13 |
-
import requests
|
14 |
|
15 |
app = Flask(__name__, template_folder="templates")
|
16 |
app.secret_key = os.urandom(24)
|
@@ -22,6 +22,42 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
22 |
config = AutoConfig.from_pretrained("openai/whisper-small")
|
23 |
config.update({"timeout": 60}) # Set timeout to 60 seconds
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# Salesforce connection details
|
26 |
try:
|
27 |
print("Attempting to connect to Salesforce...")
|
@@ -39,77 +75,53 @@ def index():
|
|
39 |
@app.route("/dashboard", methods=["GET"])
|
40 |
def dashboard():
|
41 |
return render_template("dashboard.html")
|
|
|
42 |
# β
MENU PAGE ROUTE
|
43 |
@app.route("/menu_page", methods=["GET"])
|
44 |
def menu_page():
|
45 |
return render_template("menu_page.html")
|
46 |
-
# β
MENU PAGE ROUTE (NEWLY ADDED)
|
47 |
-
@app.route("/menu_page", methods=["GET"])
|
48 |
-
def menu_page():
|
49 |
-
try:
|
50 |
-
query = "SELECT Name, Price__c, Ingredients__c, Category__c, Image_URL__c FROM Menu_Item__c"
|
51 |
-
result = sf.query(query)
|
52 |
-
|
53 |
-
menu_items = []
|
54 |
-
for item in result["records"]:
|
55 |
-
menu_items.append({
|
56 |
-
"name": item["Name"],
|
57 |
-
"price": item["Price__c"],
|
58 |
-
"ingredients": item["Ingredients__c"],
|
59 |
-
"category": item["Category__c"],
|
60 |
-
"image_url": item.get("Image_URL__c", "default_image.jpg") # Fallback if no image is found
|
61 |
-
})
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
# β
STATIC IMAGES ROUTE
|
68 |
-
@app.route("/static/images/<path:filename>")
|
69 |
-
def static_images(filename):
|
70 |
-
return send_from_directory(os.path.join(app.root_path, 'static/images'), filename)
|
71 |
-
|
72 |
-
# β
REGISTER API
|
73 |
-
@app.route("/submit", methods=["POST"])
|
74 |
-
def submit():
|
75 |
data = request.json
|
76 |
name = data.get('name')
|
77 |
email = data.get('email')
|
78 |
-
|
79 |
|
80 |
-
if not name or not email or not
|
81 |
-
return jsonify({'error': 'Missing
|
82 |
|
83 |
try:
|
84 |
customer_login = sf.Customer_Login__c.create({
|
85 |
'Name': name,
|
86 |
'Email__c': email,
|
87 |
-
'Phone_Number__c':
|
88 |
})
|
89 |
-
return jsonify({'success': True}), 200
|
90 |
except Exception as e:
|
91 |
-
return jsonify({'error': str(e)}), 500
|
92 |
|
93 |
-
# β
|
94 |
-
@app.route(
|
95 |
-
def
|
96 |
data = request.json
|
97 |
name = data.get('name')
|
98 |
email = data.get('email')
|
99 |
-
|
100 |
|
101 |
-
if not name or not email or not
|
102 |
-
return jsonify({'error': 'Missing
|
103 |
|
104 |
try:
|
105 |
customer_login = sf.Customer_Login__c.create({
|
106 |
'Name': name,
|
107 |
'Email__c': email,
|
108 |
-
'Phone_Number__c':
|
109 |
})
|
110 |
-
return jsonify({'success': True
|
111 |
except Exception as e:
|
112 |
-
return jsonify({'error':
|
113 |
|
114 |
# β
TRANSCRIBE AUDIO API
|
115 |
@app.route("/transcribe", methods=["POST"])
|
@@ -151,6 +163,29 @@ def transcribe():
|
|
151 |
except Exception as e:
|
152 |
return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
# β
START PRODUCTION SERVER
|
155 |
if __name__ == "__main__":
|
156 |
print("β
Starting Flask API Server on port 7860...")
|
|
|
1 |
import torch
|
2 |
+
from flask import Flask, render_template, request, jsonify
|
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 |
+
import requests # Import requests for exception handling
|
14 |
|
15 |
app = Flask(__name__, template_folder="templates")
|
16 |
app.secret_key = os.urandom(24)
|
|
|
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...")
|
|
|
75 |
@app.route("/dashboard", 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 |
return render_template("menu_page.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
# β
LOGIN API
|
85 |
+
@app.route('/login', methods=['POST'])
|
86 |
+
def login():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
data = request.json
|
88 |
name = data.get('name')
|
89 |
email = data.get('email')
|
90 |
+
phone_number = data.get('phone_number')
|
91 |
|
92 |
+
if not name or not email or not phone_number:
|
93 |
+
return jsonify({'error': 'Missing required fields'}), 400
|
94 |
|
95 |
try:
|
96 |
customer_login = sf.Customer_Login__c.create({
|
97 |
'Name': name,
|
98 |
'Email__c': email,
|
99 |
+
'Phone_Number__c': phone_number
|
100 |
})
|
101 |
+
return jsonify({'success': True, 'id': customer_login['id']}), 200
|
102 |
except Exception as e:
|
103 |
+
return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
|
104 |
|
105 |
+
# β
REGISTER API
|
106 |
+
@app.route("/submit", methods=["POST"])
|
107 |
+
def submit():
|
108 |
data = request.json
|
109 |
name = data.get('name')
|
110 |
email = data.get('email')
|
111 |
+
phone = data.get('phone')
|
112 |
|
113 |
+
if not name or not email or not phone:
|
114 |
+
return jsonify({'error': 'Missing data'}), 400
|
115 |
|
116 |
try:
|
117 |
customer_login = sf.Customer_Login__c.create({
|
118 |
'Name': name,
|
119 |
'Email__c': email,
|
120 |
+
'Phone_Number__c': phone
|
121 |
})
|
122 |
+
return jsonify({'success': True}), 200
|
123 |
except Exception as e:
|
124 |
+
return jsonify({'error': str(e)}), 500
|
125 |
|
126 |
# β
TRANSCRIBE AUDIO API
|
127 |
@app.route("/transcribe", methods=["POST"])
|
|
|
163 |
except Exception as e:
|
164 |
return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
|
165 |
|
166 |
+
# β
MENU API
|
167 |
+
@app.route("/menu", methods=["GET"])
|
168 |
+
def get_menu():
|
169 |
+
try:
|
170 |
+
# Fetch menu items from Salesforce
|
171 |
+
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
172 |
+
result = sf.query(query)
|
173 |
+
|
174 |
+
menu_items = []
|
175 |
+
for item in result["records"]:
|
176 |
+
menu_items.append({
|
177 |
+
"name": item["Name"],
|
178 |
+
"price": item["Price__c"],
|
179 |
+
"ingredients": item["Ingredients__c"],
|
180 |
+
"category": item["Category__c"]
|
181 |
+
})
|
182 |
+
|
183 |
+
# Pass the menu items to the template
|
184 |
+
return render_template("menu_page.html", menu=menu_items)
|
185 |
+
|
186 |
+
except Exception as e:
|
187 |
+
return jsonify({"error": f"Failed to fetch menu: {str(e)}"}), 500
|
188 |
+
|
189 |
# β
START PRODUCTION SERVER
|
190 |
if __name__ == "__main__":
|
191 |
print("β
Starting Flask API Server on port 7860...")
|