Upload 3 files
Browse files- app.py +16 -0
- chatbot.py +37 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from chatbot import predict_multi, generate_response
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="๊ฐ์ ๊ณต๊ฐ ์ฑ๋ด")
|
| 6 |
+
st.title("T / F ๊ฐ์ ๊ณต๊ฐ ์ฑ๋ด ๐ฌ")
|
| 7 |
+
|
| 8 |
+
text = st.text_area("๋ง์์ ์ด์ผ๊ธฐ๋ฅผ ๋ค๋ ค์ฃผ์ธ์")
|
| 9 |
+
mode = st.radio("์ฑ๋ด ์๋ต ์คํ์ผ", ["T - ์ด์ฑ์ ์กฐ์ธ", "F - ๊ฐ์ฑ์ ์๋ก"])
|
| 10 |
+
|
| 11 |
+
if st.button("์ฑ๋ด์๊ฒ ๋ง ๊ฑธ๊ธฐ"):
|
| 12 |
+
flag = 'T' if mode.startswith('T') else 'F'
|
| 13 |
+
emotions = predict_multi(text)
|
| 14 |
+
st.write("### ์์ธก๋ ๊ฐ์ :", ", ".join(emotions))
|
| 15 |
+
response = generate_response(emotions, flag)
|
| 16 |
+
st.success(response)
|
chatbot.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 5 |
+
|
| 6 |
+
# ๋ชจ๋ธ ๋ก๋ฉ
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained("M1NJ1/klue-bert-emotion")
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("M1NJ1/klue-bert-emotion")
|
| 9 |
+
model.eval().to("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
|
| 11 |
+
label_map = list(range(44)) # ์ค์ ๊ฐ์ ๋ผ๋ฒจ ์ด๋ฆ์ผ๋ก ์์ ๊ฐ๋ฅ
|
| 12 |
+
|
| 13 |
+
def predict_multi(text):
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=64).to(device)
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = model(**inputs)
|
| 18 |
+
probs = torch.sigmoid(outputs.logits).squeeze().cpu().numpy()
|
| 19 |
+
emotions = []
|
| 20 |
+
for i, p in enumerate(probs):
|
| 21 |
+
if p > 0.5:
|
| 22 |
+
emotions.append(f"๊ฐ์ {i}")
|
| 23 |
+
return emotions
|
| 24 |
+
|
| 25 |
+
def generate_response(emotions, mode):
|
| 26 |
+
if not emotions:
|
| 27 |
+
return "๊ฐ์ ์ ํ์
ํ์ง ๋ชปํ์ด์. ๋ค์ ๋งํด์ค ์ ์๋์?"
|
| 28 |
+
response = ""
|
| 29 |
+
if mode == 'T':
|
| 30 |
+
if '๊ฐ์ 1' in emotions:
|
| 31 |
+
response += "๋ถ์์ ์ค๋น ๋ถ์กฑ์์ ์ฌ ์ ์์ด์. ์ฐจ๊ทผํ ์ ๋ฆฌํด๋ณผ๊น์?
|
| 32 |
+
"
|
| 33 |
+
elif mode == 'F':
|
| 34 |
+
if '๊ฐ์ 1' in emotions:
|
| 35 |
+
response += "๋ถ์ํ ํ๋ฃจ์๊ฒ ๊ตฐ์... ๊ด์ฐฎ์์, ๋น์ ์ํ๊ณ ์์ด์.
|
| 36 |
+
"
|
| 37 |
+
return response or "๋น์ ์ ๊ฐ์ ์ ๊ท ๊ธฐ์ธ์ด๊ณ ์์ด์."
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
streamlit
|
| 3 |
+
transformers
|
| 4 |
+
torch
|