Spaces:
Sleeping
Sleeping
File size: 1,599 Bytes
961d809 a6dcc9d 961d809 a6dcc9d 961d809 a6dcc9d 961d809 a6dcc9d 961d809 a6dcc9d 961d809 a6dcc9d 961d809 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# --- Configuration ---
MODEL_NAME = "distilbert-base-uncased"
NUM_LABELS = 6
MODEL_PATH = "controlled_bert_model.pth" # The name of the file you uploaded
# --- Load Tokenizer and Model ---
print("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
print("Loading model architecture...")
# First, create the model "shell"
model = AutoModelForSequenceClassification.from_pretrained(
MODEL_NAME,
num_labels=NUM_LABELS
)
print(f"Loading fine-tuned weights from {MODEL_PATH}...")
# Now, load your trained weights into the shell
model.load_state_dict(
torch.load(MODEL_PATH, map_location=torch.device("cpu"))
)
model.eval() # Set model to evaluation mode
print("Model loaded successfully!")
def classify_log(log_text):
"""
This function runs the classification using your loaded .pth model.
"""
inputs = tokenizer(log_text, return_tensors="pt", padding=True, truncation=True)
with torch.no_grad():
logits = model(**inputs).logits
scores = torch.softmax(logits, dim=1).squeeze().tolist()
# Create a dictionary of {label_name: score}
confidences = {model.config.id2label[i]: score for i, score in enumerate(scores)}
return confidences
# This creates the Gradio interface and API endpoint
gr.Interface(
fn=classify_log,
inputs=gr.Textbox(lines=5, label="Log Entry"),
outputs=gr.Label(num_top_classes=6, label="Classification Results"),
title="Infrnce Private Log Classifier API"
).launch()
|