Spaces:
Runtime error
Runtime error
Commit Β·
7f55279
1
Parent(s): 1b37578
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ import csv
|
|
| 11 |
import datetime
|
| 12 |
|
| 13 |
from huggingface_hub import hf_hub_download
|
| 14 |
-
encoder_text_path = hf_hub_download(repo_id="PierreHanna/TextRetrieval", repo_type="space", filename=
|
| 15 |
use_auth_token=os.environ['TOKEN'])
|
| 16 |
print("DEBUG ", encoder_text_path)
|
| 17 |
# NO GPU
|
|
@@ -19,58 +19,23 @@ os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
| 19 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 20 |
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
python_path = hf_hub_download(repo_id="PierreHanna/TextRetrieval", repo_type="space", filename="models.py",
|
| 25 |
-
use_auth_token=os.environ['TOKEN']
|
|
|
|
| 26 |
print(python_path)
|
| 27 |
-
os.system('ls -la')
|
| 28 |
#from models import *
|
| 29 |
|
| 30 |
-
sys.path.append(
|
| 31 |
-
|
| 32 |
-
#import site
|
| 33 |
-
#site.addsitedir('./ph.spaces--PierreHanna--TextRetrieval.snapshots.ee43bbe093de2cd1b2fbda7c04d00ed4d360d730')
|
| 34 |
-
#from ph.spaces--PierreHanna--TextRetrieval.snapshots.ee43bbe093de2cd1b2fbda7c04d00ed4d360d730.models import *
|
| 35 |
from models import *
|
| 36 |
|
| 37 |
-
def make_preprocess_model(sentence_features, tfhub_handle_preprocess, seq_length=128):
|
| 38 |
-
"""Returns Model mapping string features to BERT inputs.
|
| 39 |
-
"""
|
| 40 |
-
|
| 41 |
-
input_segments = [
|
| 42 |
-
tf.keras.layers.Input(shape=(), dtype=tf.string, name=ft)
|
| 43 |
-
for ft in sentence_features]
|
| 44 |
-
|
| 45 |
-
bert_preprocess = hub.load(tfhub_handle_preprocess)
|
| 46 |
-
tokenizer = hub.KerasLayer(bert_preprocess.tokenize, name='tokenizer')
|
| 47 |
-
segments = [tokenizer(s) for s in input_segments]
|
| 48 |
-
|
| 49 |
-
truncated_segments = segments
|
| 50 |
-
packer = hub.KerasLayer(bert_preprocess.bert_pack_inputs,
|
| 51 |
-
arguments=dict(seq_length=seq_length),
|
| 52 |
-
name='packer')
|
| 53 |
-
model_inputs = packer(truncated_segments)
|
| 54 |
-
return tf.keras.Model(input_segments, model_inputs)
|
| 55 |
-
|
| 56 |
def process(prompt, lang):
|
| 57 |
|
| 58 |
# Getting prompt user
|
| 59 |
#prompt = input("Audio Search - enter text : ")
|
| 60 |
#print(prompt)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
bert_model_name = 'small_bert/bert_en_uncased_L-4_H-512_A-8'
|
| 64 |
-
tfhub_handle_encoder = 'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-512_A-8/1'
|
| 65 |
-
tfhub_handle_preprocess = 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3'
|
| 66 |
-
|
| 67 |
-
MAX_LENGTH = 130 # MAX de 512 !!! TENSORFLOW !!!
|
| 68 |
-
TOP = 10
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
bert_preprocess_model = make_preprocess_model(['my_input'], tfhub_handle_preprocess, seq_length = MAX_LENGTH)
|
| 72 |
-
bert_model = hub.KerasLayer(tfhub_handle_encoder)
|
| 73 |
-
|
| 74 |
now = datetime.datetime.now()
|
| 75 |
print()
|
| 76 |
print('*************')
|
|
@@ -79,8 +44,8 @@ def process(prompt, lang):
|
|
| 79 |
print('*************')
|
| 80 |
print()
|
| 81 |
prompt=[prompt]
|
| 82 |
-
text_preprocessed =
|
| 83 |
-
embed_prompt =
|
| 84 |
print(" text representation computed.")
|
| 85 |
|
| 86 |
# Embed text
|
|
|
|
| 11 |
import datetime
|
| 12 |
|
| 13 |
from huggingface_hub import hf_hub_download
|
| 14 |
+
encoder_text_path = hf_hub_download(repo_id="PierreHanna/TextRetrieval", repo_type="space", filename=os.environ['ENCODER_TXT'],
|
| 15 |
use_auth_token=os.environ['TOKEN'])
|
| 16 |
print("DEBUG ", encoder_text_path)
|
| 17 |
# NO GPU
|
|
|
|
| 19 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
| 22 |
python_path = hf_hub_download(repo_id="PierreHanna/TextRetrieval", repo_type="space", filename="models.py",
|
| 23 |
+
use_auth_token=os.environ['TOKEN'])
|
| 24 |
+
|
| 25 |
print(python_path)
|
| 26 |
+
#os.system('ls -la')
|
| 27 |
#from models import *
|
| 28 |
|
| 29 |
+
sys.path.append(os.environ['PRIVATE_DIR'])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
from models import *
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def process(prompt, lang):
|
| 33 |
|
| 34 |
# Getting prompt user
|
| 35 |
#prompt = input("Audio Search - enter text : ")
|
| 36 |
#print(prompt)
|
| 37 |
+
preprocess_model, model = get_models()
|
| 38 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
now = datetime.datetime.now()
|
| 40 |
print()
|
| 41 |
print('*************')
|
|
|
|
| 44 |
print('*************')
|
| 45 |
print()
|
| 46 |
prompt=[prompt]
|
| 47 |
+
text_preprocessed = preprocess_model([np.array(prompt)])
|
| 48 |
+
embed_prompt = model(text_preprocessed)
|
| 49 |
print(" text representation computed.")
|
| 50 |
|
| 51 |
# Embed text
|