Spaces:
Sleeping
Sleeping
Sindhura83
commited on
Commit
•
0ea17e2
1
Parent(s):
7dfdb75
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import pytube as pt
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
MODEL_NAME = "openai/whisper-large-v2"
|
8 |
+
BATCH_SIZE = 8
|
9 |
+
|
10 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
11 |
+
|
12 |
+
pipe = pipeline(
|
13 |
+
task="automatic-speech-recognition",
|
14 |
+
model=MODEL_NAME,
|
15 |
+
chunk_length_s=30,
|
16 |
+
device=device,
|
17 |
+
)
|
18 |
+
|
19 |
+
|
20 |
+
all_special_ids = pipe.tokenizer.all_special_ids
|
21 |
+
transcribe_token_id = all_special_ids[-5]
|
22 |
+
translate_token_id = all_special_ids[-6]
|
23 |
+
|
24 |
+
|
25 |
+
def transcribe(microphone, file_upload, task):
|
26 |
+
warn_output = ""
|
27 |
+
if (microphone is not None) and (file_upload is not None):
|
28 |
+
warn_output = (
|
29 |
+
"WARNING: You've uploaded an audio file and used the microphone. "
|
30 |
+
"The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
|
31 |
+
)
|
32 |
+
|
33 |
+
elif (microphone is None) and (file_upload is None):
|
34 |
+
return "ERROR: You have to either use the microphone or upload an audio file"
|
35 |
+
|
36 |
+
file = microphone if microphone is not None else file_upload
|
37 |
+
|
38 |
+
pipe.model.config.forced_decoder_ids = [[2, transcribe_token_id if task=="transcribe" else translate_token_id]]
|
39 |
+
|
40 |
+
textt = pipe(file, batch_size=BATCH_SIZE)["text"]
|
41 |
+
|
42 |
+
with open('outt.txt', 'a+') as sw:
|
43 |
+
sw.writelines(textt)
|
44 |
+
|
45 |
+
return [textt,"outt.txt"]
|
46 |
+
|
47 |
+
|
48 |
+
def _return_yt_html_embed(yt_url):
|
49 |
+
video_id = yt_url.split("?v=")[-1]
|
50 |
+
HTML_str = (
|
51 |
+
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
52 |
+
" </center>"
|
53 |
+
)
|
54 |
+
return HTML_str
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
def yt_transcribe(yt_url, task):
|
59 |
+
yt = pt.YouTube(yt_url)
|
60 |
+
html_embed_str = _return_yt_html_embed(yt_url)
|
61 |
+
stream = yt.streams.filter(only_audio=True)[0]
|
62 |
+
stream.download(filename="audio.mp3")
|
63 |
+
|
64 |
+
pipe.model.config.forced_decoder_ids = [[2, transcribe_token_id if task=="transcribe" else translate_token_id]]
|
65 |
+
|
66 |
+
text = pipe("audio.mp3", batch_size=BATCH_SIZE)["text"]
|
67 |
+
|
68 |
+
return html_embed_str, text
|
69 |
+
|
70 |
+
demo = gr.Blocks()
|
71 |
+
output_2 = gr.File(label="Download")
|
72 |
+
description = """This application displays transcribed text for given audio input <img src="https://i.ibb.co/J5DscKw/GVP-Womens.jpg" width=100px>"""
|
73 |
+
mf_transcribe = gr.Interface(
|
74 |
+
fn=transcribe,
|
75 |
+
inputs=[
|
76 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
77 |
+
gr.inputs.Audio(source="upload", type="filepath", optional=True),
|
78 |
+
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
|
79 |
+
],
|
80 |
+
outputs=["text",output_2],
|
81 |
+
layout="horizontal",
|
82 |
+
theme="huggingface",
|
83 |
+
title="Speech to Text Converter using OpenAI Whisper Model",
|
84 |
+
description= description,
|
85 |
+
allow_flagging="never",
|
86 |
+
)
|
87 |
+
|
88 |
+
yt_transcribe = gr.Interface(
|
89 |
+
fn=yt_transcribe,
|
90 |
+
inputs=[
|
91 |
+
gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
92 |
+
gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
|
93 |
+
],
|
94 |
+
outputs=["html", "text"],
|
95 |
+
layout="horizontal",
|
96 |
+
theme="huggingface",
|
97 |
+
title="Speech to Text Converter using OpenAI Whisper Model",
|
98 |
+
description=(
|
99 |
+
"Transcribe YouTube Videos to Text"
|
100 |
+
),
|
101 |
+
allow_flagging="never",
|
102 |
+
)
|
103 |
+
|
104 |
+
with demo:
|
105 |
+
gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
|
106 |
+
|
107 |
+
demo.launch(enable_queue=True)
|