Unggi commited on
Commit
ec2527d
1 Parent(s): 3d2306e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pip
2
+ pip.main(['install', 'torch'])
3
+ pip.main(['install', 'transformers'])
4
+
5
+ import torch
6
+ import gradio as gr
7
+ import transformers
8
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
9
+
10
+ def load_model(model_name):
11
+ # model_name = "Unggi/hate_speech_bert"
12
+ # model
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
14
+ # tokenizer..
15
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
16
+
17
+ return model, tokenizer
18
+
19
+
20
+ def inference(prompt):
21
+ model_name = "Unggi/hate_speech_bert"
22
+
23
+ model, tokenizer = load_model(
24
+ model_name = model_name
25
+ )
26
+
27
+ inputs = tokenizer(
28
+ prompt,
29
+ return_tensors="pt"
30
+ )
31
+
32
+ with torch.no_grad():
33
+ logits = model(**inputs).logits
34
+
35
+ predicted_class_id = logits.argmax().item()
36
+ class_id = model.config.id2label[predicted_class_id]
37
+
38
+ return class_id
39
+
40
+ demo = gr.Interface(
41
+ fn=inference,
42
+ inputs="text",
43
+ outputs="text", #return 값
44
+ examples=[
45
+ "아무 방 눌렀다가 미친 게임 봤다"
46
+ ]
47
+ ).launch() # launch(share=True)를 설정하면 외부에서 접속 가능한 링크가 생성됨
48
+
49
+ demo.launch()