Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
|
|
|
| 4 |
|
|
|
|
|
|
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained("SAVSNET/PetBERT_ICD")
|
| 6 |
model = AutoModelForSequenceClassification.from_pretrained("SAVSNET/PetBERT_ICD")
|
|
|
|
| 7 |
|
|
|
|
| 8 |
def predict(text):
|
|
|
|
|
|
|
| 9 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
|
|
|
|
|
| 10 |
with torch.no_grad():
|
| 11 |
logits = model(**inputs).logits
|
|
|
|
|
|
|
| 12 |
probs = torch.sigmoid(logits).squeeze().tolist()
|
|
|
|
| 13 |
|
| 14 |
labels = model.config.id2label
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return result or {"์์ธก๋ ์ง๋ณ ์์": "๐คท"}
|
| 17 |
|
|
|
|
| 18 |
demo = gr.Interface(
|
| 19 |
-
fn=predict,
|
| 20 |
-
inputs="
|
| 21 |
-
outputs="
|
| 22 |
-
title="๐พ PetBERT ICD",
|
| 23 |
-
description="
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
|
| 5 |
+
# โ
๋ชจ๋ธ ๋ก๋ฉ
|
| 6 |
+
print("๐ฆ ๋ชจ๋ธ ๋ก๋ฉ ์์...")
|
| 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 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 16 |
+
print("๐งช Tokenized inputs:", inputs)
|
| 17 |
+
|
| 18 |
with torch.no_grad():
|
| 19 |
logits = model(**inputs).logits
|
| 20 |
+
print("๐ Logits:", logits)
|
| 21 |
+
|
| 22 |
probs = torch.sigmoid(logits).squeeze().tolist()
|
| 23 |
+
print("๐ ํ๋ฅ ๊ฐ:", probs)
|
| 24 |
|
| 25 |
labels = model.config.id2label
|
| 26 |
+
print("๐ท๏ธ ๋ผ๋ฒจ ๋ชฉ๋ก:", labels)
|
| 27 |
+
|
| 28 |
+
result = {}
|
| 29 |
+
for i, p in enumerate(probs):
|
| 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 |
+
print("โ
์ต์ข
์์ธก ๊ฒฐ๊ณผ:", result)
|
| 38 |
return result or {"์์ธก๋ ์ง๋ณ ์์": "๐คท"}
|
| 39 |
|
| 40 |
+
# โ
Gradio ์ธํฐํ์ด์ค ์ ์
|
| 41 |
demo = gr.Interface(
|
| 42 |
+
fn=predict, # ์ฌ๊ธฐ์ predict()๊ฐ ํธ์ถ๋จ
|
| 43 |
+
inputs=gr.Textbox(label="๋ฐ๋ ค๋๋ฌผ ์ฆ์ ์
๋ ฅ", placeholder="์: ๊ธฐ์นจ์ ์์ฃผ ํด์"),
|
| 44 |
+
outputs=gr.JSON(label="์์ธก ์ง๋ณ ๋ชฉ๋ก"),
|
| 45 |
+
title="๐พ PetBERT ICD ์์์ฌ ์์ธก๊ธฐ",
|
| 46 |
+
description="๋ฐ๋ ค๋๋ฌผ์ ์ฆ์ ๋ฌธ์ฅ์ ์
๋ ฅํ๋ฉด AI๊ฐ ์ง๋ณ ๊ฐ๋ฅ์ฑ์ ์์ธกํฉ๋๋ค."
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# โ
์ฑ ์คํ
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|