Update app.py
Browse files
app.py
CHANGED
|
@@ -76,12 +76,22 @@ app.layout = dbc.Container([
|
|
| 76 |
])
|
| 77 |
], fluid=True)
|
| 78 |
|
| 79 |
-
def chunk_audio(audio_segment,
|
| 80 |
chunks = []
|
| 81 |
-
for i in range(0, len(audio_segment),
|
| 82 |
-
chunks.append(audio_segment[i:i+
|
| 83 |
return chunks
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def process_media(file_path, is_url=False):
|
| 86 |
global generated_file, transcription_text
|
| 87 |
temp_file = None
|
|
@@ -89,22 +99,22 @@ def process_media(file_path, is_url=False):
|
|
| 89 |
try:
|
| 90 |
if is_url:
|
| 91 |
logger.info(f"Processing URL: {file_path}")
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
temp_file.close()
|
| 103 |
else:
|
| 104 |
logger.info("Processing uploaded file")
|
| 105 |
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
| 106 |
temp_file.write(file_path)
|
| 107 |
temp_file.close()
|
|
|
|
| 108 |
|
| 109 |
file_extension = os.path.splitext(temp_file.name)[1].lower()
|
| 110 |
logger.info(f"Detected file extension: {file_extension}")
|
|
@@ -127,26 +137,20 @@ def process_media(file_path, is_url=False):
|
|
| 127 |
|
| 128 |
logger.info(f"Audio extracted to WAV: {wav_path}")
|
| 129 |
|
|
|
|
| 130 |
audio = AudioSegment.from_wav(wav_path)
|
| 131 |
chunks = chunk_audio(audio)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
with open(chunk_file.name, "rb") as audio_file:
|
| 140 |
-
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 141 |
-
full_transcript += transcript.get('text', '') + " "
|
| 142 |
-
|
| 143 |
-
os.unlink(chunk_file.name)
|
| 144 |
-
|
| 145 |
-
formatted_transcript = full_transcript.strip()
|
| 146 |
transcription_text = formatted_transcript
|
| 147 |
generated_file = io.BytesIO(transcription_text.encode())
|
| 148 |
-
logger.info("Transcription completed successfully")
|
| 149 |
-
return "Transcription completed successfully!", True
|
| 150 |
except Exception as e:
|
| 151 |
logger.error(f"Error during processing: {str(e)}")
|
| 152 |
return f"An error occurred: {str(e)}", False
|
|
@@ -197,7 +201,7 @@ def update_output(contents, n_clicks, filename, url):
|
|
| 197 |
def download_transcription(n_clicks):
|
| 198 |
if n_clicks is None:
|
| 199 |
return None
|
| 200 |
-
return dcc.send_bytes(generated_file.getvalue(), "
|
| 201 |
|
| 202 |
if __name__ == '__main__':
|
| 203 |
print("Starting the Dash application...")
|
|
|
|
| 76 |
])
|
| 77 |
], fluid=True)
|
| 78 |
|
| 79 |
+
def chunk_audio(audio_segment, chunk_size_ms=60000):
|
| 80 |
chunks = []
|
| 81 |
+
for i in range(0, len(audio_segment), chunk_size_ms):
|
| 82 |
+
chunks.append(audio_segment[i:i+chunk_size_ms])
|
| 83 |
return chunks
|
| 84 |
|
| 85 |
+
def transcribe_audio_chunks(chunks):
|
| 86 |
+
transcriptions = []
|
| 87 |
+
for chunk in chunks:
|
| 88 |
+
with io.BytesIO() as audio_file:
|
| 89 |
+
chunk.export(audio_file, format="wav")
|
| 90 |
+
audio_file.seek(0)
|
| 91 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 92 |
+
transcriptions.append(transcript.get('text', ''))
|
| 93 |
+
return ' '.join(transcriptions)
|
| 94 |
+
|
| 95 |
def process_media(file_path, is_url=False):
|
| 96 |
global generated_file, transcription_text
|
| 97 |
temp_file = None
|
|
|
|
| 99 |
try:
|
| 100 |
if is_url:
|
| 101 |
logger.info(f"Processing URL: {file_path}")
|
| 102 |
+
try:
|
| 103 |
+
response = requests.get(file_path)
|
| 104 |
+
response.raise_for_status()
|
| 105 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 106 |
+
temp_file.write(response.content)
|
| 107 |
+
temp_file.close()
|
| 108 |
+
logger.info(f"URL content downloaded: {temp_file.name}")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
logger.error(f"Error downloading URL content: {str(e)}")
|
| 111 |
+
return f"Error downloading URL content: {str(e)}", False
|
|
|
|
| 112 |
else:
|
| 113 |
logger.info("Processing uploaded file")
|
| 114 |
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
| 115 |
temp_file.write(file_path)
|
| 116 |
temp_file.close()
|
| 117 |
+
logger.info(f"Uploaded file saved: {temp_file.name}")
|
| 118 |
|
| 119 |
file_extension = os.path.splitext(temp_file.name)[1].lower()
|
| 120 |
logger.info(f"Detected file extension: {file_extension}")
|
|
|
|
| 137 |
|
| 138 |
logger.info(f"Audio extracted to WAV: {wav_path}")
|
| 139 |
|
| 140 |
+
# Chunk the audio file
|
| 141 |
audio = AudioSegment.from_wav(wav_path)
|
| 142 |
chunks = chunk_audio(audio)
|
| 143 |
|
| 144 |
+
# Transcribe chunks
|
| 145 |
+
transcription = transcribe_audio_chunks(chunks)
|
| 146 |
+
|
| 147 |
+
# Diarization (simplified as OpenAI doesn't provide speaker diarization)
|
| 148 |
+
formatted_transcript = f"Speaker 1: {transcription}"
|
| 149 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
transcription_text = formatted_transcript
|
| 151 |
generated_file = io.BytesIO(transcription_text.encode())
|
| 152 |
+
logger.info("Transcription and diarization completed successfully")
|
| 153 |
+
return "Transcription and diarization completed successfully!", True
|
| 154 |
except Exception as e:
|
| 155 |
logger.error(f"Error during processing: {str(e)}")
|
| 156 |
return f"An error occurred: {str(e)}", False
|
|
|
|
| 201 |
def download_transcription(n_clicks):
|
| 202 |
if n_clicks is None:
|
| 203 |
return None
|
| 204 |
+
return dcc.send_bytes(generated_file.getvalue(), "diarized_transcription.txt")
|
| 205 |
|
| 206 |
if __name__ == '__main__':
|
| 207 |
print("Starting the Dash application...")
|