Spaces:
Running on Zero
Running on Zero
Update app.py
#148
by
amirado - opened
app.py
CHANGED
|
@@ -1,108 +1,14 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
|
| 16 |
-
|
| 17 |
-
device = 0 if torch.cuda.is_available() else "cpu"
|
| 18 |
-
|
| 19 |
-
pipe = pipeline(
|
| 20 |
-
task="automatic-speech-recognition",
|
| 21 |
-
model=MODEL_NAME,
|
| 22 |
-
chunk_length_s=30,
|
| 23 |
-
device=device,
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
@spaces.GPU
|
| 27 |
-
def transcribe(inputs, task):
|
| 28 |
-
if inputs is None:
|
| 29 |
-
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
| 30 |
-
|
| 31 |
-
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 32 |
-
return text
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def _return_yt_html_embed(yt_url):
|
| 36 |
-
video_id = yt_url.split("?v=")[-1]
|
| 37 |
-
HTML_str = (
|
| 38 |
-
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
| 39 |
-
" </center>"
|
| 40 |
-
)
|
| 41 |
-
return HTML_str
|
| 42 |
-
|
| 43 |
-
def download_yt_audio(yt_url, filename):
|
| 44 |
-
info_loader = youtube_dl.YoutubeDL()
|
| 45 |
-
|
| 46 |
-
try:
|
| 47 |
-
info = info_loader.extract_info(yt_url, download=False)
|
| 48 |
-
except youtube_dl.utils.DownloadError as err:
|
| 49 |
-
raise gr.Error(str(err))
|
| 50 |
-
|
| 51 |
-
file_length = info["duration_string"]
|
| 52 |
-
file_h_m_s = file_length.split(":")
|
| 53 |
-
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
| 54 |
-
|
| 55 |
-
if len(file_h_m_s) == 1:
|
| 56 |
-
file_h_m_s.insert(0, 0)
|
| 57 |
-
if len(file_h_m_s) == 2:
|
| 58 |
-
file_h_m_s.insert(0, 0)
|
| 59 |
-
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
| 60 |
-
|
| 61 |
-
if file_length_s > YT_LENGTH_LIMIT_S:
|
| 62 |
-
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
| 63 |
-
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
| 64 |
-
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
| 65 |
-
|
| 66 |
-
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
| 67 |
-
|
| 68 |
-
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
| 69 |
-
try:
|
| 70 |
-
ydl.download([yt_url])
|
| 71 |
-
except youtube_dl.utils.ExtractorError as err:
|
| 72 |
-
raise gr.Error(str(err))
|
| 73 |
-
|
| 74 |
-
@spaces.GPU
|
| 75 |
-
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
| 76 |
-
html_embed_str = _return_yt_html_embed(yt_url)
|
| 77 |
-
|
| 78 |
-
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 79 |
-
filepath = os.path.join(tmpdirname, "video.mp4")
|
| 80 |
-
download_yt_audio(yt_url, filepath)
|
| 81 |
-
with open(filepath, "rb") as f:
|
| 82 |
-
inputs = f.read()
|
| 83 |
-
|
| 84 |
-
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
| 85 |
-
inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
| 86 |
-
|
| 87 |
-
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 88 |
-
|
| 89 |
-
return html_embed_str, text
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
demo = gr.Interface(
|
| 93 |
-
fn=transcribe,
|
| 94 |
-
inputs=[
|
| 95 |
-
gr.Audio(type="filepath"),
|
| 96 |
-
gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
|
| 97 |
-
],
|
| 98 |
-
outputs=gr.Textbox(lines=3),
|
| 99 |
-
title="Whisper Large V3: Transcribe Audio",
|
| 100 |
-
description=(
|
| 101 |
-
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
|
| 102 |
-
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
| 103 |
-
" of arbitrary length."
|
| 104 |
-
),
|
| 105 |
-
allow_flagging="never",
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
demo.launch()
|
|
|
|
| 1 |
+
import smtplib
|
| 2 |
+
from email.mime.text import MIMEText
|
| 3 |
+
|
| 4 |
+
def send_email(text, recipient_email):
|
| 5 |
+
sender_email = "your_email@gmail.com"
|
| 6 |
+
password = "your_app_password" # safe password for apps
|
| 7 |
+
msg = MIMEText(text)
|
| 8 |
+
msg["Subject"] = "Your Audio Transcription"
|
| 9 |
+
msg["From"] = sender_email
|
| 10 |
+
msg["To"] = recipient_email
|
| 11 |
+
|
| 12 |
+
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
| 13 |
+
server.login(sender_email, password)
|
| 14 |
+
server.sendmail(sender_email, recipient_email, msg.as_string())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|