Update app.py
Browse files
app.py
CHANGED
@@ -5,24 +5,11 @@ from transformers import pipeline
|
|
5 |
from gtts import gTTS
|
6 |
from pydub import AudioSegment
|
7 |
from pydub.silence import detect_nonsilent
|
8 |
-
from simple_salesforce import Salesforce # Import Salesforce connection
|
9 |
-
import re
|
10 |
from waitress import serve
|
|
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
|
14 |
-
# Salesforce connection using provided credentials
|
15 |
-
sf_username = '[email protected]'
|
16 |
-
sf_password = 'Sati@1020'
|
17 |
-
sf_token = 'sSSjyhInIsUohKpG8sHzty2q'
|
18 |
-
|
19 |
-
# Establish Salesforce connection
|
20 |
-
try:
|
21 |
-
sf = Salesforce(username=sf_username, password=sf_password, security_token=sf_token)
|
22 |
-
print("Connected to Salesforce successfully!")
|
23 |
-
except Exception as e:
|
24 |
-
print(f"Failed to connect to Salesforce: {str(e)}")
|
25 |
-
|
26 |
# Use whisper-small for faster processing and better speed
|
27 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
28 |
asr_model = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if device == "cuda" else -1)
|
@@ -62,47 +49,49 @@ def convert_to_wav(input_path, output_path):
|
|
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 |
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) # Reduced silence duration
|
|
|
72 |
return len(nonsilent_parts) == 0 # If no speech detected
|
73 |
|
74 |
-
#
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
phone = phone.group(0) if phone else "0000000000"
|
83 |
-
|
84 |
-
return name, email, phone
|
85 |
|
86 |
# Function to create Salesforce record
|
87 |
def create_salesforce_record(name, email, phone_number):
|
88 |
try:
|
89 |
-
#
|
90 |
customer_login = sf.Customer_Login__c.create({
|
91 |
'Name': name,
|
92 |
'Email__c': email,
|
93 |
'Phone_Number__c': phone_number
|
94 |
})
|
95 |
-
|
96 |
-
|
97 |
if customer_login.get('id'):
|
98 |
print(f"Record created successfully with ID: {customer_login['id']}")
|
99 |
return customer_login
|
100 |
else:
|
101 |
-
print("
|
102 |
return {"error": "Record creation failed: No ID returned"}
|
103 |
except Exception as e:
|
104 |
-
|
105 |
-
|
|
|
|
|
106 |
|
107 |
@app.route("/")
|
108 |
def index():
|
@@ -111,6 +100,7 @@ def index():
|
|
111 |
@app.route("/transcribe", methods=["POST"])
|
112 |
def transcribe():
|
113 |
if "audio" not in request.files:
|
|
|
114 |
return jsonify({"error": "No audio file provided"}), 400
|
115 |
|
116 |
audio_file = request.files["audio"]
|
@@ -125,13 +115,20 @@ def transcribe():
|
|
125 |
# Check for silence
|
126 |
if is_silent_audio(output_audio_path):
|
127 |
return jsonify({"error": "No speech detected. Please try again."}), 400
|
|
|
|
|
128 |
|
129 |
# Use Whisper ASR model for transcription
|
130 |
result = asr_model(output_audio_path, generate_kwargs={"language": "en"})
|
131 |
transcribed_text = result["text"].strip().capitalize()
|
|
|
132 |
|
133 |
# Extract name, email, and phone number from the transcribed text
|
134 |
-
|
|
|
|
|
|
|
|
|
135 |
|
136 |
# Create record in Salesforce
|
137 |
salesforce_response = create_salesforce_record(name, email, phone_number)
|
@@ -140,7 +137,7 @@ def transcribe():
|
|
140 |
if "error" in salesforce_response:
|
141 |
print(f"Error creating record in Salesforce: {salesforce_response['error']}")
|
142 |
return jsonify(salesforce_response), 500
|
143 |
-
|
144 |
print(f"Salesforce Response: {salesforce_response}")
|
145 |
return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
|
146 |
|
|
|
5 |
from gtts import gTTS
|
6 |
from pydub import AudioSegment
|
7 |
from pydub.silence import detect_nonsilent
|
|
|
|
|
8 |
from waitress import serve
|
9 |
+
from simple_salesforce import Salesforce
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Use whisper-small for faster processing and better speed
|
14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
asr_model = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if device == "cuda" else -1)
|
|
|
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 |
+
print(f"Converted audio to {output_path}")
|
53 |
except Exception as e:
|
54 |
+
print(f"Error: {str(e)}")
|
55 |
raise Exception(f"Audio conversion failed: {str(e)}")
|
56 |
|
57 |
# Function to check if audio contains actual speech
|
58 |
def is_silent_audio(audio_path):
|
59 |
audio = AudioSegment.from_wav(audio_path)
|
60 |
nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16) # Reduced silence duration
|
61 |
+
print(f"Detected nonsilent parts: {nonsilent_parts}")
|
62 |
return len(nonsilent_parts) == 0 # If no speech detected
|
63 |
|
64 |
+
# Salesforce connection details
|
65 |
+
try:
|
66 |
+
print("Attempting to connect to Salesforce...")
|
67 |
+
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
68 |
+
print("Connected to Salesforce successfully!")
|
69 |
+
print("User Info:", sf.UserInfo) # Log the user info to verify the connection
|
70 |
+
except Exception as e:
|
71 |
+
print(f"Failed to connect to Salesforce: {str(e)}")
|
|
|
|
|
|
|
72 |
|
73 |
# Function to create Salesforce record
|
74 |
def create_salesforce_record(name, email, phone_number):
|
75 |
try:
|
76 |
+
# Attempt to create a record in Salesforce
|
77 |
customer_login = sf.Customer_Login__c.create({
|
78 |
'Name': name,
|
79 |
'Email__c': email,
|
80 |
'Phone_Number__c': phone_number
|
81 |
})
|
82 |
+
print(f"Salesforce response: {customer_login}")
|
83 |
+
|
84 |
if customer_login.get('id'):
|
85 |
print(f"Record created successfully with ID: {customer_login['id']}")
|
86 |
return customer_login
|
87 |
else:
|
88 |
+
print("No ID returned, record creation may have failed.")
|
89 |
return {"error": "Record creation failed: No ID returned"}
|
90 |
except Exception as e:
|
91 |
+
# Catch and log any exceptions during record creation
|
92 |
+
error_message = str(e)
|
93 |
+
print(f"Error creating Salesforce record: {error_message}")
|
94 |
+
return {"error": f"Failed to create record in Salesforce: {error_message}"}
|
95 |
|
96 |
@app.route("/")
|
97 |
def index():
|
|
|
100 |
@app.route("/transcribe", methods=["POST"])
|
101 |
def transcribe():
|
102 |
if "audio" not in request.files:
|
103 |
+
print("No audio file provided")
|
104 |
return jsonify({"error": "No audio file provided"}), 400
|
105 |
|
106 |
audio_file = request.files["audio"]
|
|
|
115 |
# Check for silence
|
116 |
if is_silent_audio(output_audio_path):
|
117 |
return jsonify({"error": "No speech detected. Please try again."}), 400
|
118 |
+
else:
|
119 |
+
print("Audio contains speech, proceeding with transcription.")
|
120 |
|
121 |
# Use Whisper ASR model for transcription
|
122 |
result = asr_model(output_audio_path, generate_kwargs={"language": "en"})
|
123 |
transcribed_text = result["text"].strip().capitalize()
|
124 |
+
print(f"Transcribed text: {transcribed_text}")
|
125 |
|
126 |
# Extract name, email, and phone number from the transcribed text
|
127 |
+
parts = transcribed_text.split()
|
128 |
+
name = parts[0] if len(parts) > 0 else "Unknown Name"
|
129 |
+
email = parts[1] if '@' in parts[1] else "[email protected]"
|
130 |
+
phone_number = parts[2] if len(parts) > 2 else "0000000000"
|
131 |
+
print(f"Parsed data - Name: {name}, Email: {email}, Phone Number: {phone_number}")
|
132 |
|
133 |
# Create record in Salesforce
|
134 |
salesforce_response = create_salesforce_record(name, email, phone_number)
|
|
|
137 |
if "error" in salesforce_response:
|
138 |
print(f"Error creating record in Salesforce: {salesforce_response['error']}")
|
139 |
return jsonify(salesforce_response), 500
|
140 |
+
|
141 |
print(f"Salesforce Response: {salesforce_response}")
|
142 |
return jsonify({"text": transcribed_text, "salesforce_record": salesforce_response})
|
143 |
|