Spaces:
Sleeping
Sleeping
| 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() | |