Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import assemblyai as aai
|
| 3 |
+
import re
|
| 4 |
+
import os
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
# Set your AssemblyAI API key
|
| 8 |
+
aai.settings.api_key = os.genenv('ASSEMBLYAI_API_KEY')
|
| 9 |
+
|
| 10 |
+
def create_assembly_transcript(audio_file):
|
| 11 |
+
transcriber = aai.Transcriber()
|
| 12 |
+
transcript = transcriber.transcribe(
|
| 13 |
+
audio_file.name, config=aai.TranscriptionConfig(speaker_labels=True)
|
| 14 |
+
)
|
| 15 |
+
return transcript
|
| 16 |
+
|
| 17 |
+
def transcript_to_string(transcript):
|
| 18 |
+
output = ""
|
| 19 |
+
for utterance in transcript.utterances:
|
| 20 |
+
name = f"SPEAKER {utterance.speaker}"
|
| 21 |
+
start_time = format_time(utterance.start)
|
| 22 |
+
output += f"{name} {start_time}\n{utterance.text}\n\n"
|
| 23 |
+
return output
|
| 24 |
+
|
| 25 |
+
def format_time(milliseconds):
|
| 26 |
+
seconds = milliseconds // 1000
|
| 27 |
+
hours, seconds = divmod(seconds, 3600)
|
| 28 |
+
minutes, seconds = divmod(seconds, 60)
|
| 29 |
+
return f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"
|
| 30 |
+
|
| 31 |
+
def format_transcript(transcript_string):
|
| 32 |
+
# Regex pattern for speaker labels
|
| 33 |
+
speaker_label_pattern = r"^(.+?)(?=\s\d{2}:\d{2}:\d{2})"
|
| 34 |
+
# Regex pattern for timestamps
|
| 35 |
+
timestamp_pattern = r"(\d{2}:\d{2}:\d{2})"
|
| 36 |
+
# Replace speaker labels with bold syntax
|
| 37 |
+
formatted_transcript = re.sub(
|
| 38 |
+
speaker_label_pattern, r"**\1**", transcript_string, flags=re.MULTILINE
|
| 39 |
+
)
|
| 40 |
+
# Replace timestamps with italicized syntax
|
| 41 |
+
formatted_transcript = re.sub(
|
| 42 |
+
timestamp_pattern, r"_\1_", formatted_transcript, flags=re.MULTILINE
|
| 43 |
+
)
|
| 44 |
+
return formatted_transcript
|
| 45 |
+
|
| 46 |
+
def transcribe_audio(audio_file):
|
| 47 |
+
if audio_file is None:
|
| 48 |
+
return "Please upload an audio file."
|
| 49 |
+
|
| 50 |
+
transcript = create_assembly_transcript(audio_file)
|
| 51 |
+
|
| 52 |
+
if transcript.error:
|
| 53 |
+
return f"An error occurred: {transcript.error}"
|
| 54 |
+
|
| 55 |
+
transcript_string = transcript_to_string(transcript)
|
| 56 |
+
md_transcript = format_transcript(transcript_string)
|
| 57 |
+
|
| 58 |
+
# Save the markdown transcript to a temporary file
|
| 59 |
+
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.md') as temp_file:
|
| 60 |
+
temp_file.write(md_transcript)
|
| 61 |
+
temp_file_path = temp_file.name
|
| 62 |
+
|
| 63 |
+
return md_transcript, temp_file_path
|
| 64 |
+
|
| 65 |
+
def launch_app():
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=transcribe_audio,
|
| 68 |
+
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 69 |
+
outputs=[
|
| 70 |
+
gr.Textbox(label="Transcript Preview", lines=10),
|
| 71 |
+
gr.File(label="Download Transcript")
|
| 72 |
+
],
|
| 73 |
+
title="Audio Transcription App",
|
| 74 |
+
description="Upload an audio file to get a transcription with speaker labels."
|
| 75 |
+
)
|
| 76 |
+
iface.launch()
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
launch_app()
|