Spaces:
Running
Running
File size: 5,359 Bytes
79f8da9 d149d93 c157987 79f8da9 c157987 79f8da9 d149d93 271fe07 79f8da9 c157987 79f8da9 d149d93 79f8da9 d149d93 79f8da9 d149d93 79f8da9 c157987 79f8da9 c157987 d149d93 79f8da9 d149d93 79f8da9 d149d93 c157987 d149d93 c157987 d149d93 c157987 d149d93 c157987 d149d93 c157987 d149d93 c157987 d04c52c d149d93 558d7d3 d149d93 c157987 d149d93 |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
import gradio as gr
import torch
import numpy as np
import json
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from peft import PeftModel
from huggingface_hub import hf_hub_download
import os
# --- 1. CONFIGURATION ---
ADAPTER_REPO = "jvillar-sheff/ag-news-distilbert-lora"
BASE_MODEL_ID = "distilbert-base-uncased"
CLASS_NAMES = {0: "World", 1: "Sports", 2: "Business", 3: "Sci/Tech"}
# --- 2. DYNAMIC METRICS LOADING ---
def fetch_metrics():
"""Downloads evaluation_report.json from the Model Hub."""
try:
file_path = hf_hub_download(repo_id=ADAPTER_REPO, filename="evaluation_report.json")
with open(file_path, "r") as f:
data = json.load(f)
# Extract numbers
acc = data['overall_metrics']['Accuracy']
f1 = data['overall_metrics']['F1 Macro']
return {
"Accuracy": f"{acc:.2%}",
"F1_Score": f"{f1:.4f}"
}
except Exception as e:
print(f"Error loading metrics: {e}")
return {"Accuracy": "N/A", "F1_Score": "N/A"}
# Load metrics on app startup
MODEL_METRICS = fetch_metrics()
# --- 3. MODEL LOADING ---
def load_model():
print("Loading Base Model...")
base_model = AutoModelForSequenceClassification.from_pretrained(
BASE_MODEL_ID,
num_labels=len(CLASS_NAMES),
id2label={k: v for k, v in enumerate(CLASS_NAMES.values())},
label2id={v: k for k, v in CLASS_NAMES.items()}
)
print("Loading Tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(ADAPTER_REPO)
print("Loading Adapters...")
model = PeftModel.from_pretrained(base_model, ADAPTER_REPO)
# Force CPU for Free Tier Spaces
device = torch.device("cpu")
model.to(device)
model.eval()
return model, tokenizer, device
model, tokenizer, device = load_model()
# --- 4. PREDICTION LOGIC ---
def predict(text):
if not text.strip():
return None, None, None
inputs = tokenizer(
text, return_tensors="pt", truncation=True, padding="max_length", max_length=128
).to(device)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
probs = torch.nn.functional.softmax(logits, dim=1).squeeze().cpu().numpy()
# 1. Get Top Label
pred_idx = np.argmax(probs)
pred_label = CLASS_NAMES[pred_idx]
conf = float(probs[pred_idx])
# 2. Create Probability Dict for the Chart
class_probs = {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}
# 3. Create HTML for the "Confidence Badge"
if conf > 0.85:
bg_color, txt_color, icon = "#d4edda", "#155724", "β" # Green
elif conf > 0.60:
bg_color, txt_color, icon = "#fff3cd", "#856404", "~" # Yellow
else:
bg_color, txt_color, icon = "#f8d7da", "#721c24", "β" # Red
badge_html = f"""
<div style='background-color: {bg_color}; color: {txt_color};
padding: 8px 16px; border-radius: 5px; display: inline-block; font-weight: bold; font-size: 16px;'>
{icon} Confidence: {conf:.2%}
</div>
"""
# Return: Label Text, Badge HTML, Chart Data
return f"# {pred_label}", badge_html, class_probs
# --- 5. UI LAYOUT (gr.Blocks) ---
with gr.Blocks() as demo:
gr.Markdown("# π° NLP News Classifier")
gr.Markdown("Classify news articles into World, Sports, Business, or Sci/Tech using DistilBERT + LoRA.")
# -- The "Green Banner" (HTML) --
gr.HTML(f"""
<div style="
background-color: #d1e7dd;
padding: 15px;
border-radius: 5px;
border: 1px solid #badbcc;
margin-bottom: 20px;
color: #0f5132;
">
<span style="color: #0f5132; font-weight: bold;">β
Model Performance (Test Set):</span>
<span style="color: #0f5132;">Accuracy: {MODEL_METRICS['Accuracy']} | F1 Score: {MODEL_METRICS['F1_Score']}</span>
</div>
""")
with gr.Row():
# Left Column: Input
with gr.Column(scale=1):
input_text = gr.Textbox(
lines=6,
placeholder="Paste a news snippet here...",
label="News Article"
)
btn = gr.Button("Classify Article", variant="primary")
gr.Markdown("### Examples")
gr.Examples(
examples=[
["The stock market rallied today as tech companies reported record profits."],
["The local team won the championship after a stunning overtime goal."],
["NASA announces plans to launch a new rover to Mars next July."]
],
inputs=input_text
)
# Right Column: Results
with gr.Column(scale=1):
gr.Markdown("### Prediction")
# Output 1: Big Label text
out_label = gr.Markdown()
# Output 2: The Colored Badge
out_badge = gr.HTML()
gr.Markdown("### Probability Breakdown")
# Output 3: Bar Chart
out_chart = gr.Label(num_top_classes=4, label="Confidence Scores")
# Wire up the button
btn.click(
fn=predict,
inputs=input_text,
outputs=[out_label, out_badge, out_chart]
)
# Launch
if __name__ == "__main__":
demo.launch() |