Spaces:
Sleeping
Sleeping
Update app.py
Browse files- app.py +90 -4
- requirements.txt +3 -0
app.py
CHANGED
@@ -1,7 +1,93 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import whisper
|
3 |
+
from pytube import YouTube
|
4 |
+
from typing import List
|
5 |
+
from transformers import pipeline
|
6 |
|
7 |
+
def transcribe(
|
8 |
+
url: str,
|
9 |
+
model_size: str
|
10 |
+
) -> str:
|
11 |
+
# Get audio from the video.
|
12 |
+
yt_client = YouTube(url=url)
|
13 |
+
audio_file = yt_client.streams.filter(only_audio=True)[0].download(filename="file.mp4")
|
14 |
|
15 |
+
# Load the model
|
16 |
+
model = whisper.load_model(model_size)
|
17 |
+
|
18 |
+
# Load the audio into the model
|
19 |
+
audio = whisper.load_audio(audio_file)
|
20 |
+
|
21 |
+
# Get results
|
22 |
+
result = model.transcribe(audio)
|
23 |
+
return format_result(result), summarize(result["text"])
|
24 |
+
|
25 |
+
|
26 |
+
def summarize(text: str) -> str:
|
27 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
28 |
+
out = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]["summary_text"]
|
29 |
+
return out
|
30 |
+
|
31 |
+
|
32 |
+
def format_result(result: whisper.DecodingResult) -> str:
|
33 |
+
out = []
|
34 |
+
for item in result["segments"]:
|
35 |
+
out.append(f"from {item['start']:6.2f} to {item['end']:6.2f} {item['text']}")
|
36 |
+
return "\n".join(out)
|
37 |
+
|
38 |
+
|
39 |
+
def get_model_sizes() -> List[str]:
|
40 |
+
"""
|
41 |
+
:rtype: list
|
42 |
+
:return: List of possible sizes of the Whisper model.
|
43 |
+
"""
|
44 |
+
return list(
|
45 |
+
whisper._MODELS.keys()
|
46 |
+
)
|
47 |
+
|
48 |
+
|
49 |
+
title = "YouTube transcribe + summarization"
|
50 |
+
desc = "Transcribe YouTube videos using OpenAI Whisper."
|
51 |
+
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.HTML(title)
|
54 |
+
with gr.Row():
|
55 |
+
with gr.Column():
|
56 |
+
gr.Markdown(
|
57 |
+
f"""
|
58 |
+
{desc}
|
59 |
+
"""
|
60 |
+
)
|
61 |
+
with gr.Row():
|
62 |
+
model_size = gr.Dropdown(
|
63 |
+
label="Model size",
|
64 |
+
choices=get_model_sizes(),
|
65 |
+
value="tiny"
|
66 |
+
)
|
67 |
+
url = gr.Textbox(label="YouTube URL")
|
68 |
+
with gr.Row():
|
69 |
+
text = gr.Textbox(
|
70 |
+
label="Transcription",
|
71 |
+
lines=10
|
72 |
+
)
|
73 |
+
with gr.Row():
|
74 |
+
summarization = gr.Textbox(
|
75 |
+
label="Summarization",
|
76 |
+
lines=5
|
77 |
+
)
|
78 |
+
with gr.Row().style(equal_height=True):
|
79 |
+
submit_button = gr.Button("Submit")
|
80 |
+
|
81 |
+
submit_button.click(
|
82 |
+
transcribe,
|
83 |
+
inputs=[
|
84 |
+
url,
|
85 |
+
model_size
|
86 |
+
],
|
87 |
+
outputs=[
|
88 |
+
text,
|
89 |
+
summarization
|
90 |
+
]
|
91 |
+
)
|
92 |
+
|
93 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pytube
|
2 |
+
openai-whisper
|
3 |
+
transformers
|