Spaces:
Runtime error
Runtime error
BeMerciless
commited on
Commit
•
2a33573
1
Parent(s):
7885096
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,58 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer
|
4 |
|
5 |
+
#def greet(name):
|
6 |
+
# return "Hello " + name + "!!"
|
7 |
+
|
8 |
+
def greet(sent):
|
9 |
+
|
10 |
+
print("input_sent= " + sent)
|
11 |
+
pt_model = 'best.pt'
|
12 |
+
print(pt_model)
|
13 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
14 |
+
print("device:",device)
|
15 |
+
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
model = torch.load(pt_model, map_location=device)
|
18 |
+
print(model)
|
19 |
+
|
20 |
+
MODEL_NAME = "beomi/KcELECTRA-base" # hugging face 에 등록된 모델
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
22 |
+
|
23 |
+
model.eval() # 평가
|
24 |
+
|
25 |
+
# 입력문장 토크나이징
|
26 |
+
tokenized_sent = tokenizer(
|
27 |
+
sent,
|
28 |
+
return_tensors="pt",
|
29 |
+
truncation=True,
|
30 |
+
add_special_tokens=True,
|
31 |
+
max_length=128
|
32 |
+
)
|
33 |
+
|
34 |
+
# 모델 위치 gpu이동
|
35 |
+
tokenized_sent.to(device)
|
36 |
+
|
37 |
+
# 예측
|
38 |
+
with torch.no_grad():
|
39 |
+
outputs = model(
|
40 |
+
input_ids=tokenized_sent["input_ids"],
|
41 |
+
attention_mask=tokenized_sent["attention_mask"],
|
42 |
+
token_type_ids=tokenized_sent["token_type_ids"],
|
43 |
+
)
|
44 |
+
|
45 |
+
# 결과
|
46 |
+
logits = outputs[0] ## 마지막 노드에서 아무런 Activation Function을 거치지 않은 값을 Logit
|
47 |
+
logits = logits.detach().cpu()
|
48 |
+
result = logits.argmax(-1)
|
49 |
+
if result == 0:
|
50 |
+
result = sent + ">> 악성글로 판단됩니다. 조심하세요."
|
51 |
+
|
52 |
+
elif result ==1:
|
53 |
+
result= sent + ">> 악의적인 내용이 보이지 않습니다."
|
54 |
+
|
55 |
+
return result
|
56 |
|
57 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
58 |
iface.launch()
|