Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,14 @@ from tensorflow.keras.models import load_model
|
|
| 7 |
from official.nlp.data import classifier_data_lib
|
| 8 |
from official.nlp.tools import tokenization
|
| 9 |
import joblib
|
|
|
|
|
|
|
| 10 |
|
| 11 |
model = load_model('best_model.h5', custom_objects={'KerasLayer': hub.KerasLayer})
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
vocab_file = model.resolved_object.vocab_file.asset_path.numpy()
|
| 15 |
do_lower_case = model.resolved_object.do_lower_case.numpy()
|
|
@@ -53,4 +58,37 @@ def preprocess_new_data(texts):
|
|
| 53 |
dataset = dataset.batch(32, drop_remainder=False)
|
| 54 |
dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)
|
| 55 |
|
| 56 |
-
return dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from official.nlp.data import classifier_data_lib
|
| 8 |
from official.nlp.tools import tokenization
|
| 9 |
import joblib
|
| 10 |
+
import zipfile
|
| 11 |
+
import gradio as gr
|
| 12 |
|
| 13 |
model = load_model('best_model.h5', custom_objects={'KerasLayer': hub.KerasLayer})
|
| 14 |
|
| 15 |
+
with zipfile.ZipFile('model.zip', 'r') as zip_ref:
|
| 16 |
+
zip_ref.extractall('model')
|
| 17 |
+
|
| 18 |
|
| 19 |
vocab_file = model.resolved_object.vocab_file.asset_path.numpy()
|
| 20 |
do_lower_case = model.resolved_object.do_lower_case.numpy()
|
|
|
|
| 58 |
dataset = dataset.batch(32, drop_remainder=False)
|
| 59 |
dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)
|
| 60 |
|
| 61 |
+
return dataset
|
| 62 |
+
def launch(input):
|
| 63 |
+
# Load the label encoder
|
| 64 |
+
label_encoder = joblib.load('label_encoder.joblib')
|
| 65 |
+
|
| 66 |
+
# Preprocess the new data
|
| 67 |
+
sample_example = [input]
|
| 68 |
+
new_data_dataset = preprocess_new_data(sample_example)
|
| 69 |
+
|
| 70 |
+
# Assuming you have a model already loaded (add model loading code if needed)
|
| 71 |
+
# Make predictions on the new data
|
| 72 |
+
predictions = model.predict(new_data_dataset)
|
| 73 |
+
|
| 74 |
+
# Decode the predictions
|
| 75 |
+
predicted_classes = [label_list[np.argmax(pred)] for pred in predictions]
|
| 76 |
+
|
| 77 |
+
# Print the predicted classes
|
| 78 |
+
print(predicted_classes)
|
| 79 |
+
|
| 80 |
+
# Calculate the highest probabilities
|
| 81 |
+
highest_probabilities = [max(instance) for instance in predictions]
|
| 82 |
+
|
| 83 |
+
# Decode labels using the label encoder
|
| 84 |
+
decoded_labels = label_encoder.inverse_transform(predicted_classes)
|
| 85 |
+
|
| 86 |
+
print("Most likely ISCO code is {} and probability is {}".format(decoded_labels,highest_probabilities))
|
| 87 |
+
|
| 88 |
+
# Gradio Interface
|
| 89 |
+
iface = gr.Interface(fn=launch,
|
| 90 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter job title and description here..."),
|
| 91 |
+
outputs="text")
|
| 92 |
+
|
| 93 |
+
# Launch the Gradio app
|
| 94 |
+
iface.launch()
|