Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
+
import os
|
| 4 |
+
from pytube import YouTube
|
| 5 |
+
from moviepy.editor import VideoFileClip
|
| 6 |
+
|
| 7 |
+
model = whisper.load_model("base")
|
| 8 |
+
|
| 9 |
+
def download_youtube_audio(url):
|
| 10 |
+
yt = YouTube(url)
|
| 11 |
+
stream = yt.streams.filter(only_audio=True).first()
|
| 12 |
+
audio_file = stream.download(filename="yt_audio.mp4")
|
| 13 |
+
return audio_file
|
| 14 |
+
|
| 15 |
+
def transcribe_from_file(file):
|
| 16 |
+
filename, ext = os.path.splitext(file)
|
| 17 |
+
if ext == ".mp4":
|
| 18 |
+
video = VideoFileClip(file)
|
| 19 |
+
audio_path = filename + ".mp3"
|
| 20 |
+
video.audio.write_audiofile(audio_path)
|
| 21 |
+
result = model.transcribe(audio_path)
|
| 22 |
+
os.remove(audio_path)
|
| 23 |
+
else:
|
| 24 |
+
result = model.transcribe(file)
|
| 25 |
+
return result["text"]
|
| 26 |
+
|
| 27 |
+
def transcribe_combined(file, url):
|
| 28 |
+
if url:
|
| 29 |
+
try:
|
| 30 |
+
downloaded = download_youtube_audio(url)
|
| 31 |
+
return transcribe_from_file(downloaded)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"β μ νλΈ λ€μ΄λ‘λ μ€ν¨: {str(e)}"
|
| 34 |
+
elif file:
|
| 35 |
+
return transcribe_from_file(file)
|
| 36 |
+
else:
|
| 37 |
+
return "β οΈ νμΌμ΄λ URL μ€ νλλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ."
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
fn=transcribe_combined,
|
| 41 |
+
inputs=[
|
| 42 |
+
gr.File(label="π¬ νμΌ μ
λ‘λ (μ ν)", file_types=[".mp3", ".wav", ".mp4"]),
|
| 43 |
+
gr.Textbox(label="π YouTube λ§ν¬ (μ ν)", placeholder="https://youtu.be/...")
|
| 44 |
+
],
|
| 45 |
+
outputs="text",
|
| 46 |
+
title="π§ Whisper μ μ¬κΈ° + YouTube μ§μ",
|
| 47 |
+
description="mp4/mp3/wav νμΌμ μ
λ‘λνκ±°λ YouTube μμ μ£Όμλ₯Ό λΆμ¬λ£μ΄ ν
μ€νΈλ‘ μ μ¬νμΈμ!",
|
| 48 |
+
allow_flagging="never"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
demo.launch()
|