Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from transformers import pipeline
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
import pytube as pt
|
6 |
+
|
7 |
+
checkpoint = "GGmorello/whisper-small-it"
|
8 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
9 |
+
print(device)
|
10 |
+
|
11 |
+
pipe = pipeline(task = "automatic-speech-recognition", model = checkpoint,chunk_length_s=30,device = device)
|
12 |
+
|
13 |
+
def transcribe(audio):
|
14 |
+
text = pipe(audio)["text"]
|
15 |
+
return text
|
16 |
+
|
17 |
+
def transcribe_url(yt_url):
|
18 |
+
yt = pt.YouTube(yt_url)
|
19 |
+
stream = yt.streams.filter(only_audio=True)[0]
|
20 |
+
stream.download(filename = "audio.mp3")
|
21 |
+
text = pipe("audio.mp3")["text"]
|
22 |
+
return text
|
23 |
+
|
24 |
+
|
25 |
+
demo = gr.Blocks()
|
26 |
+
|
27 |
+
microphone_interface = gr.Interface(
|
28 |
+
fn=transcribe,
|
29 |
+
inputs = gr.Audio(sources="microphone", type="filepath"),
|
30 |
+
outputs="text",
|
31 |
+
title="Whisper Small Italian Finetuned raw microphone audio",
|
32 |
+
description="Realtime demo for Italian speech recognition using a fine-tuned Whisper small model."
|
33 |
+
|
34 |
+
)
|
35 |
+
|
36 |
+
|
37 |
+
file_interface = gr.Interface(
|
38 |
+
fn=transcribe,
|
39 |
+
inputs = gr.Audio(sources="upload", type="filepath"),
|
40 |
+
outputs="text",
|
41 |
+
title="Whisper Small Italian Finetuned for audio file.",
|
42 |
+
description="Realtime demo for Italian speech recognition using a fine-tuned Whisper small model."
|
43 |
+
|
44 |
+
)
|
45 |
+
|
46 |
+
|
47 |
+
url_interface = gr.Interface(
|
48 |
+
fn = transcribe_url,
|
49 |
+
inputs = gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
50 |
+
outputs = "text",
|
51 |
+
title = "Whisper Small Italian Finetuned for URL transcription",
|
52 |
+
description = "Realtime demo for Italian speech recognition using a fine-tuned Whisper small model."
|
53 |
+
|
54 |
+
|
55 |
+
)
|
56 |
+
|
57 |
+
with demo:
|
58 |
+
gr.TabbedInterface([microphone_interface,file_interface, url_interface], ["Transcribe Audio", "Transcribe File" , "Transcribe YouTube"])
|
59 |
+
|
60 |
+
demo.launch(share=True)
|