FiratIsmailoglu's picture
Update app.py
777565c verified
import torch
import torch.nn.functional as F
import gradio as gr
from transformers import BigBirdConfig, AutoTokenizer
from huggingface_hub import hf_hub_download
from bigbird_anayasa_classifier import BigBirdClassifier
REPO_ID = "FiratIsmailoglu/bigbird_anayasa_classifier"
# Load tokenizer and config
tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
config = BigBirdConfig.from_pretrained(REPO_ID)
# Build model
model = BigBirdClassifier(config)
# Download weights from HF
weights_path = hf_hub_download(REPO_ID, filename="anayasa_bigbird_classifier_model.bin")
state_dict = torch.load(weights_path, map_location="cpu")
model.load_state_dict(state_dict)
model.eval()
def classify(text):
enc = tokenizer(
text,
truncation=True,
padding="max_length",
max_length=3072,
return_tensors="pt",
)
with torch.no_grad():
logits = model(enc["input_ids"], enc["attention_mask"])
probs = F.softmax(logits, dim=-1)[0].numpy()
labels = config.id2label
pred = int(probs.argmax())
pred_label = labels[pred]
probs_dict = {labels[i]: float(probs[i]) for i in range(len(probs))}
return f"Predicted: **{pred_label}**", probs_dict
with gr.Blocks() as demo:
gr.Markdown("# BigBird Tabanlı Metin Sınıflandırma")
text_input = gr.Textbox(lines=10, label="Metni girin")
out_label = gr.Markdown()
out_probs = gr.Label()
btn = gr.Button("Sınıflandır")
btn.click(classify, text_input, [out_label, out_probs])
demo.launch()