Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,28 +5,20 @@ 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 |
-
import
|
| 11 |
-
from transformers import pipeline
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
|
| 15 |
-
retry_attempts = 3
|
| 16 |
-
timeout = 60 # 1 minute timeout for each attempt
|
| 17 |
-
model = None
|
| 18 |
-
for attempt in range(retry_attempts):
|
| 19 |
-
try:
|
| 20 |
-
model = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config={"timeout": timeout})
|
| 21 |
-
print("Model loaded successfully!")
|
| 22 |
-
break
|
| 23 |
-
except requests.exceptions.ReadTimeout:
|
| 24 |
-
print(f"Timeout occurred, retrying attempt {attempt + 1}/{retry_attempts}...")
|
| 25 |
-
time.sleep(5) # Retry after 5 seconds
|
| 26 |
-
|
| 27 |
# Use whisper-small for faster processing and better speed
|
| 28 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Function to generate audio prompts
|
| 32 |
def generate_audio_prompt(text, filename):
|
|
@@ -63,7 +55,6 @@ def convert_to_wav(input_path, output_path):
|
|
| 63 |
audio = AudioSegment.from_file(input_path)
|
| 64 |
audio = audio.set_frame_rate(16000).set_channels(1) # Convert to 16kHz, mono
|
| 65 |
audio.export(output_path, format="wav")
|
| 66 |
-
print(f"Converted audio to {output_path}")
|
| 67 |
except Exception as e:
|
| 68 |
print(f"Error: {str(e)}")
|
| 69 |
raise Exception(f"Audio conversion failed: {str(e)}")
|
|
@@ -110,7 +101,6 @@ def create_salesforce_record(name, email, phone_number):
|
|
| 110 |
print(f"Error creating Salesforce record: {error_message}")
|
| 111 |
return {"error": f"Failed to create record in Salesforce: {error_message}"}
|
| 112 |
|
| 113 |
-
|
| 114 |
@app.route("/")
|
| 115 |
def index():
|
| 116 |
return render_template("index.html")
|
|
@@ -137,7 +127,20 @@ def transcribe():
|
|
| 137 |
print("Audio contains speech, proceeding with transcription.")
|
| 138 |
|
| 139 |
# Use Whisper ASR model for transcription
|
| 140 |
-
result =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
transcribed_text = result["text"].strip().capitalize()
|
| 142 |
print(f"Transcribed text: {transcribed_text}")
|
| 143 |
|
|
|
|
| 5 |
from gtts import gTTS
|
| 6 |
from pydub import AudioSegment
|
| 7 |
from pydub.silence import detect_nonsilent
|
| 8 |
+
from transformers import AutoConfig # Import AutoConfig for the config object
|
| 9 |
+
import time
|
| 10 |
from waitress import serve
|
| 11 |
from simple_salesforce import Salesforce
|
| 12 |
+
import requests # Import requests for exception handling
|
|
|
|
| 13 |
|
| 14 |
app = Flask(__name__)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Use whisper-small for faster processing and better speed
|
| 17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
|
| 19 |
+
# Create config object to set timeout and other parameters
|
| 20 |
+
config = AutoConfig.from_pretrained("openai/whisper-small")
|
| 21 |
+
config.update({"timeout": 60}) # Set timeout to 60 seconds
|
| 22 |
|
| 23 |
# Function to generate audio prompts
|
| 24 |
def generate_audio_prompt(text, filename):
|
|
|
|
| 55 |
audio = AudioSegment.from_file(input_path)
|
| 56 |
audio = audio.set_frame_rate(16000).set_channels(1) # Convert to 16kHz, mono
|
| 57 |
audio.export(output_path, format="wav")
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error: {str(e)}")
|
| 60 |
raise Exception(f"Audio conversion failed: {str(e)}")
|
|
|
|
| 101 |
print(f"Error creating Salesforce record: {error_message}")
|
| 102 |
return {"error": f"Failed to create record in Salesforce: {error_message}"}
|
| 103 |
|
|
|
|
| 104 |
@app.route("/")
|
| 105 |
def index():
|
| 106 |
return render_template("index.html")
|
|
|
|
| 127 |
print("Audio contains speech, proceeding with transcription.")
|
| 128 |
|
| 129 |
# Use Whisper ASR model for transcription
|
| 130 |
+
result = None
|
| 131 |
+
retry_attempts = 3
|
| 132 |
+
for attempt in range(retry_attempts):
|
| 133 |
+
try:
|
| 134 |
+
result = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=0 if torch.cuda.is_available() else -1, config=config)
|
| 135 |
+
print(f"Transcribed text: {result['text']}")
|
| 136 |
+
break
|
| 137 |
+
except requests.exceptions.ReadTimeout:
|
| 138 |
+
print(f"Timeout occurred, retrying attempt {attempt + 1}/{retry_attempts}...")
|
| 139 |
+
time.sleep(5)
|
| 140 |
+
|
| 141 |
+
if result is None:
|
| 142 |
+
return jsonify({"error": "Unable to transcribe audio after retries."}), 500
|
| 143 |
+
|
| 144 |
transcribed_text = result["text"].strip().capitalize()
|
| 145 |
print(f"Transcribed text: {transcribed_text}")
|
| 146 |
|