Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import ffmpeg
|
| 3 |
+
import os
|
| 4 |
+
import docx
|
| 5 |
+
import warnings
|
| 6 |
+
import assemblyai as aai
|
| 7 |
+
import subprocess
|
| 8 |
+
|
| 9 |
+
# Suppress FutureWarnings
|
| 10 |
+
warnings.simplefilter("ignore", category=FutureWarning)
|
| 11 |
+
|
| 12 |
+
Key = os.getenv("KeyA") # Ensure this is set in your environment
|
| 13 |
+
aai.settings.api_key = Key
|
| 14 |
+
|
| 15 |
+
# Function to check if FFmpeg is installed
|
| 16 |
+
def is_ffmpeg_installed():
|
| 17 |
+
try:
|
| 18 |
+
subprocess.run(["ffmpeg", "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
|
| 19 |
+
return True
|
| 20 |
+
except subprocess.CalledProcessError:
|
| 21 |
+
return False
|
| 22 |
+
except FileNotFoundError:
|
| 23 |
+
return False
|
| 24 |
+
|
| 25 |
+
# Function to extract audio from video safely
|
| 26 |
+
def extract_audio(video_path, output_audio_path="temp_audio.mp3"):
|
| 27 |
+
if not is_ffmpeg_installed():
|
| 28 |
+
raise RuntimeError("FFmpeg is not installed or not found in PATH.")
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
ffmpeg.input(video_path).output(output_audio_path, format="mp3").run(overwrite_output=True, quiet=True)
|
| 32 |
+
return output_audio_path
|
| 33 |
+
except ffmpeg.Error as e:
|
| 34 |
+
raise RuntimeError(f"FFmpeg error: {e.stderr.decode()}")
|
| 35 |
+
|
| 36 |
+
# Function to transcribe audio using AssemblyAI
|
| 37 |
+
def transcribe_audio(file):
|
| 38 |
+
ext = os.path.splitext(file.name)[-1].lower()
|
| 39 |
+
audio_path = "temp_audio.mp3"
|
| 40 |
+
|
| 41 |
+
# Extract audio if video is uploaded
|
| 42 |
+
if ext in [".mp4", ".avi", ".mov", ".mkv"]:
|
| 43 |
+
audio_path = extract_audio(file.name)
|
| 44 |
+
else:
|
| 45 |
+
audio_path = file.name # Use audio file directly
|
| 46 |
+
|
| 47 |
+
# Upload file to AssemblyAI
|
| 48 |
+
transcriber = aai.Transcriber()
|
| 49 |
+
config = aai.TranscriptionConfig(speaker_labels=True)
|
| 50 |
+
transcript = transcriber.transcribe(audio_path, config=config)
|
| 51 |
+
|
| 52 |
+
return "\n".join([f"Speaker {utt.speaker}: {utt.text}" for utt in transcript.utterances])
|
| 53 |
+
|
| 54 |
+
# Function to export transcription
|
| 55 |
+
def save_transcription(text, file_format):
|
| 56 |
+
file_path = f"transcription.{file_format.lower()}"
|
| 57 |
+
|
| 58 |
+
if file_format == "TXT":
|
| 59 |
+
with open(file_path, "w") as f:
|
| 60 |
+
f.write(text)
|
| 61 |
+
elif file_format == "DOCX":
|
| 62 |
+
doc = docx.Document()
|
| 63 |
+
doc.add_paragraph(text)
|
| 64 |
+
doc.save(file_path)
|
| 65 |
+
elif file_format == "SRT":
|
| 66 |
+
with open(file_path, "w") as f:
|
| 67 |
+
for i, line in enumerate(text.split(".")):
|
| 68 |
+
start_time = f"00:00:{i*5:02d},000"
|
| 69 |
+
end_time = f"00:00:{(i+1)*5:02d},000"
|
| 70 |
+
f.write(f"{i+1}\n{start_time} --> {end_time}\n{line.strip()}\n\n")
|
| 71 |
+
|
| 72 |
+
return file_path
|
| 73 |
+
|
| 74 |
+
# Gradio Interface
|
| 75 |
+
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("# 🎙️ Skroll - Audio & Video Transcription Tool")
|
| 77 |
+
gr.Markdown("Upload an audio or video file and transcribe. Export in .txt, .docx, or .srt format.")
|
| 78 |
+
|
| 79 |
+
file_input = gr.File(label="Upload Audio or Video")
|
| 80 |
+
transcript_output = gr.Textbox(label="Transcription", interactive=True, lines=10)
|
| 81 |
+
transcribe_btn = gr.Button("Transcribe")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
file_format = gr.Dropdown(["TXT", "DOCX", "SRT"], label="Export Format")
|
| 85 |
+
export_btn = gr.Button("Export")
|
| 86 |
+
|
| 87 |
+
download_link = gr.File(label="Download Transcription")
|
| 88 |
+
|
| 89 |
+
# Define Actions
|
| 90 |
+
transcribe_btn.click(transcribe_audio, inputs=[file_input], outputs=transcript_output)
|
| 91 |
+
export_btn.click(save_transcription, inputs=[transcript_output, file_format], outputs=download_link)
|
| 92 |
+
|
| 93 |
+
# Launch App
|
| 94 |
+
demo.launch(debug=True)
|