lokesh341 commited on
Commit
2940ad4
·
verified ·
1 Parent(s): 33ddbfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -92
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import torch
2
- from flask import Flask, render_template, request, jsonify
3
  import json
4
  import os
5
  from transformers import pipeline
@@ -13,6 +13,7 @@ from simple_salesforce import Salesforce
13
  import requests # Import requests for exception handling
14
 
15
  app = Flask(__name__)
 
16
 
17
  # Use whisper-small for faster processing and better speed
18
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -30,6 +31,9 @@ try:
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:
@@ -78,13 +82,51 @@ def is_silent_audio(audio_path):
78
  return len(nonsilent_parts) == 0 # If no speech detected
79
 
80
  # Routes and Views
81
- @app.route("/")
82
- def index():
83
- return render_template("index.html")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
- @app.route("/dashboard", methods=["GET"])
86
- def dashboard():
87
- return render_template("dashboard.html") # Render the dashboard template
 
 
88
 
89
  @app.route('/submit', methods=['POST'])
90
  def submit():
@@ -169,95 +211,20 @@ def menu_page():
169
  # Route for handling order
170
  @app.route("/order", methods=["POST"])
171
  def place_order():
172
- item_name = request.json.get('item_name')
173
- quantity = request.json.get('quantity')
174
- order_data = {"Item__c": item_name, "Quantity__c": quantity}
175
- sf.Order__c.create(order_data)
176
- return jsonify({"success": True, "message": f"Order for {item_name} placed successfully."})
177
 
178
- # Route to handle the cart
179
- @app.route("/cart", methods=["GET"])
180
- def cart():
181
- cart_items = [] # Placeholder for cart items
182
- return render_template("cart_page.html", cart_items=cart_items)
183
-
184
- # Route for the order summary page
185
- @app.route("/order-summary", methods=["GET"])
186
- def order_summary():
187
- order_details = [] # Placeholder for order details
188
- return render_template("order_summary.html", order_details=order_details)
189
-
190
- @app.route("/transcribe", methods=["POST"])
191
- def transcribe():
192
- if "audio" not in request.files:
193
- print("No audio file provided")
194
- return jsonify({"error": "No audio file provided"}), 400
195
-
196
- audio_file = request.files["audio"]
197
- input_audio_path = os.path.join("static", "temp_input.wav")
198
- output_audio_path = os.path.join("static", "temp.wav")
199
- audio_file.save(input_audio_path)
200
 
201
  try:
202
- # Convert to WAV
203
- convert_to_wav(input_audio_path, output_audio_path)
204
-
205
- # Check for silence
206
- if is_silent_audio(output_audio_path):
207
- return jsonify({"error": "No speech detected. Please try again."}), 400
208
- else:
209
- print("Audio contains speech, proceeding with transcription.")
210
-
211
- # Use Whisper ASR model for transcription
212
- result = None
213
- retry_attempts = 3
214
- for attempt in range(retry_attempts):
215
- try:
216
- result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
217
- print(f"Transcribed text: {result['text']}")
218
- break
219
- except requests.exceptions.ReadTimeout:
220
- print(f"Timeout occurred, retrying attempt {attempt + 1}/{retry_attempts}...")
221
- time.sleep(5)
222
-
223
- if result is None:
224
- return jsonify({"error": "Unable to transcribe audio after retries."}), 500
225
-
226
- transcribed_text = result["text"].strip().capitalize()
227
- print(f"Transcribed text: {transcribed_text}")
228
-
229
- # Extract name, email, and phone number from the transcribed text
230
- parts = transcribed_text.split()
231
- name = parts[0] if len(parts) > 0 else "Unknown Name"
232
- email = parts[1] if '@' in parts[1] else "[email protected]"
233
- phone_number = parts[2] if len(parts) > 2 else "0000000000"
234
- print(f"Parsed data - Name: {name}, Email: {email}, Phone Number: {phone_number}")
235
-
236
- # Confirm details before submission
237
- confirmation = f"Is this correct? Name: {name}, Email: {email}, Phone: {phone_number}"
238
- generate_audio_prompt(confirmation, "confirmation.mp3")
239
-
240
- # Simulate confirmation via user action
241
- user_confirms = True # Assuming the user confirms, you can replace this with actual user input logic
242
-
243
- if user_confirms:
244
- # Create record in Salesforce
245
- salesforce_response = create_salesforce_record(name, email, phone_number)
246
-
247
- # Log the Salesforce response
248
- print(f"Salesforce record creation response: {salesforce_response}")
249
-
250
- # Check if the response contains an error
251
- if "error" in salesforce_response:
252
- print(f"Error creating record in Salesforce: {salesforce_response['error']}")
253
- return jsonify(salesforce_response), 500
254
-
255
- return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
256
 
257
  except Exception as e:
258
- print(f"Error in transcribing or processing: {str(e)}")
259
- return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
260
 
261
- # Start Production Server
262
  if __name__ == "__main__":
263
- serve(app, host="0.0.0.0", port=7860)
 
1
  import torch
2
+ from flask import Flask, render_template, request, jsonify, session
3
  import json
4
  import os
5
  from transformers import pipeline
 
13
  import requests # Import requests for exception handling
14
 
15
  app = Flask(__name__)
16
+ app.secret_key = 'your_secret_key'
17
 
18
  # Use whisper-small for faster processing and better speed
19
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
31
  except Exception as e:
32
  print(f"Failed to connect to Salesforce: {str(e)}")
33
 
34
+ # Global cart variable to store cart items
35
+ cart = []
36
+
37
  # Function for Salesforce operations
38
  def create_salesforce_record(name, email, phone_number):
39
  try:
 
82
  return len(nonsilent_parts) == 0 # If no speech detected
83
 
84
  # Routes and Views
85
+ @app.route("/menu", methods=["GET"])
86
+ def menu_page():
87
+ menu_items = get_menu_items()
88
+ menu_data = [{"name": item['Name'], "price": item['Price__c'], "ingredients": item['Ingredients__c'], "category": item['Category__c']} for item in menu_items]
89
+ return render_template("menu_page.html", menu_items=menu_data)
90
+
91
+ @app.route("/add_to_cart", methods=["POST"])
92
+ def add_to_cart():
93
+ item_name = request.json.get('item_name')
94
+ quantity = request.json.get('quantity')
95
+
96
+ # Find item price
97
+ for category, items in MENU.items():
98
+ if item_name in items:
99
+ price = items[item_name]
100
+ cart_item = {
101
+ 'name': item_name,
102
+ 'price': price,
103
+ 'quantity': quantity
104
+ }
105
+ cart.append(cart_item)
106
+ return jsonify({"message": f"Added {quantity} x {item_name} to the cart."})
107
+
108
+ return jsonify({"message": "Item not found in menu."}), 404
109
+
110
+ # Route for handling order
111
+ @app.route("/order", methods=["POST"])
112
+ def place_order():
113
+ item_name = request.json.get('item_name')
114
+ quantity = request.json.get('quantity')
115
+ order_data = {"Item__c": item_name, "Quantity__c": quantity}
116
+ sf.Order__c.create(order_data)
117
+ return jsonify({"success": True, "message": f"Order for {item_name} placed successfully."})
118
+
119
+ # Route to handle the cart
120
+ @app.route("/cart", methods=["GET"])
121
+ def cart():
122
+ cart_items = [] # Placeholder for cart items
123
+ return render_template("cart_page.html", cart_items=cart_items)
124
 
125
+ # Route for the order summary page
126
+ @app.route("/order-summary", methods=["GET"])
127
+ def order_summary():
128
+ order_details = [] # Placeholder for order details
129
+ return render_template("order_summary.html", order_details=order_details)
130
 
131
  @app.route('/submit', methods=['POST'])
132
  def submit():
 
211
  # Route for handling order
212
  @app.route("/order", methods=["POST"])
213
  def place_order():
214
+ cart_items = request.json.get("cart_items")
215
+ total_price = sum([item['price'] * item['quantity'] for item in cart_items])
216
+ customer_id = session.get('customer_id')
 
 
217
 
218
+ if not customer_id:
219
+ return jsonify({"message": "User not logged in"}), 400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
  try:
222
+ # Create order in Salesforce
223
+ order = create_salesforce_order(cart_items, total_price, customer_id)
224
+ return jsonify({"message": "Order placed successfully!", "order": order})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
  except Exception as e:
227
+ return jsonify({"message": f"Failed to create order: {str(e)}"}), 500
 
228
 
 
229
  if __name__ == "__main__":
230
+ app.run(debug=True)