Update app.py
Browse files
app.py
CHANGED
|
@@ -71,13 +71,17 @@ app.layout = dbc.Container([
|
|
| 71 |
|
| 72 |
def transcribe_and_diarize_audio(contents, filename):
|
| 73 |
global generated_file, transcription_text
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
content_type, content_string = contents.split(',')
|
| 76 |
decoded = base64.b64decode(content_string)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
logger.info(f"File uploaded: {temp_audio_file_path}")
|
| 83 |
|
|
@@ -90,12 +94,14 @@ def transcribe_and_diarize_audio(contents, filename):
|
|
| 90 |
audio.export(wav_path, format="wav")
|
| 91 |
|
| 92 |
with open(wav_path, "rb") as audio_file:
|
|
|
|
| 93 |
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
# Format the diarized transcript
|
| 101 |
formatted_transcript = ""
|
|
@@ -114,10 +120,11 @@ def transcribe_and_diarize_audio(contents, filename):
|
|
| 114 |
logger.error(f"Error during transcription and diarization: {str(e)}")
|
| 115 |
return f"An error occurred during transcription and diarization: {str(e)}", False
|
| 116 |
finally:
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
| 121 |
|
| 122 |
@app.callback(
|
| 123 |
[Output('output-audio-upload', 'children'),
|
|
|
|
| 71 |
|
| 72 |
def transcribe_and_diarize_audio(contents, filename):
|
| 73 |
global generated_file, transcription_text
|
| 74 |
+
temp_audio_file = None
|
| 75 |
+
wav_file = None
|
| 76 |
try:
|
| 77 |
content_type, content_string = contents.split(',')
|
| 78 |
decoded = base64.b64decode(content_string)
|
| 79 |
|
| 80 |
+
# Create a temporary file that won't be immediately deleted
|
| 81 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(filename)[1])
|
| 82 |
+
temp_audio_file.write(decoded)
|
| 83 |
+
temp_audio_file.close() # Close the file but don't delete it yet
|
| 84 |
+
temp_audio_file_path = temp_audio_file.name
|
| 85 |
|
| 86 |
logger.info(f"File uploaded: {temp_audio_file_path}")
|
| 87 |
|
|
|
|
| 94 |
audio.export(wav_path, format="wav")
|
| 95 |
|
| 96 |
with open(wav_path, "rb") as audio_file:
|
| 97 |
+
# Transcribe
|
| 98 |
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 99 |
+
|
| 100 |
+
# Rewind the file for diarization
|
| 101 |
+
audio_file.seek(0)
|
| 102 |
+
|
| 103 |
+
# Perform diarization (speaker separation)
|
| 104 |
+
diarized_transcript = openai.Audio.transcribe("whisper-1", audio_file, speaker_detection=2)
|
| 105 |
|
| 106 |
# Format the diarized transcript
|
| 107 |
formatted_transcript = ""
|
|
|
|
| 120 |
logger.error(f"Error during transcription and diarization: {str(e)}")
|
| 121 |
return f"An error occurred during transcription and diarization: {str(e)}", False
|
| 122 |
finally:
|
| 123 |
+
# Clean up temporary files
|
| 124 |
+
if temp_audio_file:
|
| 125 |
+
os.unlink(temp_audio_file.name)
|
| 126 |
+
if wav_file:
|
| 127 |
+
os.unlink(wav_file)
|
| 128 |
|
| 129 |
@app.callback(
|
| 130 |
[Output('output-audio-upload', 'children'),
|