Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,52 @@
|
|
1 |
-
import torch
|
2 |
-
from flask import Flask, render_template, request, jsonify
|
3 |
-
import json
|
4 |
import os
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
17 |
-
#
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
|
|
21 |
config = AutoConfig.from_pretrained("openai/whisper-small")
|
22 |
-
config.update({"timeout": 60})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,96 +54,83 @@ 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)
|
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 |
|
46 |
-
# Symbol mapping for proper recognition
|
47 |
-
SYMBOL_MAPPING = {
|
48 |
-
"at the rate": "@",
|
49 |
-
"at": "@",
|
50 |
-
"dot": ".",
|
51 |
-
"underscore": "_",
|
52 |
-
"hash": "#",
|
53 |
-
"plus": "+",
|
54 |
-
"dash": "-",
|
55 |
-
"comma": ",",
|
56 |
-
"space": " "
|
57 |
-
}
|
58 |
-
|
59 |
# Function to convert audio to WAV format
|
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 |
-
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)
|
73 |
-
|
74 |
-
return len(nonsilent_parts) == 0 # If no speech detected
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
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 |
-
#
|
86 |
-
# API endpoint to receive data from voice bot
|
87 |
@app.route('/login', methods=['POST'])
|
88 |
def login():
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
name = data.get('name')
|
93 |
-
email = data.get('email')
|
94 |
-
phone_number = data.get('phone_number')
|
95 |
|
96 |
-
if not
|
97 |
-
return jsonify({'error': 'Missing
|
98 |
|
99 |
-
# Create a record in Salesforce
|
100 |
try:
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
except Exception as e:
|
108 |
-
|
|
|
109 |
|
110 |
-
|
111 |
-
|
|
|
112 |
data = request.json
|
113 |
name = data.get('name')
|
114 |
-
email = data.get('email')
|
115 |
-
phone = data.get('phone')
|
116 |
|
117 |
if not name or not email or not phone:
|
118 |
return jsonify({'error': 'Missing data'}), 400
|
119 |
|
120 |
try:
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
122 |
customer_login = sf.Customer_Login__c.create({
|
123 |
'Name': name,
|
124 |
'Email__c': email,
|
@@ -126,22 +138,17 @@ 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")
|
140 |
-
|
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"]
|
@@ -150,56 +157,17 @@ def transcribe():
|
|
150 |
audio_file.save(input_audio_path)
|
151 |
|
152 |
try:
|
153 |
-
# Convert to WAV
|
154 |
convert_to_wav(input_audio_path, output_audio_path)
|
155 |
|
156 |
-
# Check for silence
|
157 |
if is_silent_audio(output_audio_path):
|
158 |
return jsonify({"error": "No speech detected. Please try again."}), 400
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
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
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
+
import logging
|
4 |
+
import json
|
5 |
+
import requests
|
6 |
+
import torch
|
7 |
+
from flask import Flask, render_template, request, jsonify, session
|
8 |
+
from flask_session import Session
|
9 |
+
from simple_salesforce import Salesforce
|
10 |
+
from transformers import pipeline, AutoConfig
|
11 |
from gtts import gTTS
|
12 |
from pydub import AudioSegment
|
13 |
from pydub.silence import detect_nonsilent
|
|
|
|
|
14 |
from waitress import serve
|
|
|
|
|
15 |
|
16 |
app = Flask(__name__)
|
17 |
|
18 |
+
# Configure Flask session
|
19 |
+
app.secret_key = os.getenv("SECRET_KEY", "sSSjyhInIsUohKpG8sHzty2q")
|
20 |
+
app.config["SESSION_TYPE"] = "filesystem"
|
21 |
+
app.config["SESSION_COOKIE_NAME"] = "my_session"
|
22 |
+
app.config["SESSION_COOKIE_SECURE"] = True
|
23 |
+
app.config["SESSION_COOKIE_SAMESITE"] = "None"
|
24 |
+
Session(app)
|
25 |
+
|
26 |
+
# Set up logging
|
27 |
+
logging.basicConfig(level=logging.INFO)
|
28 |
+
|
29 |
+
# Connect to Salesforce
|
30 |
+
try:
|
31 |
+
print("Attempting to connect to Salesforce...")
|
32 |
+
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
33 |
+
print("Connected to Salesforce successfully!")
|
34 |
+
except Exception as e:
|
35 |
+
print(f"Failed to connect to Salesforce: {str(e)}")
|
36 |
|
37 |
+
# Whisper ASR Configuration
|
38 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
39 |
config = AutoConfig.from_pretrained("openai/whisper-small")
|
40 |
+
config.update({"timeout": 60})
|
41 |
+
|
42 |
+
# Voice prompts for registration
|
43 |
+
prompts = {
|
44 |
+
"welcome": "Welcome to Biryani Hub.",
|
45 |
+
"ask_name": "Tell me your name.",
|
46 |
+
"ask_email": "Please provide your email address.",
|
47 |
+
"thank_you": "Thank you for registration."
|
48 |
+
}
|
49 |
|
|
|
50 |
def generate_audio_prompt(text, filename):
|
51 |
try:
|
52 |
tts = gTTS(text)
|
|
|
54 |
except gtts.tts.gTTSError as e:
|
55 |
print(f"Error: {e}")
|
56 |
print("Retrying after 5 seconds...")
|
57 |
+
time.sleep(5)
|
58 |
generate_audio_prompt(text, filename)
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
for key, text in prompts.items():
|
61 |
generate_audio_prompt(text, f"{key}.mp3")
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
# Function to convert audio to WAV format
|
64 |
def convert_to_wav(input_path, output_path):
|
65 |
try:
|
66 |
audio = AudioSegment.from_file(input_path)
|
67 |
+
audio = audio.set_frame_rate(16000).set_channels(1)
|
68 |
audio.export(output_path, format="wav")
|
69 |
except Exception as e:
|
|
|
70 |
raise Exception(f"Audio conversion failed: {str(e)}")
|
71 |
|
72 |
# Function to check if audio contains actual speech
|
73 |
def is_silent_audio(audio_path):
|
74 |
audio = AudioSegment.from_wav(audio_path)
|
75 |
+
nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16)
|
76 |
+
return len(nonsilent_parts) == 0
|
|
|
77 |
|
78 |
+
@app.route("/")
|
79 |
+
def index():
|
80 |
+
return render_template("index.html")
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
# β
LOGIN ENDPOINT (Validates Existing Users)
|
|
|
83 |
@app.route('/login', methods=['POST'])
|
84 |
def login():
|
85 |
+
data = request.json
|
86 |
+
email = data.get('email').strip().lower()
|
87 |
+
phone_number = data.get('phone_number').strip()
|
|
|
|
|
|
|
88 |
|
89 |
+
if not email or not phone_number:
|
90 |
+
return jsonify({'error': 'Missing email or phone number'}), 400
|
91 |
|
|
|
92 |
try:
|
93 |
+
print(f"π Checking login for Email: {email}, Phone: {phone_number}")
|
94 |
+
|
95 |
+
query = f"SELECT Id, Name FROM Customer_Login__c WHERE LOWER(Email__c) = '{email}' AND Phone_Number__c = '{phone_number}' LIMIT 1"
|
96 |
+
result = sf.query(query)
|
97 |
+
|
98 |
+
if result['totalSize'] == 0:
|
99 |
+
print("β No matching records found!")
|
100 |
+
return jsonify({'error': 'Invalid email or phone number. User not found'}), 401
|
101 |
+
|
102 |
+
user_data = result['records'][0]
|
103 |
+
session['user_id'] = user_data['Id']
|
104 |
+
session['name'] = user_data['Name']
|
105 |
+
print("β
User found:", user_data)
|
106 |
+
|
107 |
+
return jsonify({'success': True, 'message': 'Login successful', 'user_id': user_data['Id'], 'name': user_data['Name']}), 200
|
108 |
+
|
109 |
+
except requests.exceptions.RequestException as req_error:
|
110 |
+
print("π΄ Salesforce Connection Error:", req_error)
|
111 |
+
return jsonify({'error': f'Salesforce connection error: {str(req_error)}'}), 500
|
112 |
except Exception as e:
|
113 |
+
print("π¨ Unexpected Error:", e)
|
114 |
+
return jsonify({'error': f'Unexpected error: {str(e)}'}), 500
|
115 |
|
116 |
+
# β
REGISTRATION ENDPOINT (Creates New User)
|
117 |
+
@app.route("/register", methods=["POST"])
|
118 |
+
def register():
|
119 |
data = request.json
|
120 |
name = data.get('name')
|
121 |
+
email = data.get('email').strip().lower()
|
122 |
+
phone = data.get('phone').strip()
|
123 |
|
124 |
if not name or not email or not phone:
|
125 |
return jsonify({'error': 'Missing data'}), 400
|
126 |
|
127 |
try:
|
128 |
+
query = f"SELECT Id FROM Customer_Login__c WHERE LOWER(Email__c) = '{email}' AND Phone_Number__c = '{phone}' LIMIT 1"
|
129 |
+
existing_user = sf.query(query)
|
130 |
+
|
131 |
+
if existing_user['totalSize'] > 0:
|
132 |
+
return jsonify({'error': 'User already exists'}), 409
|
133 |
+
|
134 |
customer_login = sf.Customer_Login__c.create({
|
135 |
'Name': name,
|
136 |
'Email__c': email,
|
|
|
138 |
})
|
139 |
|
140 |
if customer_login.get('id'):
|
141 |
+
return jsonify({'success': True, 'user_id': customer_login['id']}), 200
|
142 |
else:
|
143 |
return jsonify({'error': 'Failed to create record'}), 500
|
144 |
|
145 |
except Exception as e:
|
146 |
return jsonify({'error': str(e)}), 500
|
147 |
|
148 |
+
# β
TRANSCRIPTION ENDPOINT (Converts Speech to Text)
|
|
|
|
|
|
|
|
|
149 |
@app.route("/transcribe", methods=["POST"])
|
150 |
def transcribe():
|
151 |
if "audio" not in request.files:
|
|
|
152 |
return jsonify({"error": "No audio file provided"}), 400
|
153 |
|
154 |
audio_file = request.files["audio"]
|
|
|
157 |
audio_file.save(input_audio_path)
|
158 |
|
159 |
try:
|
|
|
160 |
convert_to_wav(input_audio_path, output_audio_path)
|
161 |
|
|
|
162 |
if is_silent_audio(output_audio_path):
|
163 |
return jsonify({"error": "No speech detected. Please try again."}), 400
|
164 |
+
|
165 |
+
result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
|
166 |
+
transcribed_text = result(output_audio_path)["text"].strip().capitalize()
|
167 |
+
|
168 |
+
return jsonify({"text": transcribed_text})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
except Exception as e:
|
|
|
171 |
return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
|
172 |
|
173 |
# Start Production Server
|