RustX commited on
Commit
19ef06b
Β·
1 Parent(s): 04bf297

Create utils.py

Browse files
Files changed (1) hide show
  1. modules/utils.py +62 -0
modules/utils.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import streamlit as st
4
+
5
+ from modules.chatbot import Chatbot
6
+ from modules.embedder import Embedder
7
+
8
+
9
+ class Utilities:
10
+ @staticmethod
11
+ def load_api_key():
12
+ """
13
+ Loads the OpenAI API key from the .env file or from the user's input
14
+ and returns it
15
+ """
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_key
26
+
27
+ @staticmethod
28
+ def handle_upload():
29
+ """
30
+ Handles the file upload and displays the uploaded file
31
+ """
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"] = True
48
+ return uploaded_file
49
+
50
+ @staticmethod
51
+ def setup_chatbot(uploaded_file, model, temperature):
52
+ """
53
+ Sets up the chatbot with the uploaded file, model, and temperature
54
+ """
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"] = True
62
+ return chatbot