Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,11 +6,11 @@ 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__)
|
16 |
|
@@ -26,14 +26,12 @@ try:
|
|
26 |
print("Attempting to connect to Salesforce...")
|
27 |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
28 |
print("Connected to Salesforce successfully!")
|
29 |
-
print("User Info:", sf.UserInfo) # Log the user info to verify the connection
|
30 |
except Exception as e:
|
31 |
print(f"Failed to connect to Salesforce: {str(e)}")
|
32 |
|
33 |
# Function for Salesforce operations
|
34 |
def create_salesforce_record(name, email, phone_number):
|
35 |
try:
|
36 |
-
# Create a new record in Salesforce's Customer_Login__c object
|
37 |
customer_login = sf.Customer_Login__c.create({
|
38 |
'Name': name,
|
39 |
'Email__c': email,
|
@@ -57,15 +55,14 @@ def generate_audio_prompt(text, filename):
|
|
57 |
tts.save(os.path.join("static", filename))
|
58 |
except gtts.tts.gTTSError as e:
|
59 |
print(f"Error: {e}")
|
60 |
-
|
61 |
-
time.sleep(5) # Wait for 5 seconds before retrying
|
62 |
generate_audio_prompt(text, filename)
|
63 |
|
64 |
# Utility functions
|
65 |
def convert_to_wav(input_path, output_path):
|
66 |
try:
|
67 |
audio = AudioSegment.from_file(input_path)
|
68 |
-
audio = audio.set_frame_rate(16000).set_channels(1)
|
69 |
audio.export(output_path, format="wav")
|
70 |
except Exception as e:
|
71 |
print(f"Error: {str(e)}")
|
@@ -73,8 +70,7 @@ def convert_to_wav(input_path, output_path):
|
|
73 |
|
74 |
def is_silent_audio(audio_path):
|
75 |
audio = AudioSegment.from_wav(audio_path)
|
76 |
-
nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
|
77 |
-
print(f"Detected nonsilent parts: {nonsilent_parts}")
|
78 |
return len(nonsilent_parts) == 0 # If no speech detected
|
79 |
|
80 |
# Routes and Views
|
@@ -84,54 +80,39 @@ def index():
|
|
84 |
|
85 |
@app.route("/dashboard", methods=["GET"])
|
86 |
def dashboard():
|
87 |
-
return render_template("dashboard.html")
|
88 |
|
89 |
@app.route('/submit', methods=['POST'])
|
90 |
def submit():
|
91 |
try:
|
92 |
-
# Ensure JSON is being received
|
93 |
data = request.get_json()
|
94 |
-
print("Received Data:", data) # Debugging line
|
95 |
-
|
96 |
if not data:
|
97 |
return jsonify({"success": False, "message": "Invalid or empty JSON received"}), 400
|
98 |
|
99 |
-
# Get the values from the JSON data
|
100 |
name = data.get("name")
|
101 |
email = data.get("email")
|
102 |
phone = data.get("phone")
|
103 |
|
104 |
-
# Validate if all fields are present
|
105 |
if not name or not email or not phone:
|
106 |
return jsonify({"success": False, "message": "Missing required fields"}), 400
|
107 |
|
108 |
-
#
|
109 |
-
salesforce_data = {
|
110 |
-
"Name": name,
|
111 |
-
"Email__c": email, # Ensure this is the correct field API name in Salesforce
|
112 |
-
"Phone_Number__c": phone # Ensure this is the correct field API name in Salesforce
|
113 |
-
}
|
114 |
-
|
115 |
-
# Insert data into Salesforce using simple_salesforce
|
116 |
try:
|
117 |
-
result = sf.Customer_Login__c.create(salesforce_data)
|
118 |
-
print(f"Salesforce Response: {result}") # Debugging line
|
119 |
-
|
120 |
return jsonify({"success": True, "message": "Data submitted successfully"})
|
121 |
-
|
122 |
except Exception as e:
|
123 |
-
|
124 |
-
return jsonify({"success": False, "message": "Salesforce storage limit exceeded. Please clean up or contact support."}), 500
|
125 |
-
else:
|
126 |
-
print(f"Salesforce Insertion Error: {str(e)}") # Log Salesforce errors
|
127 |
-
return jsonify({"success": False, "message": "Salesforce submission failed", "error": str(e)}), 500
|
128 |
-
|
129 |
except Exception as e:
|
130 |
-
print(f"Server Error: {str(e)}") # Log general errors
|
131 |
return jsonify({"success": False, "message": "Internal server error", "error": str(e)}), 500
|
132 |
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
-
@app.route(
|
135 |
def validate_login():
|
136 |
try:
|
137 |
# Ensure JSON is being received
|
@@ -149,49 +130,62 @@ def validate_login():
|
|
149 |
|
150 |
if result['records']:
|
151 |
# If a matching record is found, return success and the name
|
152 |
-
user_name = result['records'][0]['Name']
|
153 |
return jsonify({"success": True, "message": "Login successful", "name": user_name})
|
154 |
else:
|
155 |
# If no matching record is found
|
156 |
return jsonify({"success": False, "message": "Invalid email or mobile number"}), 401
|
157 |
|
158 |
except Exception as e:
|
159 |
-
print(f"Error validating login: {str(e)}")
|
160 |
return jsonify({"success": False, "message": "Internal server error", "error": str(e)}), 500
|
161 |
|
162 |
-
|
163 |
-
@app.route("/menu", methods=["GET"])
|
164 |
-
def menu_page():
|
165 |
-
menu_items = get_menu_items()
|
166 |
-
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
|
167 |
-
return render_template("menu_page.html", menu_items=menu_data)
|
168 |
-
|
169 |
-
# Route for handling order
|
170 |
@app.route("/order", methods=["POST"])
|
171 |
def place_order():
|
172 |
-
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
|
182 |
-
|
183 |
-
|
184 |
|
185 |
-
|
186 |
-
|
|
|
187 |
|
188 |
-
# Route to handle the cart
|
189 |
@app.route("/cart", methods=["GET"])
|
190 |
def cart():
|
191 |
cart_items = [] # Placeholder for cart items
|
192 |
return render_template("cart_page.html", cart_items=cart_items)
|
193 |
|
194 |
-
# Route for the order summary page
|
195 |
@app.route("/order-summary", methods=["GET"])
|
196 |
def order_summary():
|
197 |
order_details = [] # Placeholder for order details
|
@@ -200,7 +194,6 @@ def order_summary():
|
|
200 |
@app.route("/transcribe", methods=["POST"])
|
201 |
def transcribe():
|
202 |
if "audio" not in request.files:
|
203 |
-
print("No audio file provided")
|
204 |
return jsonify({"error": "No audio file provided"}), 400
|
205 |
|
206 |
audio_file = request.files["audio"]
|
@@ -209,65 +202,35 @@ def transcribe():
|
|
209 |
audio_file.save(input_audio_path)
|
210 |
|
211 |
try:
|
212 |
-
# Convert to WAV
|
213 |
convert_to_wav(input_audio_path, output_audio_path)
|
214 |
|
215 |
-
# Check for silence
|
216 |
if is_silent_audio(output_audio_path):
|
217 |
return jsonify({"error": "No speech detected. Please try again."}), 400
|
218 |
-
else:
|
219 |
-
print("Audio contains speech, proceeding with transcription.")
|
220 |
-
|
221 |
-
# Use Whisper ASR model for transcription
|
222 |
-
result = None
|
223 |
-
retry_attempts = 3
|
224 |
-
for attempt in range(retry_attempts):
|
225 |
-
try:
|
226 |
-
result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
|
227 |
-
print(f"Transcribed text: {result['text']}")
|
228 |
-
break
|
229 |
-
except requests.exceptions.ReadTimeout:
|
230 |
-
print(f"Timeout occurred, retrying attempt {attempt + 1}/{retry_attempts}...")
|
231 |
-
time.sleep(5)
|
232 |
-
|
233 |
-
if result is None:
|
234 |
-
return jsonify({"error": "Unable to transcribe audio after retries."}), 500
|
235 |
|
|
|
236 |
transcribed_text = result["text"].strip().capitalize()
|
237 |
-
print(f"Transcribed text: {transcribed_text}")
|
238 |
|
239 |
# Extract name, email, and phone number from the transcribed text
|
240 |
parts = transcribed_text.split()
|
241 |
name = parts[0] if len(parts) > 0 else "Unknown Name"
|
242 |
email = parts[1] if '@' in parts[1] else "[email protected]"
|
243 |
phone_number = parts[2] if len(parts) > 2 else "0000000000"
|
244 |
-
print(f"Parsed data - Name: {name}, Email: {email}, Phone Number: {phone_number}")
|
245 |
|
246 |
-
# Confirm details before submission
|
247 |
confirmation = f"Is this correct? Name: {name}, Email: {email}, Phone: {phone_number}"
|
248 |
generate_audio_prompt(confirmation, "confirmation.mp3")
|
249 |
|
250 |
-
|
251 |
-
user_confirms = True # Assuming the user confirms, you can replace this with actual user input logic
|
252 |
|
253 |
if user_confirms:
|
254 |
-
# Create record in Salesforce
|
255 |
salesforce_response = create_salesforce_record(name, email, phone_number)
|
256 |
|
257 |
-
# Log the Salesforce response
|
258 |
-
print(f"Salesforce record creation response: {salesforce_response}")
|
259 |
-
|
260 |
-
# Check if the response contains an error
|
261 |
if "error" in salesforce_response:
|
262 |
-
print(f"Error creating record in Salesforce: {salesforce_response['error']}")
|
263 |
return jsonify(salesforce_response), 500
|
264 |
|
265 |
return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
|
266 |
|
267 |
except Exception as e:
|
268 |
-
print(f"Error in transcribing or processing: {str(e)}")
|
269 |
return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
|
270 |
|
271 |
-
# Start Production Server
|
272 |
if __name__ == "__main__":
|
273 |
serve(app, host="0.0.0.0", port=7860)
|
|
|
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__)
|
16 |
|
|
|
26 |
print("Attempting to connect to Salesforce...")
|
27 |
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
28 |
print("Connected to Salesforce successfully!")
|
|
|
29 |
except Exception as e:
|
30 |
print(f"Failed to connect to Salesforce: {str(e)}")
|
31 |
|
32 |
# Function for Salesforce operations
|
33 |
def create_salesforce_record(name, email, phone_number):
|
34 |
try:
|
|
|
35 |
customer_login = sf.Customer_Login__c.create({
|
36 |
'Name': name,
|
37 |
'Email__c': email,
|
|
|
55 |
tts.save(os.path.join("static", filename))
|
56 |
except gtts.tts.gTTSError as e:
|
57 |
print(f"Error: {e}")
|
58 |
+
time.sleep(5) # Retry after 5 seconds
|
|
|
59 |
generate_audio_prompt(text, filename)
|
60 |
|
61 |
# Utility functions
|
62 |
def convert_to_wav(input_path, output_path):
|
63 |
try:
|
64 |
audio = AudioSegment.from_file(input_path)
|
65 |
+
audio = audio.set_frame_rate(16000).set_channels(1)
|
66 |
audio.export(output_path, format="wav")
|
67 |
except Exception as e:
|
68 |
print(f"Error: {str(e)}")
|
|
|
70 |
|
71 |
def is_silent_audio(audio_path):
|
72 |
audio = AudioSegment.from_wav(audio_path)
|
73 |
+
nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
|
|
|
74 |
return len(nonsilent_parts) == 0 # If no speech detected
|
75 |
|
76 |
# Routes and Views
|
|
|
80 |
|
81 |
@app.route("/dashboard", methods=["GET"])
|
82 |
def dashboard():
|
83 |
+
return render_template("dashboard.html")
|
84 |
|
85 |
@app.route('/submit', methods=['POST'])
|
86 |
def submit():
|
87 |
try:
|
|
|
88 |
data = request.get_json()
|
|
|
|
|
89 |
if not data:
|
90 |
return jsonify({"success": False, "message": "Invalid or empty JSON received"}), 400
|
91 |
|
|
|
92 |
name = data.get("name")
|
93 |
email = data.get("email")
|
94 |
phone = data.get("phone")
|
95 |
|
|
|
96 |
if not name or not email or not phone:
|
97 |
return jsonify({"success": False, "message": "Missing required fields"}), 400
|
98 |
|
99 |
+
# Insert data into Salesforce
|
100 |
+
salesforce_data = {"Name": name, "Email__c": email, "Phone_Number__c": phone}
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
try:
|
102 |
+
result = sf.Customer_Login__c.create(salesforce_data)
|
|
|
|
|
103 |
return jsonify({"success": True, "message": "Data submitted successfully"})
|
|
|
104 |
except Exception as e:
|
105 |
+
return jsonify({"success": False, "message": "Salesforce submission failed", "error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
106 |
except Exception as e:
|
|
|
107 |
return jsonify({"success": False, "message": "Internal server error", "error": str(e)}), 500
|
108 |
|
109 |
+
@app.route("/menu", methods=["GET"])
|
110 |
+
def menu_page():
|
111 |
+
menu_items = get_menu_items()
|
112 |
+
menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
|
113 |
+
return render_template("menu_page.html", menu_items=menu_data)
|
114 |
|
115 |
+
@app.route("/validate-login", methods=["POST"])
|
116 |
def validate_login():
|
117 |
try:
|
118 |
# Ensure JSON is being received
|
|
|
130 |
|
131 |
if result['records']:
|
132 |
# If a matching record is found, return success and the name
|
133 |
+
user_name = result['records'][0]['Name']
|
134 |
return jsonify({"success": True, "message": "Login successful", "name": user_name})
|
135 |
else:
|
136 |
# If no matching record is found
|
137 |
return jsonify({"success": False, "message": "Invalid email or mobile number"}), 401
|
138 |
|
139 |
except Exception as e:
|
|
|
140 |
return jsonify({"success": False, "message": "Internal server error", "error": str(e)}), 500
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
@app.route("/order", methods=["POST"])
|
143 |
def place_order():
|
144 |
+
order_data = request.json
|
145 |
+
|
146 |
+
# Get order details from the request
|
147 |
+
customer_name = order_data.get("customerName")
|
148 |
+
customer_email = order_data.get("customerEmail")
|
149 |
+
customer_phone = order_data.get("customerPhone")
|
150 |
+
customer_address = order_data.get("customerAddress")
|
151 |
+
items = order_data.get("items")
|
152 |
+
total_amount = order_data.get("totalAmount")
|
153 |
+
|
154 |
+
# Ensure all required fields are present
|
155 |
+
if not customer_name or not customer_email or not customer_phone or not customer_address or not items or not total_amount:
|
156 |
+
return jsonify({"success": False, "message": "Missing required fields"}), 400
|
157 |
|
158 |
+
try:
|
159 |
+
# Create the order in Salesforce (custom object Order__c)
|
160 |
+
order = sf.Order__c.create({
|
161 |
+
"Customer_Name__c": customer_name,
|
162 |
+
"Customer_Email__c": customer_email,
|
163 |
+
"Customer_Phone__c": customer_phone,
|
164 |
+
"Customer_Address__c": customer_address,
|
165 |
+
"Total_Amount__c": total_amount
|
166 |
+
})
|
167 |
+
|
168 |
+
# Optionally create order items in a related object or as line items
|
169 |
+
for item in items:
|
170 |
+
sf.Order_Item__c.create({
|
171 |
+
"Order__c": order['id'], # Link to the parent order
|
172 |
+
"Item__c": item['name'],
|
173 |
+
"Quantity__c": item['quantity'],
|
174 |
+
"Price__c": item['price']
|
175 |
+
})
|
176 |
|
177 |
+
# Return success response
|
178 |
+
return jsonify({"success": True, "message": "Order placed successfully."})
|
179 |
|
180 |
+
except Exception as e:
|
181 |
+
# If an error occurs, return an error message
|
182 |
+
return jsonify({"success": False, "message": f"Error placing order: {str(e)}"}), 500
|
183 |
|
|
|
184 |
@app.route("/cart", methods=["GET"])
|
185 |
def cart():
|
186 |
cart_items = [] # Placeholder for cart items
|
187 |
return render_template("cart_page.html", cart_items=cart_items)
|
188 |
|
|
|
189 |
@app.route("/order-summary", methods=["GET"])
|
190 |
def order_summary():
|
191 |
order_details = [] # Placeholder for order details
|
|
|
194 |
@app.route("/transcribe", methods=["POST"])
|
195 |
def transcribe():
|
196 |
if "audio" not in request.files:
|
|
|
197 |
return jsonify({"error": "No audio file provided"}), 400
|
198 |
|
199 |
audio_file = request.files["audio"]
|
|
|
202 |
audio_file.save(input_audio_path)
|
203 |
|
204 |
try:
|
|
|
205 |
convert_to_wav(input_audio_path, output_audio_path)
|
206 |
|
|
|
207 |
if is_silent_audio(output_audio_path):
|
208 |
return jsonify({"error": "No speech detected. Please try again."}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
+
result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
|
211 |
transcribed_text = result["text"].strip().capitalize()
|
|
|
212 |
|
213 |
# Extract name, email, and phone number from the transcribed text
|
214 |
parts = transcribed_text.split()
|
215 |
name = parts[0] if len(parts) > 0 else "Unknown Name"
|
216 |
email = parts[1] if '@' in parts[1] else "[email protected]"
|
217 |
phone_number = parts[2] if len(parts) > 2 else "0000000000"
|
|
|
218 |
|
|
|
219 |
confirmation = f"Is this correct? Name: {name}, Email: {email}, Phone: {phone_number}"
|
220 |
generate_audio_prompt(confirmation, "confirmation.mp3")
|
221 |
|
222 |
+
user_confirms = True # Assuming user confirms, replace with actual logic
|
|
|
223 |
|
224 |
if user_confirms:
|
|
|
225 |
salesforce_response = create_salesforce_record(name, email, phone_number)
|
226 |
|
|
|
|
|
|
|
|
|
227 |
if "error" in salesforce_response:
|
|
|
228 |
return jsonify(salesforce_response), 500
|
229 |
|
230 |
return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
|
231 |
|
232 |
except Exception as e:
|
|
|
233 |
return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
|
234 |
|
|
|
235 |
if __name__ == "__main__":
|
236 |
serve(app, host="0.0.0.0", port=7860)
|