lokesh341 commited on
Commit
4e9c3dd
·
verified ·
1 Parent(s): 9c9036a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -37
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import torch
2
  from flask import Flask, render_template, request, jsonify, redirect, session
3
- import json
4
  import os
5
  from transformers import pipeline
6
  from gtts import gTTS
@@ -12,17 +11,20 @@ from waitress import serve
12
  from simple_salesforce import Salesforce
13
  import requests
14
 
 
15
  app = Flask(__name__)
16
- app.secret_key = os.urandom(24) # For session handling
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
- # Salesforce connection details
26
  try:
27
  print("Attempting to connect to Salesforce...")
28
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
@@ -31,7 +33,8 @@ try:
31
  except Exception as e:
32
  print(f"Failed to connect to Salesforce: {str(e)}")
33
 
34
- # Functions for Salesforce operations
 
35
  def create_salesforce_record(sf, name, email, phone_number):
36
  try:
37
  customer_login = sf.Customer_Login__c.create({
@@ -43,12 +46,14 @@ def create_salesforce_record(sf, name, email, phone_number):
43
  except Exception as e:
44
  raise Exception(f"Failed to create record: {str(e)}")
45
 
 
46
  def get_menu_items(sf):
47
  query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
48
  result = sf.query(query)
49
  return result['records']
50
 
51
- # Voice-related functions
 
52
  def generate_audio_prompt(text, filename):
53
  try:
54
  tts = gTTS(text)
@@ -59,7 +64,8 @@ def generate_audio_prompt(text, filename):
59
  time.sleep(5) # Wait for 5 seconds before retrying
60
  generate_audio_prompt(text, filename)
61
 
62
- # Utility functions
 
63
  def convert_to_wav(input_path, output_path):
64
  try:
65
  audio = AudioSegment.from_file(input_path)
@@ -69,25 +75,30 @@ def convert_to_wav(input_path, output_path):
69
  print(f"Error: {str(e)}")
70
  raise Exception(f"Audio conversion failed: {str(e)}")
71
 
 
 
72
  def is_silent_audio(audio_path):
73
  audio = AudioSegment.from_wav(audio_path)
74
- nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16) # Reduced silence duration
75
  print(f"Detected nonsilent parts: {nonsilent_parts}")
76
  return len(nonsilent_parts) == 0 # If no speech detected
77
 
78
- # Routes and Views
 
79
  @app.route("/")
80
  def index():
81
  return render_template("index.html")
82
 
 
83
  @app.route("/dashboard", methods=["GET"])
84
  def dashboard():
85
  return render_template("dashboard.html")
86
 
 
87
  @app.route('/login', methods=['POST'])
88
  def login():
89
- # Get data from voice bot (name, email, phone number)
90
- data = request.json # Assuming voice bot sends JSON data
91
  name = data.get('name')
92
  email = data.get('email')
93
  phone_number = data.get('phone_number')
@@ -96,54 +107,61 @@ def login():
96
  return jsonify({'error': 'Missing required fields'}), 400
97
 
98
  try:
 
99
  customer_login = create_salesforce_record(sf, name, email, phone_number)
100
  session['customer_id'] = customer_login['id'] # Store customer ID in session
101
  return redirect("/menu") # Redirect to the menu page after successful login
102
  except Exception as e:
103
  return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
104
 
 
105
  @app.route("/menu", methods=["GET"])
106
  def menu_page():
107
- menu_items = get_menu_items(sf) # Fetch menu items from Salesforce
 
108
  menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
109
  return render_template("menu_page.html", menu_items=menu_data)
110
 
111
- # Route to handle the cart
112
  @app.route("/cart", methods=["GET"])
113
  def cart():
114
- # For now, using session to store cart items
115
  cart_items = session.get('cart_items', [])
116
  return render_template("cart_page.html", cart_items=cart_items)
117
 
118
- # Route for the order summary page
119
  @app.route("/order-summary", methods=["GET"])
120
  def order_summary():
121
- # Get the order details from the session
122
  order_details = session.get('cart_items', [])
123
  total_price = sum(item['price'] * item['quantity'] for item in order_details)
124
  return render_template("order_summary.html", order_details=order_details, total_price=total_price)
125
 
 
126
  @app.route("/final_order", methods=["GET"])
127
  def final_order():
 
 
128
  return render_template("final_order.html")
129
 
130
- # Route for handling order placement
131
  @app.route("/order", methods=["POST"])
132
  def place_order():
 
133
  item_name = request.json.get('item_name')
134
  quantity = request.json.get('quantity')
135
-
136
- # Store the item in the session cart
137
  cart_items = session.get('cart_items', [])
138
- cart_items.append({"name": item_name, "quantity": quantity, "price": 10}) # Assuming a fixed price for simplicity
139
- session['cart_items'] = cart_items # Save the updated cart to the session
140
-
141
- return jsonify({"success": True, "message": f"Order for {item_name} placed successfully."})
 
142
 
143
  @app.route("/transcribe", methods=["POST"])
144
  def transcribe():
145
  if "audio" not in request.files:
146
- print("No audio file provided")
147
  return jsonify({"error": "No audio file provided"}), 400
148
 
149
  audio_file = request.files["audio"]
@@ -152,10 +170,10 @@ def transcribe():
152
  audio_file.save(input_audio_path)
153
 
154
  try:
155
- # Convert to WAV
156
  convert_to_wav(input_audio_path, output_audio_path)
157
 
158
- # Check for silence
159
  if is_silent_audio(output_audio_path):
160
  return jsonify({"error": "No speech detected. Please try again."}), 400
161
  else:
@@ -184,6 +202,7 @@ def transcribe():
184
  name = parts[0] if len(parts) > 0 else "Unknown Name"
185
  email = parts[1] if '@' in parts[1] else "[email protected]"
186
  phone_number = parts[2] if len(parts) > 2 else "0000000000"
 
187
  print(f"Parsed data - Name: {name}, Email: {email}, Phone Number: {phone_number}")
188
 
189
  # Confirm details before submission
@@ -191,26 +210,19 @@ def transcribe():
191
  generate_audio_prompt(confirmation, "confirmation.mp3")
192
 
193
  # Simulate confirmation via user action
194
- user_confirms = True # Assuming the user confirms, you can replace this with actual user input logic
195
 
196
  if user_confirms:
197
- # Create record in Salesforce
198
  salesforce_response = create_salesforce_record(name, email, phone_number)
199
 
200
- # Log the Salesforce response
201
- print(f"Salesforce record creation response: {salesforce_response}")
202
-
203
- # Check if the response contains an error
204
- if "error" in salesforce_response:
205
- print(f"Error creating record in Salesforce: {salesforce_response['error']}")
206
- return jsonify(salesforce_response), 500
207
-
208
  return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
209
 
210
  except Exception as e:
211
  print(f"Error in transcribing or processing: {str(e)}")
212
  return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
213
 
 
214
  # Start Production Server
215
  if __name__ == "__main__":
216
  serve(app, host="0.0.0.0", port=7860)
 
1
  import torch
2
  from flask import Flask, render_template, request, jsonify, redirect, session
 
3
  import os
4
  from transformers import pipeline
5
  from gtts import gTTS
 
11
  from simple_salesforce import Salesforce
12
  import requests
13
 
14
+ # Initialize Flask app
15
  app = Flask(__name__)
 
16
 
17
+ # Secret key for session management
18
+ app.secret_key = os.urandom(24) # For session management (secure)
19
+
20
+ # Set the device for processing
21
  device = "cuda" if torch.cuda.is_available() else "cpu"
22
 
23
+ # Whisper ASR model configuration for speech recognition
24
  config = AutoConfig.from_pretrained("openai/whisper-small")
25
  config.update({"timeout": 60}) # Set timeout to 60 seconds
26
 
27
+ # Salesforce Connection Setup
28
  try:
29
  print("Attempting to connect to Salesforce...")
30
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
 
33
  except Exception as e:
34
  print(f"Failed to connect to Salesforce: {str(e)}")
35
 
36
+
37
+ # Functions for Salesforce Operations
38
  def create_salesforce_record(sf, name, email, phone_number):
39
  try:
40
  customer_login = sf.Customer_Login__c.create({
 
46
  except Exception as e:
47
  raise Exception(f"Failed to create record: {str(e)}")
48
 
49
+
50
  def get_menu_items(sf):
51
  query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
52
  result = sf.query(query)
53
  return result['records']
54
 
55
+
56
+ # Voice-related function to generate audio prompts
57
  def generate_audio_prompt(text, filename):
58
  try:
59
  tts = gTTS(text)
 
64
  time.sleep(5) # Wait for 5 seconds before retrying
65
  generate_audio_prompt(text, filename)
66
 
67
+
68
+ # Utility function to convert audio files to WAV format
69
  def convert_to_wav(input_path, output_path):
70
  try:
71
  audio = AudioSegment.from_file(input_path)
 
75
  print(f"Error: {str(e)}")
76
  raise Exception(f"Audio conversion failed: {str(e)}")
77
 
78
+
79
+ # Utility function to check for silence in audio
80
  def is_silent_audio(audio_path):
81
  audio = AudioSegment.from_wav(audio_path)
82
+ nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
83
  print(f"Detected nonsilent parts: {nonsilent_parts}")
84
  return len(nonsilent_parts) == 0 # If no speech detected
85
 
86
+
87
+ # Routes for Handling Views
88
  @app.route("/")
89
  def index():
90
  return render_template("index.html")
91
 
92
+
93
  @app.route("/dashboard", methods=["GET"])
94
  def dashboard():
95
  return render_template("dashboard.html")
96
 
97
+
98
  @app.route('/login', methods=['POST'])
99
  def login():
100
+ # Get data from the voice bot (name, email, phone number)
101
+ data = request.json # Assuming the voice bot sends JSON data
102
  name = data.get('name')
103
  email = data.get('email')
104
  phone_number = data.get('phone_number')
 
107
  return jsonify({'error': 'Missing required fields'}), 400
108
 
109
  try:
110
+ # Create a Salesforce record for the customer
111
  customer_login = create_salesforce_record(sf, name, email, phone_number)
112
  session['customer_id'] = customer_login['id'] # Store customer ID in session
113
  return redirect("/menu") # Redirect to the menu page after successful login
114
  except Exception as e:
115
  return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
116
 
117
+
118
  @app.route("/menu", methods=["GET"])
119
  def menu_page():
120
+ # Fetch menu items from Salesforce
121
+ menu_items = get_menu_items(sf)
122
  menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
123
  return render_template("menu_page.html", menu_items=menu_data)
124
 
125
+
126
  @app.route("/cart", methods=["GET"])
127
  def cart():
128
+ # Retrieve cart items from session
129
  cart_items = session.get('cart_items', [])
130
  return render_template("cart_page.html", cart_items=cart_items)
131
 
132
+
133
  @app.route("/order-summary", methods=["GET"])
134
  def order_summary():
135
+ # Retrieve the order details from session
136
  order_details = session.get('cart_items', [])
137
  total_price = sum(item['price'] * item['quantity'] for item in order_details)
138
  return render_template("order_summary.html", order_details=order_details, total_price=total_price)
139
 
140
+
141
  @app.route("/final_order", methods=["GET"])
142
  def final_order():
143
+ # Clear cart items from session after confirming order
144
+ session.pop('cart_items', None)
145
  return render_template("final_order.html")
146
 
147
+
148
  @app.route("/order", methods=["POST"])
149
  def place_order():
150
+ # Adding items to the cart
151
  item_name = request.json.get('item_name')
152
  quantity = request.json.get('quantity')
153
+
154
+ # Store the item in session cart
155
  cart_items = session.get('cart_items', [])
156
+ cart_items.append({"name": item_name, "quantity": quantity, "price": 10}) # Example with fixed price
157
+ session['cart_items'] = cart_items # Save the updated cart to session
158
+
159
+ return jsonify({"success": True, "message": f"Added {item_name} to cart."})
160
+
161
 
162
  @app.route("/transcribe", methods=["POST"])
163
  def transcribe():
164
  if "audio" not in request.files:
 
165
  return jsonify({"error": "No audio file provided"}), 400
166
 
167
  audio_file = request.files["audio"]
 
170
  audio_file.save(input_audio_path)
171
 
172
  try:
173
+ # Convert audio to WAV format
174
  convert_to_wav(input_audio_path, output_audio_path)
175
 
176
+ # Check if audio contains silence
177
  if is_silent_audio(output_audio_path):
178
  return jsonify({"error": "No speech detected. Please try again."}), 400
179
  else:
 
202
  name = parts[0] if len(parts) > 0 else "Unknown Name"
203
  email = parts[1] if '@' in parts[1] else "[email protected]"
204
  phone_number = parts[2] if len(parts) > 2 else "0000000000"
205
+
206
  print(f"Parsed data - Name: {name}, Email: {email}, Phone Number: {phone_number}")
207
 
208
  # Confirm details before submission
 
210
  generate_audio_prompt(confirmation, "confirmation.mp3")
211
 
212
  # Simulate confirmation via user action
213
+ user_confirms = True # Assuming the user confirms, replace with actual logic
214
 
215
  if user_confirms:
216
+ # Create a record in Salesforce
217
  salesforce_response = create_salesforce_record(name, email, phone_number)
218
 
 
 
 
 
 
 
 
 
219
  return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
220
 
221
  except Exception as e:
222
  print(f"Error in transcribing or processing: {str(e)}")
223
  return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
224
 
225
+
226
  # Start Production Server
227
  if __name__ == "__main__":
228
  serve(app, host="0.0.0.0", port=7860)