|
import gradio as gr |
|
import whisper |
|
import os |
|
from pytube import YouTube |
|
from moviepy.editor import VideoFileClip |
|
|
|
model = whisper.load_model("base") |
|
|
|
def download_youtube_audio(url): |
|
yt = YouTube(url) |
|
stream = yt.streams.filter(only_audio=True).first() |
|
audio_file = stream.download(filename="yt_audio.mp4") |
|
return audio_file |
|
|
|
def transcribe_from_file(file): |
|
filename, ext = os.path.splitext(file) |
|
if ext == ".mp4": |
|
video = VideoFileClip(file) |
|
audio_path = filename + ".mp3" |
|
video.audio.write_audiofile(audio_path) |
|
result = model.transcribe(audio_path) |
|
os.remove(audio_path) |
|
else: |
|
result = model.transcribe(file) |
|
return result["text"] |
|
|
|
def transcribe_combined(file, url): |
|
if url: |
|
try: |
|
downloaded = download_youtube_audio(url) |
|
return transcribe_from_file(downloaded) |
|
except Exception as e: |
|
return f"β μ νλΈ λ€μ΄λ‘λ μ€ν¨: {str(e)}" |
|
elif file: |
|
return transcribe_from_file(file) |
|
else: |
|
return "β οΈ νμΌμ΄λ URL μ€ νλλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ." |
|
|
|
demo = gr.Interface( |
|
fn=transcribe_combined, |
|
inputs=[ |
|
gr.File(label="π¬ νμΌ μ
λ‘λ (μ ν)", file_types=[".mp3", ".wav", ".mp4"]), |
|
gr.Textbox(label="π YouTube λ§ν¬ (μ ν)", placeholder="https://youtu.be/...") |
|
], |
|
outputs="text", |
|
title="π§ Whisper μ μ¬κΈ° + YouTube μ§μ", |
|
description="mp4/mp3/wav νμΌμ μ
λ‘λνκ±°λ YouTube μμ μ£Όμλ₯Ό λΆμ¬λ£μ΄ ν
μ€νΈλ‘ μ μ¬νμΈμ!", |
|
allow_flagging="never" |
|
) |
|
|
|
demo.launch() |
|
|