Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import whisper
|
| 4 |
+
import subprocess
|
| 5 |
+
|
| 6 |
+
# Install FFmpeg (required for audio extraction)
|
| 7 |
+
os.system("apt update && apt install -y ffmpeg")
|
| 8 |
+
|
| 9 |
+
# Load Whisper Model
|
| 10 |
+
model = whisper.load_model("base") # Change to "tiny", "small", etc. for performance
|
| 11 |
+
|
| 12 |
+
def process_video(video_file):
|
| 13 |
+
"""
|
| 14 |
+
Extracts audio from the uploaded video, transcribes it using Whisper, and returns the text.
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
# Define File Paths
|
| 18 |
+
video_path = video_file.name
|
| 19 |
+
audio_file = "extracted_audio.wav"
|
| 20 |
+
transcription_file = "transcription.txt"
|
| 21 |
+
|
| 22 |
+
# Extract Audio Using FFmpeg
|
| 23 |
+
command = f"ffmpeg -i {video_path} -vn -acodec pcm_s16le -ar 16000 -ac 1 {audio_file}"
|
| 24 |
+
subprocess.run(command, shell=True, check=True)
|
| 25 |
+
|
| 26 |
+
# Transcribe Audio
|
| 27 |
+
result = model.transcribe(audio_file)
|
| 28 |
+
|
| 29 |
+
# Save Transcription to a Text File
|
| 30 |
+
with open(transcription_file, "w", encoding="utf-8") as f:
|
| 31 |
+
f.write(result["text"])
|
| 32 |
+
|
| 33 |
+
return result["text"], transcription_file
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"❌ Error: {e}", None
|
| 37 |
+
|
| 38 |
+
# Gradio Interface
|
| 39 |
+
with gr.Blocks() as app:
|
| 40 |
+
gr.Markdown("# 🎙 Video to Text Transcription using Whisper")
|
| 41 |
+
gr.Markdown("Upload a video file, and the AI will transcribe the speech into text.")
|
| 42 |
+
|
| 43 |
+
video_input = gr.File(label="Upload Video File")
|
| 44 |
+
transcribed_text = gr.Textbox(label="Transcription", lines=10)
|
| 45 |
+
download_button = gr.File(label="Download Transcription")
|
| 46 |
+
|
| 47 |
+
btn = gr.Button("Transcribe")
|
| 48 |
+
|
| 49 |
+
btn.click(fn=process_video, inputs=video_input, outputs=[transcribed_text, download_button])
|
| 50 |
+
|
| 51 |
+
# Launch the Gradio App
|
| 52 |
+
app.launch()
|