JustParadis commited on
Commit
fc42c6d
·
verified ·
1 Parent(s): 2dced59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -1,18 +1,37 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- clf = pipeline(
5
- "text-classification",
6
- model="JustParadis/indobert-sentiment-comment",
7
- return_all_scores=True
8
- )
 
 
9
 
10
  def predict(text):
11
- return clf(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  gr.Interface(
14
  fn=predict,
15
  inputs=gr.Textbox(lines=4, placeholder="Masukkan komentar"),
16
  outputs=gr.JSON(label="Prediction"),
17
- title="IndoBERT Comment Sentiment"
 
18
  ).launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
 
5
+ MODEL_ID = "JustParadis/indobert-sentiment-comment"
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
8
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
9
+ model.eval()
10
+
11
+ LABELS = model.config.id2label # uses labels from training config
12
 
13
  def predict(text):
14
+ inputs = tokenizer(
15
+ text,
16
+ return_tensors="pt",
17
+ truncation=True,
18
+ padding=True,
19
+ max_length=128
20
+ )
21
+
22
+ with torch.no_grad():
23
+ outputs = model(**inputs)
24
+ probs = torch.softmax(outputs.logits, dim=-1)[0]
25
+
26
+ return {
27
+ LABELS[i]: float(probs[i])
28
+ for i in range(len(probs))
29
+ }
30
 
31
  gr.Interface(
32
  fn=predict,
33
  inputs=gr.Textbox(lines=4, placeholder="Masukkan komentar"),
34
  outputs=gr.JSON(label="Prediction"),
35
+ title="IndoBERT Comment Sentiment",
36
+ description="2-label comment sentiment classification"
37
  ).launch()