| import gradio as gr |
| import torch |
| import joblib |
| import numpy as np |
| import pandas as pd |
| from transformers import AutoTokenizer, AutoModel |
|
|
| |
| tokenizer = AutoTokenizer.from_pretrained("indolem/indobert-base-uncased") |
|
|
| |
| model = AutoModel.from_pretrained("indolem/indobert-base-uncased") |
|
|
| |
| priority_score_mapping = {1: "LOW", 2: "MEDIUM", 3: "HIGH"} |
| problem_domain_mapping = {0: "OPERATIONAL", 1: "TECHNICAL"} |
|
|
| |
| best_classifier1 = joblib.load('best_classifier1_optimized.pkl') |
| best_classifier2 = joblib.load('best_classifier2_optimized.pkl') |
|
|
| markdown_text = ''' |
| ## Label Description |
| |
| ### Priority Score |
| * **Low** label, means that the temporary/corrective solution can solve the problem. A permanent solution will be provided later because the impact on the business can still be handled. |
| * **Medium** label, means that there's a need to determine the time constraint to solve the problem. If it remains too long, it will impact the business side. |
| * **High** label, means that the problem is urgent and must be solved immediately. |
| |
| ### Problem Domain |
| * **Operational** label, means that the scope of the problem is on the business or daily operational. |
| * **Technical** label, means that the scope of the problem is on the technical (technology) side like the mobile/web application. |
| ''' |
|
|
| description="Write the feedback about the capsule hotel that you've ever visited or stayed there. The machine learning model will predict the priority score and problem domain of the feedback." |
|
|
| |
| def predict(text): |
| |
| encoded_inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt", max_length=128) |
|
|
| |
| with torch.no_grad(): |
| outputs = model(**encoded_inputs) |
| embeddings = outputs.last_hidden_state |
|
|
| |
| embeddings = embeddings.numpy() |
|
|
| embeddings_custom_flat = embeddings.reshape(embeddings.shape[0], -1) |
|
|
| |
| num_features_expected = 768 |
| if embeddings_custom_flat.shape[1] < num_features_expected: |
| |
| pad_width = num_features_expected - embeddings_custom_flat.shape[1] |
| embeddings_custom_flat = np.pad(embeddings_custom_flat, ((0, 0), (0, pad_width)), mode='constant') |
|
|
| elif embeddings_custom_flat.shape[1] > num_features_expected: |
| |
| embeddings_custom_flat = embeddings_custom_flat[:, :num_features_expected] |
|
|
| |
| custom_priority_score = best_classifier1.predict(embeddings_custom_flat) |
|
|
| |
| custom_problem_domain = best_classifier2.predict(embeddings_custom_flat) |
|
|
| |
| mapped_priority_score = priority_score_mapping.get(custom_priority_score[0], "unknown") |
| mapped_problem_domain = problem_domain_mapping.get(custom_problem_domain[0], "unknown") |
|
|
| return f"Predicted Priority Score: {mapped_priority_score}, Predicted Problem Domain: {mapped_problem_domain}" |
|
|
|
|
| |
| gr.Interface(fn=predict, inputs="text", outputs="text", title="Simple Risk Classifier Demo (Case Study: Capsule Hotel)", description=description, article=markdown_text).launch(debug=True) |