Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,8 @@ 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 |
# Use whisper-small for faster processing and better speed
|
18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -21,15 +22,14 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
21 |
config = AutoConfig.from_pretrained("openai/whisper-small")
|
22 |
config.update({"timeout": 60}) # Set timeout to 60 seconds
|
23 |
|
24 |
-
#
|
25 |
def generate_audio_prompt(text, filename):
|
26 |
try:
|
27 |
tts = gTTS(text)
|
28 |
tts.save(os.path.join("static", filename))
|
29 |
-
except
|
30 |
print(f"Error: {e}")
|
31 |
-
|
32 |
-
time.sleep(5) # Wait for 5 seconds before retrying
|
33 |
generate_audio_prompt(text, filename)
|
34 |
|
35 |
# Generate required voice prompts
|
@@ -43,19 +43,6 @@ prompts = {
|
|
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:
|
@@ -63,32 +50,41 @@ def convert_to_wav(input_path, output_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)
|
73 |
-
|
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 |
-
#
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
@app.route('/login', methods=['POST'])
|
88 |
def login():
|
89 |
-
|
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')
|
@@ -96,7 +92,6 @@ def login():
|
|
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,
|
@@ -107,6 +102,7 @@ def login():
|
|
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():
|
112 |
data = request.json
|
@@ -118,30 +114,19 @@ 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,
|
125 |
'Phone_Number__c': phone
|
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,66 +135,55 @@ 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 |
-
# 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 |
-
# Confirm details before submission
|
188 |
confirmation = f"Is this correct? Name: {name}, Email: {email}, Phone: {phone_number}"
|
189 |
generate_audio_prompt(confirmation, "confirmation.mp3")
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
salesforce_response = create_salesforce_record(name, email, phone_number)
|
197 |
-
|
198 |
-
# Log the Salesforce response
|
199 |
-
print(f"Salesforce record creation response: {salesforce_response}")
|
200 |
-
|
201 |
-
# Check if the response contains an error
|
202 |
-
if "error" in salesforce_response:
|
203 |
-
print(f"Error creating record in Salesforce: {salesforce_response['error']}")
|
204 |
-
return jsonify(salesforce_response), 500
|
205 |
|
206 |
-
|
207 |
-
return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
|
208 |
|
209 |
except Exception as e:
|
210 |
-
print(f"Error in transcribing or processing: {str(e)}")
|
211 |
return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
|
212 |
|
213 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
if __name__ == "__main__":
|
215 |
-
|
|
|
|
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"
|
|
|
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
|
|
|
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:
|
|
|
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
|
|
|
|
|
88 |
name = data.get('name')
|
89 |
email = data.get('email')
|
90 |
phone_number = data.get('phone_number')
|
|
|
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,
|
|
|
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
|
|
|
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:
|
|
|
130 |
return jsonify({"error": "No audio file provided"}), 400
|
131 |
|
132 |
audio_file = request.files["audio"]
|
|
|
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]"
|
150 |
phone_number = parts[2] if len(parts) > 2 else "0000000000"
|
|
|
151 |
|
|
|
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,
|
158 |
+
'Phone_Number__c': phone_number
|
159 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
+
return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
|
|
|
162 |
|
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 |
+
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c"
|
171 |
+
result = sf.query(query)
|
172 |
+
|
173 |
+
menu_items = []
|
174 |
+
for item in result["records"]:
|
175 |
+
menu_items.append({
|
176 |
+
"name": item["Name"],
|
177 |
+
"price": item["Price__c"],
|
178 |
+
"ingredients": item["Ingredients__c"],
|
179 |
+
"category": item["Category__c"]
|
180 |
+
})
|
181 |
+
|
182 |
+
return jsonify({"success": True, "menu": menu_items})
|
183 |
+
except Exception as e:
|
184 |
+
return jsonify({"error": f"Failed to fetch menu: {str(e)}"}), 500
|
185 |
+
|
186 |
+
# β
START PRODUCTION SERVER
|
187 |
if __name__ == "__main__":
|
188 |
+
print("β
Starting Flask API Server on port 7860...")
|
189 |
+
serve(app, host="0.0.0.0", port=7860)
|