Commit
·
0374802
1
Parent(s):
09529ba
feat(app.py): :sparkles: initialization
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from transformers import AutoTokenizer, AutoModel
|
| 7 |
+
|
| 8 |
+
# Load IndoBERT tokenizer
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("indolem/indobert-base-uncased")
|
| 10 |
+
|
| 11 |
+
# Load IndoBERT model
|
| 12 |
+
model = AutoModel.from_pretrained("indolem/indobert-base-uncased")
|
| 13 |
+
|
| 14 |
+
# Mapping dictionaries for labels
|
| 15 |
+
priority_score_mapping = {1: "low", 2: "medium", 3: "high"}
|
| 16 |
+
problem_domain_mapping = {0: "operational", 1: "tech"}
|
| 17 |
+
|
| 18 |
+
# Load the trained Random Forest models
|
| 19 |
+
best_classifier1 = joblib.load('/content/gdrive/My Drive/Tesis/best_classifier1.pkl')
|
| 20 |
+
best_classifier2 = joblib.load('/content/gdrive/My Drive/Tesis/best_classifier2.pkl')
|
| 21 |
+
|
| 22 |
+
# Function to perform predictions
|
| 23 |
+
def predict(text):
|
| 24 |
+
# Convert the sentences into input features
|
| 25 |
+
encoded_inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt", max_length=128)
|
| 26 |
+
|
| 27 |
+
# Perform word embedding using IndoBERT model
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(**encoded_inputs)
|
| 30 |
+
embeddings = outputs.last_hidden_state
|
| 31 |
+
|
| 32 |
+
# Convert the embeddings to numpy array
|
| 33 |
+
embeddings = embeddings.numpy()
|
| 34 |
+
|
| 35 |
+
embeddings_custom_flat = embeddings.reshape(embeddings.shape[0], -1)
|
| 36 |
+
|
| 37 |
+
# Ensure mean_pooled_embeddings has exactly 768 features
|
| 38 |
+
num_features_expected = 768
|
| 39 |
+
if embeddings_custom_flat.shape[1] < num_features_expected:
|
| 40 |
+
# If the number of features is less than 768, pad the embeddings
|
| 41 |
+
pad_width = num_features_expected - embeddings_custom_flat.shape[1]
|
| 42 |
+
embeddings_custom_flat = np.pad(embeddings_custom_flat, ((0, 0), (0, pad_width)), mode='constant')
|
| 43 |
+
|
| 44 |
+
elif embeddings_custom_flat.shape[1] > num_features_expected:
|
| 45 |
+
# If the number of features is more than 768, truncate the embeddings
|
| 46 |
+
embeddings_custom_flat = embeddings_custom_flat[:, :num_features_expected]
|
| 47 |
+
|
| 48 |
+
# Predict the priority_score for the custom input
|
| 49 |
+
custom_priority_score = best_classifier1.predict(embeddings_custom_flat)
|
| 50 |
+
|
| 51 |
+
# Predict the problem_domain for the custom input
|
| 52 |
+
custom_problem_domain = best_classifier2.predict(embeddings_custom_flat)
|
| 53 |
+
|
| 54 |
+
# Map numerical labels to human-readable labels
|
| 55 |
+
mapped_priority_score = priority_score_mapping.get(custom_priority_score[0], "unknown")
|
| 56 |
+
mapped_problem_domain = problem_domain_mapping.get(custom_problem_domain[0], "unknown")
|
| 57 |
+
|
| 58 |
+
return f"Predicted Priority Score: {mapped_priority_score}, Predicted Problem Domain: {mapped_problem_domain}"
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# Create a Gradio interface
|
| 62 |
+
gr.Interface(fn=predict, inputs="text", outputs="text", title="Simple Risk Classifier Demo").launch(debug=True)
|