Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,9 @@ from transformers import pipeline
|
|
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 |
|
@@ -58,17 +59,25 @@ def is_silent_audio(audio_path):
|
|
58 |
nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16) # Reduced silence duration
|
59 |
return len(nonsilent_parts) == 0 # If no speech detected
|
60 |
|
61 |
-
#
|
62 |
-
|
|
|
|
|
|
|
63 |
|
64 |
-
#
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# Function to create Salesforce record
|
69 |
def create_salesforce_record(name, email, phone_number):
|
70 |
try:
|
71 |
-
#
|
72 |
customer_login = sf.Customer_Login__c.create({
|
73 |
'Name': name,
|
74 |
'Email__c': email,
|
@@ -76,21 +85,15 @@ def create_salesforce_record(name, email, phone_number):
|
|
76 |
})
|
77 |
|
78 |
# Log the response from Salesforce
|
79 |
-
print(f"Salesforce response: {customer_login}")
|
80 |
-
|
81 |
if customer_login.get('id'):
|
82 |
print(f"Record created successfully with ID: {customer_login['id']}")
|
|
|
83 |
else:
|
84 |
-
print("No ID returned
|
85 |
-
|
86 |
-
return customer_login
|
87 |
except Exception as e:
|
88 |
-
|
89 |
-
|
90 |
-
print(f"Error creating Salesforce record: {error_message}")
|
91 |
-
return {"error": f"Failed to create record in Salesforce: {error_message}"}
|
92 |
-
|
93 |
-
|
94 |
|
95 |
@app.route("/")
|
96 |
def index():
|
@@ -118,11 +121,8 @@ def transcribe():
|
|
118 |
result = asr_model(output_audio_path, generate_kwargs={"language": "en"})
|
119 |
transcribed_text = result["text"].strip().capitalize()
|
120 |
|
121 |
-
#
|
122 |
-
|
123 |
-
name = parts[0] # Assuming first word is the name
|
124 |
-
email = parts[1] if '@' in parts[1] else "[email protected]" # Very basic email extraction
|
125 |
-
phone_number = parts[2] if len(parts) > 2 else "0000000000" # Basic phone number assumption
|
126 |
|
127 |
# Create record in Salesforce
|
128 |
salesforce_response = create_salesforce_record(name, email, phone_number)
|
@@ -139,7 +139,6 @@ def transcribe():
|
|
139 |
print(f"Error in transcribing or processing: {str(e)}")
|
140 |
return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
|
141 |
|
142 |
-
|
143 |
# Start Production Server
|
144 |
if __name__ == "__main__":
|
145 |
serve(app, host="0.0.0.0", port=7860)
|
|
|
5 |
from gtts import gTTS
|
6 |
from pydub import AudioSegment
|
7 |
from pydub.silence import detect_nonsilent
|
8 |
+
from salesforce import get_salesforce_connection # Import the Salesforce connection function
|
9 |
+
import re
|
10 |
from waitress import serve
|
|
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
|
|
|
59 |
nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16) # Reduced silence duration
|
60 |
return len(nonsilent_parts) == 0 # If no speech detected
|
61 |
|
62 |
+
# Extract name, email, and phone number from transcribed text
|
63 |
+
def extract_name_email_phone(text):
|
64 |
+
# Regex for basic email and phone number
|
65 |
+
email = re.search(r'\S+@\S+', text)
|
66 |
+
phone = re.search(r'\+?\d{10,15}', text) # Consider different formats for phone numbers
|
67 |
|
68 |
+
name = text.split(' ')[0] # Simplified assumption that name is the first word
|
69 |
+
email = email.group(0) if email else "[email protected]"
|
70 |
+
phone = phone.group(0) if phone else "0000000000"
|
71 |
+
|
72 |
+
return name, email, phone
|
73 |
+
|
74 |
+
# Get Salesforce connection using salesforce.py
|
75 |
+
sf = get_salesforce_connection()
|
76 |
|
77 |
# Function to create Salesforce record
|
78 |
def create_salesforce_record(name, email, phone_number):
|
79 |
try:
|
80 |
+
# Create the record in Salesforce
|
81 |
customer_login = sf.Customer_Login__c.create({
|
82 |
'Name': name,
|
83 |
'Email__c': email,
|
|
|
85 |
})
|
86 |
|
87 |
# Log the response from Salesforce
|
|
|
|
|
88 |
if customer_login.get('id'):
|
89 |
print(f"Record created successfully with ID: {customer_login['id']}")
|
90 |
+
return customer_login
|
91 |
else:
|
92 |
+
print("Record creation failed: No ID returned")
|
93 |
+
return {"error": "Record creation failed: No ID returned"}
|
|
|
94 |
except Exception as e:
|
95 |
+
print(f"Error creating Salesforce record: {str(e)}")
|
96 |
+
return {"error": f"Failed to create record in Salesforce: {str(e)}"}
|
|
|
|
|
|
|
|
|
97 |
|
98 |
@app.route("/")
|
99 |
def index():
|
|
|
121 |
result = asr_model(output_audio_path, generate_kwargs={"language": "en"})
|
122 |
transcribed_text = result["text"].strip().capitalize()
|
123 |
|
124 |
+
# Extract name, email, and phone number from the transcribed text
|
125 |
+
name, email, phone_number = extract_name_email_phone(transcribed_text)
|
|
|
|
|
|
|
126 |
|
127 |
# Create record in Salesforce
|
128 |
salesforce_response = create_salesforce_record(name, email, phone_number)
|
|
|
139 |
print(f"Error in transcribing or processing: {str(e)}")
|
140 |
return jsonify({"error": f"Speech recognition error: {str(e)}"}), 500
|
141 |
|
|
|
142 |
# Start Production Server
|
143 |
if __name__ == "__main__":
|
144 |
serve(app, host="0.0.0.0", port=7860)
|