SHK4K commited on
Commit
2ca7d5d
·
1 Parent(s): e569939

Add application file

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ pipe = pipeline(
5
+ "text-classification",
6
+ model="SHK4K/suicide-roberta",
7
+ )
8
+
9
+ def is_safe(text,exp=None):
10
+ inputs = tokenizer(
11
+ text,
12
+ padding=True,
13
+ truncation=True,
14
+ max_length=512,
15
+ return_tensors="pt",
16
+ )
17
+ with torch.no_grad():
18
+ logits = model(**inputs).logits
19
+ predicted_label = logits.argmax(dim=-1).item()
20
+ confidence = logits.softmax(dim=-1)[0][predicted_label].item()
21
+
22
+ label_map = {0: "safe", 1: "unsafe"}
23
+
24
+ print(f"Prediction: {label_map[predicted_label]} (confidence: {confidence:.3f}) {f"Expected: {label_map[exp]}" if exp is not None else ""}")
25
+ return label_map[predicted_label]
26
+
27
+ demo = gr.Interface(
28
+ fn=is_safe,
29
+ inputs="textbox",
30
+ outputs="label",
31
+ )
32
+
33
+ demo.launch(share=True)