Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch | |
| import torch.nn.functional as F | |
| # ๋ชจ๋ธ๋ช : ์์๋ก KR-FinBERT ๊ฐ์ ๋ถ์ ๋ชจ๋ธ ์ฌ์ฉ (๊ธ์ต ๋ด์ค์ ์ ํฉ) | |
| MODEL_NAME = "snunlp/KR-FinBERT" | |
| # ๋ชจ๋ธ/ํ ํฌ๋์ด์ ๋ก๋ฉ (์ต์ด 1ํ) | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) | |
| # ๋ผ๋ฒจ ๋งคํ (๋ชจ๋ธ์ ๋ฐ๋ผ ๋ค๋ฅผ ์ ์์) | |
| id2label = {0: "neutral", 1: "positive", 2: "negative"} | |
| def predict_sentiment(text): | |
| # ์ ๋ ฅ ์ ์ฒ๋ฆฌ ๋ฐ ํ ํฐํ | |
| inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=256) | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| logits = outputs.logits | |
| probs = F.softmax(logits, dim=1).squeeze().tolist() | |
| pred_id = int(torch.argmax(logits, dim=1)) | |
| label = id2label[pred_id] | |
| score = probs[pred_id] | |
| return {"label": label, "score": round(score, 4), "probs": {id2label[i]: round(p, 4) for i, p in enumerate(probs)}} | |
| with gr.Blocks(title="๊ตญ๋ด ๋ด์ค ๊ฐ์ ๋ถ์ (KR-FinBERT)") as demo: | |
| gr.Markdown("# ๊ตญ๋ด ๋ด์ค ๊ฐ์ ๋ถ์ (KR-FinBERT)") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_text = gr.Textbox(label="๋ด์ค ๋ณธ๋ฌธ", lines=12) | |
| submit_btn = gr.Button("Submit") | |
| with gr.Column(): | |
| output_json = gr.JSON(label="๋ถ์ ๊ฒฐ๊ณผ") | |
| submit_btn.click(predict_sentiment, input_text, output_json) | |
| demo.launch() |