M1NJ1 commited on
Commit
eadeab6
ยท
verified ยท
1 Parent(s): 66c3b6d

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +16 -0
  2. chatbot.py +37 -0
  3. 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