Spaces:
Sleeping
Sleeping
Fix: Load model from local .pth file instead of Hub
Browse files
app.py
CHANGED
|
@@ -1,17 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
model = AutoModelForTokenClassification.from_pretrained(
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def predict_ner(text):
|
| 17 |
"""
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 3 |
import torch
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Label mapping
|
| 7 |
+
LABEL_LIST = ['O', 'B-Organisation', 'I-Organisation', 'B-Person', 'I-Person', 'B-Place', 'I-Place']
|
| 8 |
+
LABEL_TO_ID = {l: i for i, l in enumerate(LABEL_LIST)}
|
| 9 |
+
ID_TO_LABEL = {i: l for l, i in LABEL_TO_ID.items()}
|
| 10 |
|
| 11 |
+
# Base model
|
| 12 |
+
BASE_MODEL = "ChristopherA08/IndoELECTRA"
|
| 13 |
+
MODEL_PATH = "./model.pth"
|
| 14 |
+
|
| 15 |
+
print("Loading tokenizer and model...")
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 17 |
+
|
| 18 |
+
# Load model architecture
|
| 19 |
model = AutoModelForTokenClassification.from_pretrained(
|
| 20 |
+
BASE_MODEL,
|
| 21 |
+
num_labels=len(LABEL_LIST),
|
| 22 |
+
label2id=LABEL_TO_ID,
|
| 23 |
+
id2label=ID_TO_LABEL
|
| 24 |
)
|
| 25 |
+
|
| 26 |
+
# Resize embeddings (penting!)
|
| 27 |
+
model.resize_token_embeddings(len(tokenizer))
|
| 28 |
+
|
| 29 |
+
# Load weights dari .pth file
|
| 30 |
+
if os.path.exists(MODEL_PATH):
|
| 31 |
+
print(f"Loading weights from {MODEL_PATH}...")
|
| 32 |
+
state_dict = torch.load(MODEL_PATH, map_location="cpu")
|
| 33 |
+
model.load_state_dict(state_dict)
|
| 34 |
+
print("✅ Model loaded successfully!")
|
| 35 |
+
else:
|
| 36 |
+
print(f"⚠️ Warning: {MODEL_PATH} not found. Using base model.")
|
| 37 |
|
| 38 |
def predict_ner(text):
|
| 39 |
"""
|
model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:005c48c9d56e3718ee8f4269303d63b97dd269817dba1e7a1e87d0ad8235cba0
|
| 3 |
+
size 449428284
|