whisper / app.py
englissi's picture
Create app.py
ed33df7 verified
raw
history blame
1.62 kB
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()