import pandas as pd import gradio as gr import pymssql import csv from sentence_transformers import SentenceTransformer from sklearn.metrics.pairwise import cosine_similarity #유사도 검색 기능을 사용하기 위함 import glob df = pd.read_csv('./data/real/_UnivSample_20231113.csv') encoder = SentenceTransformer('jhgan/ko-sroberta-multitask') # 선택 모델 (ko가 붙은건 한국어 지원) df['embedding'] = pd.Series([[]] * len(df)) # dummy df['embedding'] = df['query'].map(lambda x: list(encoder.encode(x))) # 챗봇의 답변을 처리하는 함수 def respond(message, chat_history): embedding = encoder.encode(message) df['distance'] = df['embedding'].map(lambda x: cosine_similarity([embedding], [x]).squeeze()) answer = df.loc[df['distance'].idxmax()] chat_history.append([message, answer['answer']]) # historySave(message=message, answer=str(answer['answer']).replace("'","")) # historySave(message=message, answer="") return "", chat_history # def historySave(message, answer): # conn = pymssql.connect(host=r"(local)", database='Chatbot_Manage', charset='utf8') # conn.autocommit(True) # 오토 커밋 활성화 # # Connection 으로부터 Cursor 생성 # cursor = conn.cursor() # SystemType = "SentenceModel" # # SQL문 실행' # _sql = "EXEC ChatHistory_InsUpd '" + SystemType + "','" + message + "', '" + answer + "'" # cursor.execute(_sql) # conn.close() ## 연결 끊기 # 챗봇 설명 title = """
sentence_transformers를 이용한 Chatbot