Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BertConfig, BertForSequenceClassification, AutoTokenizer
|
| 3 |
+
from safetensors import safe_open
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
config_path = "modelbert2/config.json"
|
| 7 |
+
config = BertConfig.from_json_file(config_path)
|
| 8 |
+
model = BertForSequenceClassification(config)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
model_path = "modelbert2"
|
| 12 |
+
model = BertForSequenceClassification.from_pretrained(model_path)
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 14 |
+
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained("modelbert2/")
|
| 16 |
+
|
| 17 |
+
# Load the label encoder
|
| 18 |
+
import pickle
|
| 19 |
+
with open('label_encoder.pkl', 'rb') as file:
|
| 20 |
+
label_encoder = pickle.load(file)
|
| 21 |
+
|
| 22 |
+
def predict(text):
|
| 23 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = model(**inputs)
|
| 26 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 27 |
+
predicted_class = torch.argmax(probs, dim=-1).item()
|
| 28 |
+
predicted_label = label_encoder.inverse_transform([predicted_class])[0]
|
| 29 |
+
return predicted_label
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# Define the markdown text with bullet points
|
| 33 |
+
markdown_text = """
|
| 34 |
+
- This is for test purpose only.
|
| 35 |
+
- Input one budget line per time.
|
| 36 |
+
- Accuracy of the model is around 72%.
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
# Define the interface
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=predict,
|
| 42 |
+
inputs=gr.inputs.Textbox(lines=1, placeholder="Enter Budget line here..."),
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="COFOG Level 1 Classification",
|
| 45 |
+
description=markdown_text # Add the markdown text to the description
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Run the interface
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
iface.launch()
|