Spaces:
Runtime error
Runtime error
Commit Β·
2735a87
1
Parent(s): ffd944b
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,139 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tempfile
|
| 2 |
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import tensorflow_hub as hub
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
import tensorflow_text as text
|
| 7 |
+
import sys
|
| 8 |
+
import numpy as np
|
| 9 |
+
import faiss
|
| 10 |
+
import csv
|
| 11 |
|
| 12 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
|
|
|
|
| 13 |
|
| 14 |
+
|
| 15 |
+
def make_bert_preprocess_model(sentence_features, tfhub_handle_preprocess, seq_length=128):
|
| 16 |
+
"""Returns Model mapping string features to BERT inputs.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
input_segments = [
|
| 20 |
+
tf.keras.layers.Input(shape=(), dtype=tf.string, name=ft)
|
| 21 |
+
for ft in sentence_features]
|
| 22 |
+
|
| 23 |
+
bert_preprocess = hub.load(tfhub_handle_preprocess)
|
| 24 |
+
tokenizer = hub.KerasLayer(bert_preprocess.tokenize, name='tokenizer')
|
| 25 |
+
segments = [tokenizer(s) for s in input_segments]
|
| 26 |
+
|
| 27 |
+
truncated_segments = segments
|
| 28 |
+
packer = hub.KerasLayer(bert_preprocess.bert_pack_inputs,
|
| 29 |
+
arguments=dict(seq_length=seq_length),
|
| 30 |
+
name='packer')
|
| 31 |
+
model_inputs = packer(truncated_segments)
|
| 32 |
+
return tf.keras.Model(input_segments, model_inputs)
|
| 33 |
+
|
| 34 |
+
from models import *
|
| 35 |
+
|
| 36 |
+
def process(prompt, lang):
|
| 37 |
+
|
| 38 |
+
# Getting prompt user
|
| 39 |
+
#prompt = input("Audio Search - enter text : ")
|
| 40 |
+
#print(prompt)
|
| 41 |
+
|
| 42 |
+
# prompt embedding
|
| 43 |
+
bert_model_name = 'small_bert/bert_en_uncased_L-4_H-512_A-8'
|
| 44 |
+
tfhub_handle_encoder = 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-512_A-8/1'
|
| 45 |
+
tfhub_handle_preprocess = 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3'
|
| 46 |
+
|
| 47 |
+
MAX_LENGTH = 130 # MAX de 512 !!! TENSORFLOW !!!
|
| 48 |
+
TOP = 10
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
bert_preprocess_model = make_bert_preprocess_model(['my_input'], tfhub_handle_preprocess, seq_length = MAX_LENGTH)
|
| 52 |
+
bert_model = hub.KerasLayer(tfhub_handle_encoder)
|
| 53 |
+
|
| 54 |
+
print("Text input : ", prompt)
|
| 55 |
+
prompt=[prompt]
|
| 56 |
+
text_preprocessed = bert_preprocess_model([np.array(prompt)])
|
| 57 |
+
embed_prompt = bert_model(text_preprocessed)
|
| 58 |
+
print(" text representation computed.")
|
| 59 |
+
|
| 60 |
+
# Embed text
|
| 61 |
+
#from models import *
|
| 62 |
+
encoder_text = tf.keras.models.load_model('encoder_text_retrievaltext_bmg_221022_54.h5')
|
| 63 |
+
embed_query = encoder_text.predict(embed_prompt["pooled_output"])
|
| 64 |
+
faiss.normalize_L2(embed_query)
|
| 65 |
+
print(" text embed computed.")
|
| 66 |
+
|
| 67 |
+
# load embed audio catalog
|
| 68 |
+
index = faiss.read_index("BMG_221022.index")
|
| 69 |
+
|
| 70 |
+
# distance computing
|
| 71 |
+
D, I = index.search(embed_query, TOP)
|
| 72 |
+
|
| 73 |
+
# names index
|
| 74 |
+
import joblib
|
| 75 |
+
audio_names = joblib.load(open('BMG_221022_names.index', 'rb'))
|
| 76 |
+
|
| 77 |
+
#url
|
| 78 |
+
url_dict={}
|
| 79 |
+
with open("bmg_clean.csv") as csv_file:
|
| 80 |
+
csv_reader = csv.reader(csv_file, delimiter=';')
|
| 81 |
+
for row in csv_reader:
|
| 82 |
+
f = row[2].split('/')[-1]
|
| 83 |
+
url_dict[f.split('/')[-1][:-4]] = row[2]
|
| 84 |
+
|
| 85 |
+
# output : top N audio file names
|
| 86 |
+
print(I)
|
| 87 |
+
print(D)
|
| 88 |
+
print("----")
|
| 89 |
+
for i in range(len(I[0])):
|
| 90 |
+
print(audio_names[I[0][i]], " with distance ", D[0][i])
|
| 91 |
+
print(" url : ", url_dict[audio_names[I[0][i]]])
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
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]]]]
|
| 95 |
+
|
| 96 |
+
inputs = [gr.Textbox(label="Input", value="type your description", max_lines=2),
|
| 97 |
+
gr.Radio(label="Language", choices=["en"], value="en")]
|
| 98 |
+
|
| 99 |
+
poc_examples = [#[["I love learning machine learning"],["autre"]]
|
| 100 |
+
["Mysterious filmscore with Arabic influenced instruments"],
|
| 101 |
+
["Let's go on a magical adventure with wizzards, dragons and castles"],
|
| 102 |
+
["Creepy piano opening evolves and speeds up into a cinematic orchestral piece"],
|
| 103 |
+
["Halloween rock with creepy organ"],
|
| 104 |
+
["Rhythmic electro dance track for sport, motivation and sweating"],
|
| 105 |
+
["soundtrack for an action movie from the eighties in a retro synth wave style"],
|
| 106 |
+
["Choral female singing is rhythmically accompanied in a church with medieval instruments"],
|
| 107 |
+
["Christmas"],
|
| 108 |
+
["love romantic with piano, strings and vocals"],
|
| 109 |
+
["Electronic soundscapes for chilling and relaxing"],
|
| 110 |
+
["Minimal, emotional, melancholic piano"],
|
| 111 |
+
["# Minimal, happy, joyful piano"],
|
| 112 |
+
["A calm and romantic acoustic guitar melody"],
|
| 113 |
+
["horror suspense piano"],
|
| 114 |
+
["Big Band"],
|
| 115 |
+
["90 eurodance beat"],
|
| 116 |
+
]
|
| 117 |
+
|
| 118 |
+
'''
|
| 119 |
+
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")]
|
| 120 |
+
demo2 = gr.Interface(fn=process, inputs=inputs, outputs=outputs)
|
| 121 |
+
|
| 122 |
+
demo2.launch(debug=True)
|
| 123 |
+
'''
|
| 124 |
+
|
| 125 |
+
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")]
|
| 126 |
+
demo1 = gr.Interface(fn=process, inputs=inputs, outputs=outputs, examples=poc_examples)
|
| 127 |
+
|
| 128 |
+
demo1.launch(debug=True, share=True)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def id(inp):
|
| 132 |
+
return inp
|
| 133 |
+
|
| 134 |
+
#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")]
|
| 135 |
+
#demo1 = gr.Interface(fn=id, inputs=outputs, outputs=outputs)
|
| 136 |
+
#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")]
|
| 137 |
+
|
| 138 |
+
#all = gr.Series(demo2, demo1)
|
| 139 |
+
#all.launch(debug=True)
|