lokesh341 commited on
Commit
37d87bb
·
verified ·
1 Parent(s): cc4f4bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -79
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 # 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__)
16
 
@@ -19,9 +19,17 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
19
 
20
  # Create config object to set timeout and other parameters
21
  config = AutoConfig.from_pretrained("openai/whisper-small")
22
- config.update({"timeout": 60}) # Set timeout to 60 seconds
23
 
24
- # Your function where you generate and save the audio
 
 
 
 
 
 
 
 
25
  def generate_audio_prompt(text, filename):
26
  try:
27
  tts = gTTS(text)
@@ -29,17 +37,9 @@ def generate_audio_prompt(text, filename):
29
  except gtts.tts.gTTSError as e:
30
  print(f"Error: {e}")
31
  print("Retrying after 5 seconds...")
32
- time.sleep(5) # Wait for 5 seconds 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.",
39
- "ask_email": "Please provide your email address.",
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
 
@@ -60,52 +60,50 @@ SYMBOL_MAPPING = {
60
  def convert_to_wav(input_path, output_path):
61
  try:
62
  audio = AudioSegment.from_file(input_path)
63
- audio = audio.set_frame_rate(16000).set_channels(1) # Convert to 16kHz, mono
64
  audio.export(output_path, format="wav")
65
  except Exception as e:
66
- print(f"Error: {str(e)}")
67
  raise Exception(f"Audio conversion failed: {str(e)}")
68
 
69
  # Function to check if audio contains actual speech
70
  def is_silent_audio(audio_path):
71
  audio = AudioSegment.from_wav(audio_path)
72
- nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16) # Reduced silence duration
73
- print(f"Detected nonsilent parts: {nonsilent_parts}")
74
- return len(nonsilent_parts) == 0 # If no speech detected
75
 
76
  # Salesforce connection details
77
  try:
78
  print("Attempting to connect to Salesforce...")
79
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
80
  print("Connected to Salesforce successfully!")
81
- print("User Info:", sf.UserInfo) # Log the user info to verify the connection
82
  except Exception as e:
83
  print(f"Failed to connect to Salesforce: {str(e)}")
84
 
85
- # Function to create Salesforce record
86
- # API endpoint to receive data from voice bot
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
-
92
- name = data.get('name')
93
  email = data.get('email')
94
  phone_number = data.get('phone_number')
95
 
96
- if not name or not email or not phone_number:
97
- return jsonify({'error': 'Missing required fields'}), 400
98
 
99
- # Create a record in Salesforce
100
  try:
101
- customer_login = sf.Customer_Login__c.create({
102
- 'Name': name,
103
- 'Email__c': email,
104
- 'Phone_Number__c': phone_number
105
- })
106
- return jsonify({'success': True, 'id': customer_login['id']}), 200
 
 
 
 
 
 
107
  except Exception as e:
108
- return jsonify({'error': f'Failed to create record in Salesforce: {str(e)}'}), 500
109
 
110
  @app.route("/submit", methods=["POST"])
111
  def submit():
@@ -118,7 +116,14 @@ def submit():
118
  return jsonify({'error': 'Missing data'}), 400
119
 
120
  try:
121
- # Create Salesforce record
 
 
 
 
 
 
 
122
  customer_login = sf.Customer_Login__c.create({
123
  'Name': name,
124
  'Email__c': email,
@@ -126,14 +131,13 @@ def submit():
126
  })
127
 
128
  if customer_login.get('id'):
129
- return jsonify({'success': True})
130
  else:
131
  return jsonify({'error': 'Failed to create record'}), 500
132
 
133
  except Exception as e:
134
  return jsonify({'error': str(e)}), 500
135
 
136
-
137
  @app.route("/")
138
  def index():
139
  return render_template("index.html")
@@ -141,7 +145,6 @@ def index():
141
  @app.route("/transcribe", methods=["POST"])
142
  def transcribe():
143
  if "audio" not in request.files:
144
- print("No audio file provided")
145
  return jsonify({"error": "No audio file provided"}), 400
146
 
147
  audio_file = request.files["audio"]
@@ -156,50 +159,14 @@ def transcribe():
156
  # Check for silence
157
  if is_silent_audio(output_audio_path):
158
  return jsonify({"error": "No speech detected. Please try again."}), 400
159
- else:
160
- print("Audio contains speech, proceeding with transcription.")
161
 
162
  # Use Whisper ASR model for transcription
163
- result = None
164
- retry_attempts = 3
165
- for attempt in range(retry_attempts):
166
- try:
167
- result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
168
- print(f"Transcribed text: {result['text']}")
169
- break
170
- except requests.exceptions.ReadTimeout:
171
- print(f"Timeout occurred, retrying attempt {attempt + 1}/{retry_attempts}...")
172
- time.sleep(5)
173
-
174
- if result is None:
175
- return jsonify({"error": "Unable to transcribe audio after retries."}), 500
176
-
177
- transcribed_text = result["text"].strip().capitalize()
178
- print(f"Transcribed text: {transcribed_text}")
179
-
180
- # Extract name, email, and phone number from the transcribed text
181
- parts = transcribed_text.split()
182
- name = parts[0] if len(parts) > 0 else "Unknown Name"
183
- email = parts[1] if '@' in parts[1] else "[email protected]"
184
- phone_number = parts[2] if len(parts) > 2 else "0000000000"
185
- print(f"Parsed data - Name: {name}, Email: {email}, Phone Number: {phone_number}")
186
-
187
- # Create record in Salesforce
188
- salesforce_response = create_salesforce_record(name, email, phone_number)
189
-
190
- # Log the Salesforce response
191
- print(f"Salesforce record creation response: {salesforce_response}")
192
-
193
- # Check if the response contains an error
194
- if "error" in salesforce_response:
195
- print(f"Error creating record in Salesforce: {salesforce_response['error']}")
196
- return jsonify(salesforce_response), 500
197
-
198
- # If creation was successful, return the details
199
- return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
200
 
201
  except Exception as e:
202
- print(f"Error in transcribing or processing: {str(e)}")
203
  return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
204
 
205
  # Start Production Server
 
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
 
 
19
 
20
  # Create config object to set timeout and other parameters
21
  config = AutoConfig.from_pretrained("openai/whisper-small")
22
+ config.update({"timeout": 60})
23
 
24
+ # Generate required voice prompts
25
+ prompts = {
26
+ "welcome": "Welcome to Biryani Hub.",
27
+ "ask_name": "Tell me your name.",
28
+ "ask_email": "Please provide your email address.",
29
+ "thank_you": "Thank you for registration."
30
+ }
31
+
32
+ # Function to generate and save audio prompts
33
  def generate_audio_prompt(text, filename):
34
  try:
35
  tts = gTTS(text)
 
37
  except gtts.tts.gTTSError as e:
38
  print(f"Error: {e}")
39
  print("Retrying after 5 seconds...")
40
+ time.sleep(5)
41
  generate_audio_prompt(text, filename)
42
 
 
 
 
 
 
 
 
 
43
  for key, text in prompts.items():
44
  generate_audio_prompt(text, f"{key}.mp3")
45
 
 
60
  def convert_to_wav(input_path, output_path):
61
  try:
62
  audio = AudioSegment.from_file(input_path)
63
+ audio = audio.set_frame_rate(16000).set_channels(1)
64
  audio.export(output_path, format="wav")
65
  except Exception as e:
 
66
  raise Exception(f"Audio conversion failed: {str(e)}")
67
 
68
  # Function to check if audio contains actual speech
69
  def is_silent_audio(audio_path):
70
  audio = AudioSegment.from_wav(audio_path)
71
+ nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
72
+ return len(nonsilent_parts) == 0
 
73
 
74
  # Salesforce connection details
75
  try:
76
  print("Attempting to connect to Salesforce...")
77
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
78
  print("Connected to Salesforce successfully!")
 
79
  except Exception as e:
80
  print(f"Failed to connect to Salesforce: {str(e)}")
81
 
82
+ # Function to handle login & registration in Salesforce
 
83
  @app.route('/login', methods=['POST'])
84
  def login():
85
+ data = request.json
 
 
 
86
  email = data.get('email')
87
  phone_number = data.get('phone_number')
88
 
89
+ if not email or not phone_number:
90
+ return jsonify({'error': 'Missing email or phone number'}), 400
91
 
 
92
  try:
93
+ # Check if user already exists
94
+ query = f"SELECT Id, Name FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone_number}' LIMIT 1"
95
+ result = sf.query(query)
96
+
97
+ if result['totalSize'] > 0:
98
+ user_data = result['records'][0]
99
+ return jsonify({'success': True, 'message': 'Login successful', 'user_id': user_data['Id'], 'name': user_data['Name']}), 200
100
+ else:
101
+ return jsonify({'error': 'Invalid email or phone number. User not found'}), 401
102
+
103
+ except requests.exceptions.RequestException as req_error:
104
+ return jsonify({'error': f'Salesforce connection error: {str(req_error)}'}), 500
105
  except Exception as e:
106
+ return jsonify({'error': f'Unexpected error: {str(e)}'}), 500
107
 
108
  @app.route("/submit", methods=["POST"])
109
  def submit():
 
116
  return jsonify({'error': 'Missing data'}), 400
117
 
118
  try:
119
+ # Check if user already exists
120
+ query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}' AND Phone_Number__c = '{phone}' LIMIT 1"
121
+ existing_user = sf.query(query)
122
+
123
+ if existing_user['totalSize'] > 0:
124
+ return jsonify({'error': 'User already exists'}), 409 # Conflict
125
+
126
+ # Create new user
127
  customer_login = sf.Customer_Login__c.create({
128
  'Name': name,
129
  'Email__c': email,
 
131
  })
132
 
133
  if customer_login.get('id'):
134
+ return jsonify({'success': True, 'user_id': customer_login['id']}), 200
135
  else:
136
  return jsonify({'error': 'Failed to create record'}), 500
137
 
138
  except Exception as e:
139
  return jsonify({'error': str(e)}), 500
140
 
 
141
  @app.route("/")
142
  def index():
143
  return render_template("index.html")
 
145
  @app.route("/transcribe", methods=["POST"])
146
  def transcribe():
147
  if "audio" not in request.files:
 
148
  return jsonify({"error": "No audio file provided"}), 400
149
 
150
  audio_file = request.files["audio"]
 
159
  # Check for silence
160
  if is_silent_audio(output_audio_path):
161
  return jsonify({"error": "No speech detected. Please try again."}), 400
 
 
162
 
163
  # Use Whisper ASR model for transcription
164
+ result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
165
+ transcribed_text = result(output_audio_path)["text"].strip().capitalize()
166
+
167
+ return jsonify({"text": transcribed_text})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  except Exception as e:
 
170
  return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
171
 
172
  # Start Production Server