CSV-ChatBot / modules /utils.py
RustX's picture
Create utils.py
19ef06b
Raw
History Blame Contribute Delete
2.43 kB
import os
import pandas as pd
import streamlit as st
from modules.chatbot import Chatbot
from modules.embedder import Embedder
class Utilities:
@staticmethod
def load_api_key():
"""
Loads the OpenAI API key from the .env file or from the user's input
and returns it
"""
if os.path.exists(".env") and os.environ.get("OPENAI_API_KEY") is not None:
user_api_key = os.environ["OPENAI_API_KEY"]
st.sidebar.success("API key loaded from .env / .envμ—μ„œ λ‘œλ“œλœ API ν‚€", icon="πŸš€")
else:
user_api_key = st.sidebar.text_input(
label="#### Your OpenAI API key / OpenAI API ν‚€ πŸ‘‡", placeholder="Paste your openAI API key, sk-", type="password"
)
if user_api_key:
st.sidebar.success("API key loaded / API ν‚€κ°€ λ‘œλ“œλ˜μ—ˆμŠ΅λ‹ˆλ‹€", icon="πŸš€")
return user_api_key
@staticmethod
def handle_upload():
"""
Handles the file upload and displays the uploaded file
"""
uploaded_file = st.sidebar.file_uploader("upload", type="csv", label_visibility="collapsed")
if uploaded_file is not None:
def show_user_file(uploaded_file):
file_container = st.expander("Your CSV file : / CSV 파일:")
shows = pd.read_csv(uploaded_file)
uploaded_file.seek(0)
file_container.write(shows)
show_user_file(uploaded_file)
else:
st.sidebar.info(
"πŸ‘† Upload your CSV file to get started, / μ‹œμž‘ν•˜λ €λ©΄ CSV νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ„Έμš” "
"sample for try : / μ‹œλ„ν•΄ λ³Ό μƒ˜ν”Œ: [example.csv](https://drive.google.com/file/d/1g7x0Ydg5kr51Ha2XIYBSQBVUw1yYlgmc/view?usp=share_link)"
)
st.session_state["reset_chat"] = True
return uploaded_file
@staticmethod
def setup_chatbot(uploaded_file, model, temperature):
"""
Sets up the chatbot with the uploaded file, model, and temperature
"""
embeds = Embedder()
with st.spinner("Processing... / 처리 쀑..."):
uploaded_file.seek(0)
file = uploaded_file.read()
vectors = embeds.getDocEmbeds(file, uploaded_file.name)
chatbot = Chatbot(model, temperature, vectors)
st.session_state["ready"] = True
return chatbot