lokesh341 commited on
Commit
5758799
·
verified ·
1 Parent(s): 6a2e684

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -77
app.py CHANGED
@@ -1,38 +1,45 @@
 
 
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)
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
- # 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.",
@@ -40,48 +47,95 @@ prompts = {
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...")
64
- sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
65
- print("Connected to Salesforce successfully!")
66
- except Exception as e:
67
- print(f"Failed to connect to Salesforce: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- # HOME ROUTE (Loads `index.html`)
70
  @app.route("/", methods=["GET"])
71
  def index():
72
  return render_template("index.html")
73
 
74
- # DASHBOARD ROUTE
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
@@ -92,17 +146,14 @@ def login():
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
@@ -113,17 +164,14 @@ def submit():
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"])
128
  def transcribe():
129
  if "audio" not in request.files:
@@ -135,15 +183,18 @@ def transcribe():
135
  audio_file.save(input_audio_path)
136
 
137
  try:
 
138
  convert_to_wav(input_audio_path, output_audio_path)
139
  if is_silent_audio(output_audio_path):
140
  return jsonify({"error": "No speech detected. Please try again."}), 400
141
 
 
142
  asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
143
  result = asr_pipeline(output_audio_path)
144
 
145
  transcribed_text = result["text"].strip().capitalize()
146
 
 
147
  parts = transcribed_text.split()
148
  name = parts[0] if len(parts) > 0 else "Unknown Name"
149
  email = parts[1] if '@' in parts[1] else "[email protected]"
@@ -152,6 +203,7 @@ def transcribe():
152
  confirmation = f"Is this correct? Name: {name}, Email: {email}, Phone: {phone_number}"
153
  generate_audio_prompt(confirmation, "confirmation.mp3")
154
 
 
155
  salesforce_response = sf.Customer_Login__c.create({
156
  'Name': name,
157
  'Email__c': email,
@@ -163,30 +215,7 @@ def transcribe():
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...")
192
  serve(app, host="0.0.0.0", port=7860)
 
1
+ import os
2
+ import time
3
  import torch
4
  from flask import Flask, render_template, request, jsonify
5
+ from simple_salesforce import Salesforce
 
6
  from transformers import pipeline
7
  from gtts import gTTS
8
  from pydub import AudioSegment
9
  from pydub.silence import detect_nonsilent
10
  from transformers import AutoConfig # Import AutoConfig for the config object
 
11
  from waitress import serve
 
 
12
 
13
+ # Flask setup
14
  app = Flask(__name__, template_folder="templates")
15
  app.secret_key = os.urandom(24)
16
 
17
+ # Check for available GPU
18
  device = "cuda" if torch.cuda.is_available() else "cpu"
19
 
20
+ # Set up Whisper model config
21
  config = AutoConfig.from_pretrained("openai/whisper-small")
22
  config.update({"timeout": 60}) # Set timeout to 60 seconds
23
 
24
+ # Salesforce connection
25
+ try:
26
+ print("Attempting to connect to Salesforce...")
27
+ sf = Salesforce(username='your_username', password='your_password', security_token='your_security_token')
28
+ print("Connected to Salesforce successfully!")
29
+ except Exception as e:
30
+ print(f"Failed to connect to Salesforce: {str(e)}")
31
+
32
+ # Function to generate audio prompt using gTTS
33
  def generate_audio_prompt(text, filename):
34
  try:
35
  tts = gTTS(text)
36
  tts.save(os.path.join("static", filename))
37
  except Exception as e:
38
+ print(f"Error generating audio prompt: {e}")
39
+ time.sleep(5)
40
  generate_audio_prompt(text, filename)
41
 
42
+ # Example prompts for voice interaction
43
  prompts = {
44
  "welcome": "Welcome to Biryani Hub.",
45
  "ask_name": "Tell me your name.",
 
47
  "thank_you": "Thank you for registration."
48
  }
49
 
50
+ # Generate audio prompts
51
  for key, text in prompts.items():
52
  generate_audio_prompt(text, f"{key}.mp3")
53
 
54
+ # Function to check if the audio is silent
 
 
 
 
 
 
 
 
 
55
  def is_silent_audio(audio_path):
56
  audio = AudioSegment.from_wav(audio_path)
57
  nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
58
  return len(nonsilent_parts) == 0
59
 
60
+ # Function to fetch menu items from Salesforce
61
+ def get_menu_items():
62
+ try:
63
+ # Salesforce query to fetch all menu items
64
+ query = """
65
+ SELECT Name, Price__c, Ingredients__c, Category__c
66
+ FROM Menu_Item__c
67
+ """
68
+ result = sf.query(query)
69
+ menu_items = []
70
+ for item in result["records"]:
71
+ menu_items.append({
72
+ "name": item["Name"],
73
+ "price": item["Price__c"],
74
+ "ingredients": item["Ingredients__c"],
75
+ "category": item["Category__c"]
76
+ })
77
+ return menu_items
78
+ except Exception as e:
79
+ print(f"Error fetching menu items: {str(e)}")
80
+ return []
81
+
82
+ # Function to check if the customer exists in Salesforce (login check)
83
+ def get_customer_login(name, email, phone_number):
84
+ try:
85
+ # Salesforce query to fetch customer based on Name, Email, and Phone Number
86
+ query = f"""
87
+ SELECT Id, Name, Email__c, Phone_Number__c
88
+ FROM Customer_Login__c
89
+ WHERE Name = '{name}'
90
+ AND Email__c = '{email}'
91
+ AND Phone_Number__c = '{phone_number}'
92
+ """
93
+ result = sf.query(query)
94
+ if result["records"]:
95
+ customer = result["records"][0]
96
+ return {
97
+ "id": customer["Id"],
98
+ "name": customer["Name"],
99
+ "email": customer["Email__c"],
100
+ "phone": customer["Phone_Number__c"]
101
+ }
102
+ else:
103
+ return None
104
+ except Exception as e:
105
+ print(f"Error fetching customer login details: {str(e)}")
106
+ return None
107
+
108
+ # Function to create a new customer login in Salesforce
109
+ def create_customer_login(name, email, phone):
110
+ try:
111
+ # Create a new customer login record in Salesforce
112
+ customer_login = sf.Customer_Login__c.create({
113
+ 'Name': name,
114
+ 'Email__c': email,
115
+ 'Phone_Number__c': phone
116
+ })
117
+ return customer_login
118
+ except Exception as e:
119
+ print(f"Error creating customer login: {str(e)}")
120
+ return None
121
 
122
+ # Home Route (loads index.html)
123
  @app.route("/", methods=["GET"])
124
  def index():
125
  return render_template("index.html")
126
 
127
+ # Dashboard Route
128
  @app.route("/dashboard", methods=["GET"])
129
  def dashboard():
130
  return render_template("dashboard.html")
131
 
132
+ # Menu Page Route
133
  @app.route("/menu_page", methods=["GET"])
134
  def menu_page():
135
+ menu_items = get_menu_items()
136
+ return render_template("menu_page.html", menu=menu_items)
137
 
138
+ # Login API
139
  @app.route('/login', methods=['POST'])
140
  def login():
141
  data = request.json
 
146
  if not name or not email or not phone_number:
147
  return jsonify({'error': 'Missing required fields'}), 400
148
 
149
+ # Check if the customer exists in Salesforce
150
+ customer = get_customer_login(name, email, phone_number)
151
+ if customer:
152
+ return jsonify({'success': True, 'customer': customer}), 200
153
+ else:
154
+ return jsonify({'error': 'Customer not found'}), 404
 
 
 
155
 
156
+ # Register API (Create customer login)
157
  @app.route("/submit", methods=["POST"])
158
  def submit():
159
  data = request.json
 
164
  if not name or not email or not phone:
165
  return jsonify({'error': 'Missing data'}), 400
166
 
167
+ # Create customer login record in Salesforce
168
+ customer_login = create_customer_login(name, email, phone)
169
+ if customer_login:
 
 
 
170
  return jsonify({'success': True}), 200
171
+ else:
172
+ return jsonify({'error': 'Failed to create customer record'}), 500
173
 
174
+ # Transcribe Audio API
175
  @app.route("/transcribe", methods=["POST"])
176
  def transcribe():
177
  if "audio" not in request.files:
 
183
  audio_file.save(input_audio_path)
184
 
185
  try:
186
+ # Convert the audio to WAV format and check if it contains speech
187
  convert_to_wav(input_audio_path, output_audio_path)
188
  if is_silent_audio(output_audio_path):
189
  return jsonify({"error": "No speech detected. Please try again."}), 400
190
 
191
+ # Transcribe the audio using Whisper model
192
  asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
193
  result = asr_pipeline(output_audio_path)
194
 
195
  transcribed_text = result["text"].strip().capitalize()
196
 
197
+ # Extract details from transcribed text (Assumed format: Name Email Phone)
198
  parts = transcribed_text.split()
199
  name = parts[0] if len(parts) > 0 else "Unknown Name"
200
  email = parts[1] if '@' in parts[1] else "[email protected]"
 
203
  confirmation = f"Is this correct? Name: {name}, Email: {email}, Phone: {phone_number}"
204
  generate_audio_prompt(confirmation, "confirmation.mp3")
205
 
206
+ # Create a customer login record
207
  salesforce_response = sf.Customer_Login__c.create({
208
  'Name': name,
209
  'Email__c': email,
 
215
  except Exception as e:
216
  return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
217
 
218
+ # Start the Flask server
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
  if __name__ == "__main__":
220
+ print("Starting Flask API Server on port 7860...")
221
  serve(app, host="0.0.0.0", port=7860)