Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,66 @@
|
|
| 1 |
import os
|
| 2 |
import openai
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
openai.api_key = os.getenv('OPEN_AI_KEY')
|
| 6 |
hf_t_key = ('HF_TOKEN_KEY')
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def predict(message, history):
|
| 9 |
history_openai_format = []
|
| 10 |
for human, assistant in history:
|
|
@@ -57,5 +113,35 @@ A4 = gr.load(
|
|
| 57 |
allow_flagging="never",
|
| 58 |
examples=["A gigantic celtic leprechaun wandering the streets of downtown Atlanta","A child eating pizza in a Brazilian favela"])
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
+
import pytube as pt
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from huggingface_hub import model_info
|
| 9 |
|
| 10 |
openai.api_key = os.getenv('OPEN_AI_KEY')
|
| 11 |
hf_t_key = ('HF_TOKEN_KEY')
|
| 12 |
|
| 13 |
+
MODEL_NAME = "openai/whisper-small"
|
| 14 |
+
lang = "en
|
| 15 |
+
|
| 16 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
| 17 |
+
pipe = pipeline(
|
| 18 |
+
task="automatic-speech-recognition",
|
| 19 |
+
model=MODEL_NAME,
|
| 20 |
+
chunk_length_s=30,
|
| 21 |
+
device=device,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang, task="transcribe")
|
| 25 |
+
|
| 26 |
+
def transcribe(microphone, file_upload):
|
| 27 |
+
warn_output = ""
|
| 28 |
+
if (microphone is not None) and (file_upload is not None):
|
| 29 |
+
warn_output = (
|
| 30 |
+
"WARNING: You've uploaded an audio file and or recorded . "
|
| 31 |
+
"The recorded file from the microphone uploaded, transcribed and immediately discarded.\n"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
elif (microphone is None) and (file_upload is None):
|
| 35 |
+
return "ERROR: You have to either use the microphone or upload an audio file"
|
| 36 |
+
|
| 37 |
+
file = microphone if microphone is not None else file_upload
|
| 38 |
+
|
| 39 |
+
text = pipe(file)["text"]
|
| 40 |
+
|
| 41 |
+
return warn_output + text
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _return_yt_html_embed(yt_url):
|
| 45 |
+
video_id = yt_url.split("?v=")[-1]
|
| 46 |
+
HTML_str = (
|
| 47 |
+
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
| 48 |
+
" </center>"
|
| 49 |
+
)
|
| 50 |
+
return HTML_str
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def yt_transcribe(yt_url):
|
| 54 |
+
yt = pt.YouTube(yt_url)
|
| 55 |
+
html_embed_str = _return_yt_html_embed(yt_url)
|
| 56 |
+
stream = yt.streams.filter(only_audio=True)[0]
|
| 57 |
+
stream.download(filename="audio.mp3")
|
| 58 |
+
|
| 59 |
+
text = pipe("audio.mp3")["text"]
|
| 60 |
+
|
| 61 |
+
return html_embed_str, text
|
| 62 |
+
|
| 63 |
+
|
| 64 |
def predict(message, history):
|
| 65 |
history_openai_format = []
|
| 66 |
for human, assistant in history:
|
|
|
|
| 113 |
allow_flagging="never",
|
| 114 |
examples=["A gigantic celtic leprechaun wandering the streets of downtown Atlanta","A child eating pizza in a Brazilian favela"])
|
| 115 |
|
| 116 |
+
mf_transcribe = gr.Interface(
|
| 117 |
+
fn=transcribe,
|
| 118 |
+
inputs=[
|
| 119 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
| 120 |
+
gr.inputs.Audio(source="upload", type="filepath", optional=True),
|
| 121 |
+
],
|
| 122 |
+
outputs="text",
|
| 123 |
+
layout="horizontal",
|
| 124 |
+
theme="huggingface",
|
| 125 |
+
title=" ",
|
| 126 |
+
description=(
|
| 127 |
+
"Transcribe recorded or audio files with the click of a button."
|
| 128 |
+
),
|
| 129 |
+
allow_flagging="never",
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
yt_transcribe = gr.Interface(
|
| 133 |
+
fn=yt_transcribe,
|
| 134 |
+
inputs=[gr.inputs.Textbox(lines=1, placeholder="Paste your YouTube video URL here", label="YouTube Video URL")],
|
| 135 |
+
outputs=["html", "text"],
|
| 136 |
+
layout="horizontal",
|
| 137 |
+
theme="huggingface",
|
| 138 |
+
title=" ",
|
| 139 |
+
description=(
|
| 140 |
+
"Transcribe YouTube videos at the click of a button."
|
| 141 |
+
|
| 142 |
+
),
|
| 143 |
+
allow_flagging="never",
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
clp = gr.TabbedInterface([A1, mf_transcribe, yt_transcribe, A2, A3], ["Chat", "Transcribe Audio", "Transcribe YouTube Videos", "Describe", "Create"], theme= gr.themes.Glass(primary_hue="neutral", neutral_hue="slate"))
|
| 147 |
+
clp.queue().launch()
|