Spaces:
Sleeping
Sleeping
import gradio as gr | |
import os | |
import subprocess | |
from transformers import pipeline | |
from langdetect import detect | |
def download_audio(youtube_url): | |
output_file = "audio.mp3" | |
# Remove old file if exists | |
if os.path.exists(output_file): | |
os.remove(output_file) | |
cmd = [ | |
"yt-dlp", "-x", "--audio-format", "mp3", "-o", output_file, youtube_url | |
] | |
subprocess.run(cmd, check=True) | |
return output_file | |
def process_youtube(youtube_url): | |
try: | |
audio_path = download_audio(youtube_url) | |
except Exception as e: | |
return "Error downloading audio: " + str(e), "", "", "" | |
try: | |
asr = pipeline("automatic-speech-recognition", model="openai/whisper-large") | |
result = asr(audio_path) | |
transcript = result["text"] | |
except Exception as e: | |
return "Error in transcription: " + str(e), "", "", "" | |
try: | |
detected_lang = detect(transcript) | |
except Exception: | |
detected_lang = "unknown" | |
lang_map = {'en': 'English', 'hi': 'Hindi', 'ta': 'Tamil'} | |
lang_text = lang_map.get(detected_lang, detected_lang) | |
transcript_en = transcript | |
if detected_lang != "en": | |
try: | |
asr_translate = pipeline( | |
"automatic-speech-recognition", | |
model="openai/whisper-large", | |
task="translate" | |
) | |
result_translate = asr_translate(audio_path) | |
transcript_en = result_translate["text"] | |
except Exception as e: | |
transcript_en = f"Error translating: {e}" | |
try: | |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
summary = summarizer(transcript_en, max_length=100, min_length=30, do_sample=False) | |
summary_text = summary[0]["summary_text"] | |
except Exception as e: | |
summary_text = f"Error summarizing: {e}" | |
if os.path.exists(audio_path): | |
os.remove(audio_path) | |
return lang_text, transcript, transcript_en, summary_text | |
with gr.Blocks() as demo: | |
gr.Markdown("## YouTube Transcript, Translation & Summary (Powered by Whisper + Hugging Face)") | |
url_input = gr.Textbox(label="YouTube URL") | |
btn = gr.Button("Process") | |
lang_out = gr.Textbox(label="Detected Language") | |
transcript_out = gr.Textbox(label="Original Transcript") | |
transcript_en_out = gr.Textbox(label="English Transcript (if translated)") | |
summary_out = gr.Textbox(label="Summary") | |
btn.click( | |
process_youtube, | |
inputs=[url_input], | |
outputs=[lang_out, transcript_out, transcript_en_out, summary_out] | |
) | |
demo.launch() | |