File size: 1,622 Bytes
ed33df7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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()
|