1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| import streamlit as st from streamlit_chat import message
import pandas as pd from sentence_transformers import SentenceTransformer from sklearn.metrics.pairwise import cosine_similarity import json import os from typing import Literal, Optional, Union import streamlit.components.v1 as components
@st.cache(allow_output_mutation=True)
def cached_model(): model = SentenceTransformer('jhgan/ko-sroberta-multitask') return model
@st.cache(allow_output_mutation=True) def get_dataset(): df = pd.read_csv('wellness_dataset_final.csv') df['embedding'] = df['embedding'].apply(json.loads) return df
model = cached_model() df = get_dataset()
st.title('자연어처리 프로젝트') st.header('심리상담 챗봇')
st.markdown("❤️chatbot_think_big")
st.markdown(""" 🙂 자연어처리 1차 팀프로젝트를 위한 심리 상담 챗봇입니다. """ """ 💜 SentenceTransformer를 사용하여 문장을 임베딩하고 이를 코사인 유사도와 함께 비교하여 가장 유사한 답변을 채택합니다. """)
st.sidebar.header("NLP PROJECT") st.sidebar.subheader("TEAM : Think_Big") st.sidebar.subheader("팀원") st.sidebar.text("조인환(팀장)") st.sidebar.text("김영진") st.sidebar.text("최예은") st.sidebar.text("백서윤")
if 'generated' not in st.session_state: st.session_state['generated'] = []
if 'past' not in st.session_state: st.session_state['past'] = []
with st.form('form', clear_on_submit=True): user_input = st.text_input('당신: ', '') submitted = st.form_submit_button('전송')
if submitted and user_input: embedding = model.encode(user_input)
df['distance'] = df['embedding'].map(lambda x: cosine_similarity([embedding], [x]).squeeze()) answer = df.loc[df['distance'].idxmax()] st.session_state.past.append(user_input) st.session_state.generated.append(answer['A'])
for i in range(len(st.session_state['past'])): message(st.session_state['past'][i], is_user=True, key=str(i) + '_user') if len(st.session_state['generated']) > i: message(st.session_state['generated'][i], key=str(i) + '_bot')
|