RustX/CSV-ChatBot
1
1import os2import pandas as pd3import streamlit as st4 5from modules.chatbot import Chatbot6from modules.embedder import Embedder7 8 9class Utilities:10 @staticmethod11 def load_api_key():12 """13 Loads the OpenAI API key from the .env file or from the user's input14 and returns it15 """16 if os.path.exists(".env") and os.environ.get("OPENAI_API_KEY") is not None:17 user_api_key = os.environ["OPENAI_API_KEY"]18 st.sidebar.success("API key loaded from .env / .env에서 로드된 API 키", icon="🚀")19 else:20 user_api_key = st.sidebar.text_input(21 label="#### Your OpenAI API key / OpenAI API 키 👇", placeholder="Paste your openAI API key, sk-", type="password"22 )23 if user_api_key:24 st.sidebar.success("API key loaded / API 키가 로드되었습니다", icon="🚀")25 return user_api_key26 27 @staticmethod28 def handle_upload():29 """30 Handles the file upload and displays the uploaded file31 """32 uploaded_file = st.sidebar.file_uploader("upload", type="csv", label_visibility="collapsed")33 if uploaded_file is not None:34 35 def show_user_file(uploaded_file):36 file_container = st.expander("Your CSV file : / CSV 파일:")37 shows = pd.read_csv(uploaded_file)38 uploaded_file.seek(0)39 file_container.write(shows)40 41 show_user_file(uploaded_file)42 else:43 st.sidebar.info(44 "👆 Upload your CSV file to get started, / 시작하려면 CSV 파일을 업로드하세요 "45 "sample for try : / 시도해 볼 샘플: [example.csv](https://drive.google.com/file/d/1g7x0Ydg5kr51Ha2XIYBSQBVUw1yYlgmc/view?usp=share_link)"46 )47 st.session_state["reset_chat"] = True48 return uploaded_file49 50 @staticmethod51 def setup_chatbot(uploaded_file, model, temperature):52 """53 Sets up the chatbot with the uploaded file, model, and temperature54 """55 embeds = Embedder()56 with st.spinner("Processing... / 처리 중..."):57 uploaded_file.seek(0)58 file = uploaded_file.read()59 vectors = embeds.getDocEmbeds(file, uploaded_file.name)60 chatbot = Chatbot(model, temperature, vectors)61 st.session_state["ready"] = True62 return chatbot