import torch from sentence_transformers import SentenceTransformer, util import pandas as pd import torch import numpy as np from tqdm import tqdm import os import sys import gradio as gr tqdm.pandas() # 상대경로를 절대경로로 바꾸기 def resource_path(relative_path): try: base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) model = SentenceTransformer("Huffon/sentence-klue-roberta-base") Chatbot_Data = pd.read_csv(resource_path("chatbotData_pro_Level_4.csv")) EmbData = np.load(resource_path('emb_file_pro_level_4.npy')) def answer(state, state_chatbot, text): messages = state + [{ 'role': 'user', 'content': text }] #res = chatbot_response(messages) q = model.encode(text) # 질문을 Tensor로 바꿉니다. q = torch.tensor(q) # 코사인 유사도 cos_sim = util.pytorch_cos_sim(q, EmbData) # 유사도가 가장 비슷한 질문 인덱스를 구합니다. best_sim_idx = int(np.argmax(cos_sim)) # 질문의 유사도와 가장 비슷한 답변 제공 answer = Chatbot_Data['A'][best_sim_idx] print(answer) msg = answer #['choices'][0]['message']['content'] new_state = [{ 'role': 'user', 'content': text }, { 'role': 'assistant', 'content': msg }] state = state + new_state state_chatbot = state_chatbot + [(text, msg)] #print(state) return state, state_chatbot, state_chatbot with gr.Blocks(css='#chatbot .overflow-y-auto{height:750px}') as demo: state = gr.State([{ 'role': 'system', 'content': 'You are a helpful assistant.' }]) state_chatbot = gr.State([]) with gr.Row(): gr.HTML("""

GIS Environment System Lab. Bot

""") with gr.Row(): chatbot = gr.Chatbot(elem_id='chatbot') with gr.Row(): txt = gr.Textbox(show_label=False, placeholder='Send a message...').style(container=False) txt.submit(answer, [state, state_chatbot, txt], [state, state_chatbot, chatbot]) txt.submit(lambda: '', None, txt) demo.launch(debug=True, share=True)