Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,42 @@
|
|
| 1 |
-
|
| 2 |
-
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("SAVSNET/PetBERT_ICD")
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained("SAVSNET/PetBERT_ICD")
|
| 9 |
-
print("โ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 10 |
-
|
| 11 |
-
# โ
์์ธก ํจ์ ์ ์
|
| 12 |
-
def predict(text):
|
| 13 |
-
print("\n๐ฅ ์
๋ ฅ ํ
์คํธ:", text)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
| 18 |
with torch.no_grad():
|
| 19 |
logits = model(**inputs).logits
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
probs = torch.sigmoid(logits).squeeze().tolist()
|
| 23 |
-
print("๐ ํ๋ฅ ๊ฐ:", probs)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
try:
|
| 31 |
-
label = labels[str(i)] # โ str(i) ์ค์
|
| 32 |
-
if p > 0.5:
|
| 33 |
-
result[label] = f"{p:.1%}"
|
| 34 |
-
except Exception as e:
|
| 35 |
-
print(f"โ ๋ผ๋ฒจ ๋งคํ ์ค๋ฅ: {i} โ {e}")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
return result
|
| 39 |
|
| 40 |
-
#
|
| 41 |
demo = gr.Interface(
|
| 42 |
-
fn=
|
| 43 |
-
inputs=gr.Textbox(label="๋ฐ๋ ค๋๋ฌผ ์ฆ์ ์
๋ ฅ", placeholder="์:
|
| 44 |
-
outputs=
|
| 45 |
-
title="๐พ PetBERT ICD ์์์ฌ ์์ธก๊ธฐ",
|
| 46 |
-
description="๋ฐ๋ ค๋๋ฌผ
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
if __name__ == "__main__":
|
| 51 |
-
demo.launch()
|
|
|
|
| 1 |
+
from googletrans import Translator
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
# ๋ฒ์ญ๊ธฐ ์ด๊ธฐํ
|
| 7 |
+
translator = Translator()
|
| 8 |
+
|
| 9 |
+
# PetBERT ๋ชจ๋ธ ๋ก๋ฉ
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("SAVSNET/PetBERT_ICD")
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained("SAVSNET/PetBERT_ICD")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def predict_korean(text_kr):
|
| 14 |
+
# ํ๊ธ โ ์์ด ๋ฒ์ญ
|
| 15 |
+
translated = translator.translate(text_kr, src='ko', dest='en').text
|
| 16 |
+
print(f"๐ ๋ฒ์ญ๋ ๋ฌธ์ฅ: {translated}")
|
| 17 |
|
| 18 |
+
# ๋ชจ๋ธ ์
๋ ฅ ์ฒ๋ฆฌ
|
| 19 |
+
inputs = tokenizer(translated, return_tensors="pt", truncation=True, padding=True)
|
| 20 |
with torch.no_grad():
|
| 21 |
logits = model(**inputs).logits
|
| 22 |
+
probs = torch.sigmoid(logits)
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
threshold = 0.3 # ๋ฎ์ ์๊ณ๊ฐ
|
| 25 |
+
predicted = (probs > threshold).nonzero(as_tuple=True)[1]
|
| 26 |
|
| 27 |
+
if len(predicted) == 0:
|
| 28 |
+
return {"์์ธก๋ ์ง๋ณ ์์": "๐ถ"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
result = {model.config.id2label[idx.item()]: f"{probs[0][idx].item():.2f}" for idx in predicted}
|
| 31 |
+
return result
|
| 32 |
|
| 33 |
+
# Gradio ์ธํฐํ์ด์ค
|
| 34 |
demo = gr.Interface(
|
| 35 |
+
fn=predict_korean,
|
| 36 |
+
inputs=gr.Textbox(label="๋ฐ๋ ค๋๋ฌผ ์ฆ์ ์
๋ ฅ", placeholder="์: ๊ฐ์์ง๊ฐ ์์ฃผ ๊ธฐ์นจํด์."),
|
| 37 |
+
outputs="json",
|
| 38 |
+
title="๐พ PetBERT ICD ์์์ฌ ์์ธก๊ธฐ (ํ๊ธ ์ง์)",
|
| 39 |
+
description="ํ๊ธ๋ก ๋ฐ๋ ค๋๋ฌผ ์ฆ์์ ์
๋ ฅํ๋ฉด ์์ด๋ก ๋ฒ์ญํ ๋ค, AI๊ฐ ์ง๋ณ์ ์์ธกํฉ๋๋ค."
|
| 40 |
)
|
| 41 |
|
| 42 |
+
demo.launch()
|
|
|
|
|
|