Spaces:
Runtime error
Runtime error
Added app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pytube import YouTube
|
3 |
+
from transformers import pipeline
|
4 |
+
import whisper
|
5 |
+
|
6 |
+
class GradioInference():
|
7 |
+
def __init__(self):
|
8 |
+
self.model = pipeline(model="humeur/lab2_id2223")
|
9 |
+
self.yt = None
|
10 |
+
|
11 |
+
def __call__(self, link, subs):
|
12 |
+
if self.yt is None:
|
13 |
+
self.yt = YouTube(link)
|
14 |
+
path = self.yt.streams.filter(only_audio=True)[0].download(filename="tmp.mp4")
|
15 |
+
|
16 |
+
results = self.loaded_model(path)
|
17 |
+
|
18 |
+
if subs == "None":
|
19 |
+
return results["text"]
|
20 |
+
elif subs == ".srt":
|
21 |
+
return self.srt(results["segments"])
|
22 |
+
elif ".csv" == ".csv":
|
23 |
+
return self.csv(results["segments"])
|
24 |
+
|
25 |
+
def srt(self, segments):
|
26 |
+
output = ""
|
27 |
+
for i, segment in enumerate(segments):
|
28 |
+
output += f"{i+1}\n"
|
29 |
+
output += f"{self.format_time(segment['start'])} --> {self.format_time(segment['end'])}\n"
|
30 |
+
output += f"{segment['text']}\n\n"
|
31 |
+
return output
|
32 |
+
|
33 |
+
def csv(self, segments):
|
34 |
+
output = ""
|
35 |
+
for segment in segments:
|
36 |
+
output += f"{segment['start']},{segment['end']},{segment['text']}\n"
|
37 |
+
return output
|
38 |
+
|
39 |
+
def format_time(self, time):
|
40 |
+
hours = time//3600
|
41 |
+
minutes = (time - hours*3600)//60
|
42 |
+
seconds = time - hours*3600 - minutes*60
|
43 |
+
milliseconds = (time - int(time))*1000
|
44 |
+
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d},{int(milliseconds):03d}"
|
45 |
+
|
46 |
+
def populate_metadata(self, link):
|
47 |
+
self.yt = YouTube(link)
|
48 |
+
return self.yt.thumbnail_url, self.yt.title
|
49 |
+
|
50 |
+
gio = GradioInference()
|
51 |
+
title="SWED->EN Youtube Transcriber (Whisper)"
|
52 |
+
description="Speech to text transcription of Youtube videos using OpenAI's Whisper finetunned for Swedish to English translation"
|
53 |
+
|
54 |
+
block = gr.Blocks()
|
55 |
+
with block:
|
56 |
+
gr.HTML(
|
57 |
+
f"""
|
58 |
+
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
59 |
+
<div>
|
60 |
+
<h1>{title}</h1>
|
61 |
+
</div>
|
62 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
63 |
+
{description}
|
64 |
+
</p>
|
65 |
+
</div>
|
66 |
+
"""
|
67 |
+
)
|
68 |
+
with gr.Group():
|
69 |
+
with gr.Box():
|
70 |
+
with gr.Row().style(equal_height=True):
|
71 |
+
wt = gr.Radio(["No", ".srt", ".csv"], label="Result with timestamps?")
|
72 |
+
link = gr.Textbox(label="YouTube Link")
|
73 |
+
title = gr.Label(label="Video Title")
|
74 |
+
with gr.Row().style(equal_height=True):
|
75 |
+
img = gr.Image(label="Thumbnail")
|
76 |
+
text = gr.Textbox(label="Transcription", placeholder="Transcription Output", lines=10)
|
77 |
+
with gr.Row().style(equal_height=True):
|
78 |
+
btn = gr.Button("Transcribe")
|
79 |
+
btn.click(gio, inputs=[link, wt], outputs=[text])
|
80 |
+
link.change(gio.populate_metadata, inputs=[link], outputs=[img, title])
|
81 |
+
block.launch()
|