stock-sentiment / app.py
jccompany2007's picture
Upload app.py
493074c verified
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()