lokesh341 commited on
Commit
25311cb
·
verified ·
1 Parent(s): 0a1b877

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -184
app.py CHANGED
@@ -1,228 +1,101 @@
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
6
- from pydub import AudioSegment
7
- from pydub.silence import detect_nonsilent
8
- from transformers import AutoConfig
9
- import time
10
- from waitress import serve
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')
31
  print("Connected to Salesforce successfully!")
32
- print("User Info:", sf.UserInfo) # Log the user info to verify the connection
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({
41
  'Name': name,
42
  'Email__c': email,
43
- 'Phone_Number__c': phone_number
44
  })
45
  return customer_login
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)
60
- tts.save(os.path.join("static", filename))
61
- except gtts.tts.gTTSError as e:
62
- print(f"Error: {e}")
63
- print("Retrying after 5 seconds...")
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)
72
- audio = audio.set_frame_rate(16000).set_channels(1) # Convert to 16kHz, mono
73
- audio.export(output_path, format="wav")
74
- except Exception as e:
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')
105
 
106
- if not name or not email or not 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"]
168
- input_audio_path = os.path.join("static", "temp_input.wav")
169
- output_audio_path = os.path.join("static", "temp.wav")
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:
180
- print("Audio contains speech, proceeding with transcription.")
181
-
182
- # Use Whisper ASR model for transcription
183
- result = None
184
- retry_attempts = 3
185
- for attempt in range(retry_attempts):
186
- try:
187
- result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
188
- print(f"Transcribed text: {result['text']}")
189
- break
190
- except requests.exceptions.ReadTimeout:
191
- print(f"Timeout occurred, retrying attempt {attempt + 1}/{retry_attempts}...")
192
- time.sleep(5)
193
-
194
- if result is None:
195
- return jsonify({"error": "Unable to transcribe audio after retries."}), 500
196
-
197
- transcribed_text = result["text"].strip().capitalize()
198
- print(f"Transcribed text: {transcribed_text}")
199
-
200
- # Extract name, email, and phone number from the transcribed text
201
- parts = transcribed_text.split()
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
209
- confirmation = f"Is this correct? Name: {name}, Email: {email}, Phone: {phone_number}"
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)
 
 
 
1
  import os
2
+ from flask import Flask, render_template, request, jsonify, session, redirect
 
 
 
 
 
 
3
  from simple_salesforce import Salesforce
 
4
 
 
5
  app = Flask(__name__)
6
 
7
  # Secret key for session management
8
+ app.secret_key = os.urandom(24)
 
 
 
 
 
 
 
9
 
10
  # Salesforce Connection Setup
11
  try:
12
  print("Attempting to connect to Salesforce...")
13
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
14
  print("Connected to Salesforce successfully!")
 
15
  except Exception as e:
16
  print(f"Failed to connect to Salesforce: {str(e)}")
17
 
18
+ # Function to create Salesforce record
19
+ def create_salesforce_record(name, email, phone):
 
20
  try:
21
+ # Create record in Salesforce's Customer_Login__c object
22
  customer_login = sf.Customer_Login__c.create({
23
  'Name': name,
24
  'Email__c': email,
25
+ 'Phone_Number__c': phone
26
  })
27
  return customer_login
28
  except Exception as e:
29
+ raise Exception(f"Failed to create record in Salesforce: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ # Route to handle registration form submission
32
+ @app.route("/register", methods=["POST", "GET"])
33
+ def register():
34
+ if request.method == "POST":
35
+ name = request.json.get('name')
36
+ email = request.json.get('email')
37
+ phone = request.json.get('phone')
38
 
39
+ if not name or not email or not phone:
40
+ return jsonify({'error': 'Missing required fields'}), 400
 
 
 
 
41
 
42
+ try:
43
+ # Create Salesforce record for registration
44
+ customer_login = create_salesforce_record(name, email, phone)
45
+ session['customer_name'] = name # Store customer name in session
46
+ session['customer_id'] = customer_login['id'] # Store Salesforce record ID in session
47
 
48
+ # Return success response with a message
49
+ return jsonify({'success': True, 'message': f'Hi {name}, welcome to Briyani Hub!'}), 200
 
 
50
 
51
+ except Exception as e:
52
+ # Log the error and send a message to the frontend
53
+ print(f"Error during Salesforce submission: {str(e)}")
54
+ return jsonify({'error': f'Error submitting registration: {str(e)}'}), 500
55
 
56
+ return render_template("register.html") # This will render your register page if it's a GET request
 
 
57
 
58
+ # Route to handle the login form submission
59
+ @app.route("/login", methods=["POST"])
60
  def login():
61
+ email = request.json.get('email')
62
+ phone = request.json.get('phone')
 
 
 
63
 
64
+ if not email or not phone:
65
  return jsonify({'error': 'Missing required fields'}), 400
66
 
67
  try:
68
+ # Check if the user exists in Salesforce
69
+ result = sf.query(f"SELECT Id, Name, Email__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone}'")
70
+ if result['totalSize'] > 0:
71
+ customer = result['records'][0]
72
+ session['customer_name'] = customer['Name']
73
+ session['customer_id'] = customer['Id']
74
+
75
+ # Return success message and redirect to menu
76
+ return jsonify({'success': True, 'message': f'Hi {customer["Name"]}, welcome back to Briyani Hub!'}), 200
77
+ else:
78
+ return jsonify({'error': 'Invalid email or phone number'}), 400
 
 
 
79
 
80
+ except Exception as e:
81
+ print(f"Error during login: {str(e)}")
82
+ return jsonify({'error': f'Error during login: {str(e)}'}), 500
83
 
84
+ # Route for handling the cart (this could be updated later to handle actual cart data)
85
  @app.route("/cart", methods=["GET"])
86
  def cart():
87
+ return render_template("cart_page.html")
 
 
 
88
 
89
+ # Route for the order summary page
90
  @app.route("/order-summary", methods=["GET"])
91
  def order_summary():
92
+ return render_template("order_summary.html")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
+ # Route to redirect to the menu page
95
+ @app.route("/menu")
96
+ def menu_page():
97
+ welcome_message = request.args.get("welcome_message", "")
98
+ return render_template("menu_page.html", welcome_message=welcome_message)
99
 
 
100
  if __name__ == "__main__":
101
+ app.run(debug=True)