Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import yt_dlp
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# Load your HF token safely (set this in your Hugging Face Space secrets as HF_TOKEN)
|
| 7 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "your_token_here")
|
| 8 |
+
|
| 9 |
+
# Choose a small Burmese-capable ASR model
|
| 10 |
+
MODEL_NAME = "chuuhtetnaing/whisper-tiny-myanmar"
|
| 11 |
+
|
| 12 |
+
# Initialize pipeline
|
| 13 |
+
asr_pipeline = pipeline(
|
| 14 |
+
task="automatic-speech-recognition",
|
| 15 |
+
model=MODEL_NAME,
|
| 16 |
+
use_auth_token=HF_TOKEN
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def download_audio(youtube_url, out_path="audio.wav"):
|
| 20 |
+
"""Download and convert YouTube video to audio wav"""
|
| 21 |
+
ydl_opts = {
|
| 22 |
+
"format": "bestaudio/best",
|
| 23 |
+
"outtmpl": "audio.%(ext)s",
|
| 24 |
+
"postprocessors": [{
|
| 25 |
+
"key": "FFmpegExtractAudio",
|
| 26 |
+
"preferredcodec": "wav",
|
| 27 |
+
"preferredquality": "192",
|
| 28 |
+
}],
|
| 29 |
+
}
|
| 30 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 31 |
+
ydl.download([youtube_url])
|
| 32 |
+
|
| 33 |
+
# yt-dlp usually saves as audio.wav
|
| 34 |
+
return "audio.wav"
|
| 35 |
+
|
| 36 |
+
def transcribe(youtube_url):
|
| 37 |
+
"""Main function: download + transcribe"""
|
| 38 |
+
try:
|
| 39 |
+
audio_file = download_audio(youtube_url)
|
| 40 |
+
result = asr_pipeline(audio_file, chunk_length_s=30) # handles long audio in 30s chunks
|
| 41 |
+
text = result["text"]
|
| 42 |
+
return text
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"❌ Error: {str(e)}"
|
| 45 |
+
|
| 46 |
+
# Gradio interface
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=transcribe,
|
| 49 |
+
inputs=gr.Textbox(label="YouTube URL", placeholder="Paste your YouTube link here..."),
|
| 50 |
+
outputs=gr.Textbox(label="Transcribed Text (Burmese)"),
|
| 51 |
+
title="🎙️ Burmese Speech-to-Text (YouTube)",
|
| 52 |
+
description="Paste a YouTube link with Burmese audio (up to ~20 minutes). It will transcribe the speech into text."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
demo.launch()
|