AudioSimilarity / app.py
PierreHanna's picture
Update app.py
ebf2b7b
import time
import json
import os
import pickle
import gradio as gr
import numpy as np
import yt_dlp
import sys
from huggingface_hub import hf_hub_download
embed_html1 = '<iframe width="560" height="315" src="https://www.youtube.com/embed/'
embed_html2 = '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'
# NO GPU
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
#
python_path = hf_hub_download(repo_id=os.environ['REPO_ID'], repo_type="space", filename=os.environ['MODEL_FILE'],
use_auth_token=os.environ['TOKEN'])
sys.path.append(os.environ['PRIVATE_DIR'])
from models import *
def download_audio_(link):
with yt_dlp.YoutubeDL({'extract_audio': True, 'format': 'bestaudio', 'outtmpl': '%(title)s.mp3'}) as video:
info_dict = video.extract_info(link, download = True)
video_title = info_dict['title']
video.download(link)
return video_title
def download_audio(id_video):
id = id_video.split("?v=")[-1][:11]
audio_file = download_audio_(id_video)
audio_file = audio_file+'.mp3'
embed_html_all = embed_html1 + id +embed_html2
return audio_file, audio_file, embed_html_all
def process_url(input_path):
# setup the client
audio_file, audio_file, embed_html_all = download_audio(input_path)
return process(audio_file, embed_html_all)
def process_file(input_path):
return process(input_path, '')[1:]
def process(audio_file, embed_html_all):
emb, ts = get_embed(audio_file)
ind = get_index()
ind_filenames = get_audio_names()
catalog = get_catalog()
_, I = do_search(emb, ind)
top1, top2, top3, top4, top5 = get_top(I, ind_filenames, catalog)
return embed_html_all, top1, top2, top3, top4, top5
poc_examples = [["https://www.youtube.com/watch?v=aNzCDt2eidg"]]
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
with gr.Row():
#gr.HTML(embed_html)
html = gr.HTML()
with gr.Row():
with gr.Column():
audio_url_input = gr.Textbox(placeholder='YouTube video URL', label='YouTube video URL')
analyze_url_btn = gr.Button('Search from URL')
with gr.Row():
with gr.Column():
audio_input_file = gr.Audio(type="filepath", label='Audio Input')
analyze_file_btn = gr.Button('Search from file')
with gr.Row():
with gr.Column():
'''
gr.HTML("<h3>Top 1</h3>")
top1 = gr.Textbox(label="top1", show_label=False)
gr.HTML("<h3>Top 2</h3>")
top2 = gr.Textbox(label="top2", show_label=False)
gr.HTML("<h3>Top 3</h3>")
top3 = gr.Textbox(label="top3", show_label=False)
gr.HTML("<h3>Top 4</h3>")
top4 = gr.Textbox(label="top4", show_label=False)
gr.HTML("<h3>Top 5</h3>")
top5 = gr.Textbox(label="top5", show_label=False)
'''
top1 = gr.Audio(label="top1", show_label=True)
top2 = gr.Audio(label="top2", show_label=True)
top3 = gr.Audio(label="top3", show_label=True)
top4 = gr.Audio(label="top4", show_label=True)
top5 = gr.Audio(label="top5", show_label=True)
analyze_url_btn.click(process_url, inputs=[audio_url_input],
outputs=[html,top1, top2, top3, top4, top5])
gr.Examples(
examples = [
"https://www.youtube.com/watch?v=aNzCDt2eidg",
"https://www.youtube.com/watch?v=NBE-uBgtINg",
"https://www.youtube.com/watch?v=5NV6Rdv1a3I",
#"https://www.youtube.com/watch?v=OiC1rgCPmUQ",
#"https://www.youtube.com/watch?v=dRX0wDNK6S4",
"https://www.youtube.com/watch?v=Guzu9aAeDIc"],
inputs = [audio_url_input],
outputs = [html,top1, top2, top3, top4, top5],
fn = process_url,
cache_examples=False,
)
analyze_file_btn.click(process_file, inputs=[audio_input_file],
outputs=[top1, top2, top3, top4, top5])
demo.launch(debug=True)