FiratIsmailoglu commited on
Commit
9309766
·
verified ·
1 Parent(s): 78d6c06

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn.functional as F
3
+ import gradio as gr
4
+ from transformers import BigBirdConfig, AutoTokenizer
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ from bigbird_anayasa_classifier import BigBirdClassifier
8
+
9
+
10
+ REPO_ID = "FiratIsmailoglu/bigbird_anayasa_classifier"
11
+
12
+ # Load tokenizer and config
13
+ tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
14
+ config = BigBirdConfig.from_pretrained(REPO_ID)
15
+
16
+ # Build model
17
+ model = BigBirdClassifier(config)
18
+
19
+ # Download weights from HF
20
+ weights_path = hf_hub_download(REPO_ID, filename="anayasa_bigbird_classifier_model.bin")
21
+ state_dict = torch.load(weights_path, map_location="cpu")
22
+ model.load_state_dict(state_dict)
23
+ model.eval()
24
+
25
+ def classify(text):
26
+ enc = tokenizer(
27
+ text,
28
+ truncation=True,
29
+ padding="max_length",
30
+ max_length=3072,
31
+ return_tensors="pt",
32
+ )
33
+
34
+ with torch.no_grad():
35
+ logits = model(enc["input_ids"], enc["attention_mask"])
36
+ probs = F.softmax(logits, dim=-1)[0].numpy()
37
+
38
+ labels = config.id2label
39
+ pred = int(probs.argmax())
40
+ pred_label = labels[pred]
41
+
42
+ probs_dict = {labels[i]: float(probs[i]) for i in range(len(probs))}
43
+
44
+ return f"Predicted: **{pred_label}**", probs_dict
45
+
46
+
47
+ with gr.Blocks() as demo:
48
+ gr.Markdown("# BigBird Tabanlı Metin Sınıflandırma")
49
+ text_input = gr.Textbox(lines=10, label="Metni girin")
50
+ out_label = gr.Markdown()
51
+ out_probs = gr.Label()
52
+
53
+ btn = gr.Button("Sınıflandır")
54
+ btn.click(classify, text_input, [out_label, out_probs])
55
+
56
+ demo.launch()