Spaces:
Runtime error
Runtime error
| import tempfile | |
| import gradio as gr | |
| import os | |
| import tensorflow_hub as hub | |
| import tensorflow as tf | |
| import tensorflow_text as text | |
| import sys | |
| import numpy as np | |
| import faiss | |
| import csv | |
| # NO GPU | |
| os.environ['CUDA_VISIBLE_DEVICES'] = '-1' | |
| os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' | |
| def make_bert_preprocess_model(sentence_features, tfhub_handle_preprocess, seq_length=128): | |
| """Returns Model mapping string features to BERT inputs. | |
| """ | |
| input_segments = [ | |
| tf.keras.layers.Input(shape=(), dtype=tf.string, name=ft) | |
| for ft in sentence_features] | |
| bert_preprocess = hub.load(tfhub_handle_preprocess) | |
| tokenizer = hub.KerasLayer(bert_preprocess.tokenize, name='tokenizer') | |
| segments = [tokenizer(s) for s in input_segments] | |
| truncated_segments = segments | |
| packer = hub.KerasLayer(bert_preprocess.bert_pack_inputs, | |
| arguments=dict(seq_length=seq_length), | |
| name='packer') | |
| model_inputs = packer(truncated_segments) | |
| return tf.keras.Model(input_segments, model_inputs) | |
| from models import * | |
| def process(prompt, lang): | |
| # Getting prompt user | |
| #prompt = input("Audio Search - enter text : ") | |
| #print(prompt) | |
| # prompt embedding | |
| bert_model_name = 'small_bert/bert_en_uncased_L-4_H-512_A-8' | |
| tfhub_handle_encoder = 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-512_A-8/1' | |
| tfhub_handle_preprocess = 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3' | |
| MAX_LENGTH = 130 # MAX de 512 !!! TENSORFLOW !!! | |
| TOP = 10 | |
| bert_preprocess_model = make_bert_preprocess_model(['my_input'], tfhub_handle_preprocess, seq_length = MAX_LENGTH) | |
| bert_model = hub.KerasLayer(tfhub_handle_encoder) | |
| print("Text input : ", prompt) | |
| prompt=[prompt] | |
| text_preprocessed = bert_preprocess_model([np.array(prompt)]) | |
| embed_prompt = bert_model(text_preprocessed) | |
| print(" text representation computed.") | |
| # Embed text | |
| #from models import * | |
| encoder_text = tf.keras.models.load_model('encoder_text_retrievaltext_bmg_221022_54.h5') | |
| embed_query = encoder_text.predict(embed_prompt["pooled_output"]) | |
| faiss.normalize_L2(embed_query) | |
| print(" text embed computed.") | |
| # load embed audio catalog | |
| index = faiss.read_index("BMG_221022.index") | |
| # distance computing | |
| D, I = index.search(embed_query, TOP) | |
| # names index | |
| import joblib | |
| audio_names = joblib.load(open('BMG_221022_names.index', 'rb')) | |
| #url | |
| url_dict={} | |
| with open("bmg_clean.csv") as csv_file: | |
| csv_reader = csv.reader(csv_file, delimiter=';') | |
| for row in csv_reader: | |
| f = row[2].split('/')[-1] | |
| url_dict[f.split('/')[-1][:-4]] = row[2] | |
| # output : top N audio file names | |
| print(I) | |
| print(D) | |
| print("----") | |
| for i in range(len(I[0])): | |
| print(audio_names[I[0][i]], " with distance ", D[0][i]) | |
| print(" url : ", url_dict[audio_names[I[0][i]]]) | |
| return [url_dict[audio_names[I[0][0]]], url_dict[audio_names[I[0][1]]], url_dict[audio_names[I[0][2]]], url_dict[audio_names[I[0][3]]], url_dict[audio_names[I[0][4]]]] | |
| inputs = [gr.Textbox(label="Input", value="type your description", max_lines=2), | |
| gr.Radio(label="Language", choices=["en"], value="en")] | |
| poc_examples = [#[["I love learning machine learning"],["autre"]] | |
| ["Mysterious filmscore with Arabic influenced instruments"], | |
| ["Let's go on a magical adventure with wizzards, dragons and castles"], | |
| ["Creepy piano opening evolves and speeds up into a cinematic orchestral piece"], | |
| ["Halloween rock with creepy organ"], | |
| ["Rhythmic electro dance track for sport, motivation and sweating"], | |
| ["soundtrack for an action movie from the eighties in a retro synth wave style"], | |
| ["Choral female singing is rhythmically accompanied in a church with medieval instruments"], | |
| ["Christmas"], | |
| ["love romantic with piano, strings and vocals"], | |
| ["Electronic soundscapes for chilling and relaxing"], | |
| ["Minimal, emotional, melancholic piano"], | |
| ["# Minimal, happy, joyful piano"], | |
| ["A calm and romantic acoustic guitar melody"], | |
| ["horror suspense piano"], | |
| ["Big Band"], | |
| ["90 eurodance beat"], | |
| ] | |
| ''' | |
| outputs = [gr.Textbox(label="url 1"),gr.Textbox(label="url 2"),gr.Textbox(label="url 3"),gr.Textbox(label="url 4"),gr.Textbox(label="url 5")] | |
| demo2 = gr.Interface(fn=process, inputs=inputs, outputs=outputs) | |
| demo2.launch(debug=True) | |
| ''' | |
| outputs = [gr.Audio(label="Track 1"), gr.Audio(label="Track 2"), gr.Audio(label="Track 3"), gr.Audio(label="Track 4"), gr.Audio(label="Track 5")] | |
| demo1 = gr.Interface(fn=process, inputs=inputs, outputs=outputs, examples=poc_examples) | |
| demo1.launch(debug=True)#, share=True) | |
| def id(inp): | |
| return inp | |
| #outputs = [gr.Audio(label="Proposal 1"), gr.Audio(label="Proposal 2"), gr.Audio(label="Proposal 3"), gr.Audio(label="Proposal 4"), gr.Audio(label="Proposal 5")] | |
| #demo1 = gr.Interface(fn=id, inputs=outputs, outputs=outputs) | |
| #outputs = [gr.Audio(label="Proposal 1"), gr.Textbox(label="url 1"),gr.Audio(label="Proposal 2"), gr.Audio(label="Proposal 3"), gr.Audio(label="Proposal 4"), gr.Audio(label="Proposal 5")] | |
| #all = gr.Series(demo2, demo1) | |
| #all.launch(debug=True) | |