Spaces:
Runtime error
Runtime error
| import os | |
| import pandas as pd | |
| import streamlit as st | |
| from modules.chatbot import Chatbot | |
| from modules.embedder import Embedder | |
| class Utilities: | |
| 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 | |
| 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 | |
| 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 |