Update BrainBot.py
Browse files- BrainBot.py +22 -23
BrainBot.py
CHANGED
|
@@ -14,19 +14,18 @@ st.markdown(f'<style>{css}</style>', unsafe_allow_html=True)
|
|
| 14 |
## -------------------------------------------------------------------------------------------
|
| 15 |
# Function to save the uploaded file as a temporary file and return its path.
|
| 16 |
def save_uploaded_file(uploaded_file):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
# data_dir =
|
| 22 |
-
# # os.makedirs(data_dir, exist_ok=True)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
# Function to save the uploaded image as a temporary file and return its path.
|
| 32 |
def save_uploaded_image(uploaded_image):
|
|
@@ -130,17 +129,17 @@ if uploaded_file is not None:
|
|
| 130 |
temp_file_path = save_uploaded_file(uploaded_file)
|
| 131 |
st.write(temp_file_path)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
## IMAGE
|
| 146 |
## -------------------------------------------------------------------------------------------
|
|
|
|
| 14 |
## -------------------------------------------------------------------------------------------
|
| 15 |
# Function to save the uploaded file as a temporary file and return its path.
|
| 16 |
def save_uploaded_file(uploaded_file):
|
| 17 |
+
file_content = uploaded_file.read() # Load the document
|
| 18 |
+
st.write("read file")
|
| 19 |
+
# Create a directory if it doesn't exist
|
| 20 |
+
data_dir = "/data"
|
| 21 |
+
# os.makedirs(data_dir, exist_ok=True)
|
|
|
|
| 22 |
|
| 23 |
+
# Create a temporary file in the data directory
|
| 24 |
+
with tempfile.NamedTemporaryFile(delete=False, dir=data_dir) as temp_file:
|
| 25 |
+
st.write("writing file...")
|
| 26 |
+
temp_file.write(file_content) # Write the uploaded file content to the temporary file
|
| 27 |
+
temp_file_path = temp_file.name # Get the path of the temporary file
|
| 28 |
+
return temp_file_path
|
| 29 |
|
| 30 |
# Function to save the uploaded image as a temporary file and return its path.
|
| 31 |
def save_uploaded_image(uploaded_image):
|
|
|
|
| 129 |
temp_file_path = save_uploaded_file(uploaded_file)
|
| 130 |
st.write(temp_file_path)
|
| 131 |
|
| 132 |
+
try:
|
| 133 |
+
# Send POST request to a FastAPI endpoint to load the file into a vectorstore
|
| 134 |
+
data = {"file_path": temp_file_path, "file_type": uploaded_file.type}
|
| 135 |
+
FASTAPI_URL = f"http://localhost:7860/load_file/{llm}"
|
| 136 |
+
response = requests.post(FASTAPI_URL, json=data)
|
| 137 |
+
st.success(response.text)
|
| 138 |
+
st.session_state['current_file'] = uploaded_file.name
|
| 139 |
+
st.session_state['uploaded_file'] = True
|
| 140 |
+
st.switch_page("pages/File-chat.py")
|
| 141 |
+
except Exception as e:
|
| 142 |
+
st.switch_page("pages/error.py")
|
| 143 |
|
| 144 |
## IMAGE
|
| 145 |
## -------------------------------------------------------------------------------------------
|