Spaces:
Sleeping
Sleeping
Initial Draft
Browse files- lib/ui/main.py +15 -0
- lib/ui/sidebar.py +169 -110
lib/ui/main.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
def fn_main_configuration(ui_status_message,sm):
|
| 5 |
+
try:
|
| 6 |
+
if st.session_state.sv_mapping_file_name is not None:
|
| 7 |
+
# Excel to Dataframe
|
| 8 |
+
print(st.session_state.sv_mapping_file_name)
|
| 9 |
+
lv_excel_df = pd.read_excel(st.session_state.sv_mapping_file_name,sheet_name="Sheet1")
|
| 10 |
+
st.data_editor(
|
| 11 |
+
lv_excel_df,
|
| 12 |
+
use_container_width=True
|
| 13 |
+
)
|
| 14 |
+
except Exception as e:
|
| 15 |
+
sm.fn_display_status_messages(f"Error: {e}", "Error", ui_status_message)
|
lib/ui/sidebar.py
CHANGED
|
@@ -1,110 +1,169 @@
|
|
| 1 |
-
import subprocess
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import pandas as pd
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 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|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import os
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
import lib.ui.apiClient.APIClient as APIClient
|
| 7 |
+
|
| 8 |
+
# Function to process the training Excel file
|
| 9 |
+
def fn_process_training_excel(lv_file_name, ui_domain_name, ui_status_message, sm):
|
| 10 |
+
"""
|
| 11 |
+
Process the training Excel file and display status messages based on the response.
|
| 12 |
+
|
| 13 |
+
Parameters:
|
| 14 |
+
- lv_file_name (str): The name of the training Excel file.
|
| 15 |
+
- ui_domain_name (str): The domain name for the vector store.
|
| 16 |
+
- ui_status_message (str): The UI element to display the status message.
|
| 17 |
+
- sm (object): An instance of the StatusMessage class.
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
None
|
| 21 |
+
"""
|
| 22 |
+
try:
|
| 23 |
+
lv_response = APIClient.fn_create_vector_store(lv_file_name, ui_domain_name)
|
| 24 |
+
|
| 25 |
+
if lv_response.status_code == 200:
|
| 26 |
+
# Display a success message
|
| 27 |
+
sm.fn_display_status_messages(lv_response.json()['status'], "Success", ui_status_message)
|
| 28 |
+
else:
|
| 29 |
+
raise Exception(f"Error: {lv_response.json()['error_message']}")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
# Display an error message if there is an exception
|
| 32 |
+
sm.fn_display_status_messages(f"Error: {e}", "Error", ui_status_message)
|
| 33 |
+
|
| 34 |
+
# Function to process the mapping Excel file
|
| 35 |
+
def fn_process_mapping_excel(lv_file_name, ui_source_domain_name, ui_status_message, sm):
|
| 36 |
+
"""
|
| 37 |
+
Process the mapping Excel file and display status messages based on the response.
|
| 38 |
+
|
| 39 |
+
Parameters:
|
| 40 |
+
- lv_file_name (str): The name of the mapping Excel file.
|
| 41 |
+
- ui_source_domain_name (str): The source vector store domain name.
|
| 42 |
+
- ui_status_message (str): The UI element to display the status message.
|
| 43 |
+
- sm (object): An instance of the StatusMessage class.
|
| 44 |
+
|
| 45 |
+
Returns:
|
| 46 |
+
None
|
| 47 |
+
"""
|
| 48 |
+
try:
|
| 49 |
+
lv_response = APIClient.fn_create_data_mapping(lv_file_name, ui_source_domain_name)
|
| 50 |
+
|
| 51 |
+
if lv_response.status_code == 200:
|
| 52 |
+
lv_response_json = lv_response.json()
|
| 53 |
+
lv_response_pd = pd.DataFrame(lv_response_json)
|
| 54 |
+
lv_mapping_file_name = 'db/'+ui_source_domain_name+"_"+lv_file_name.split('/')[-1]
|
| 55 |
+
lv_response_pd.to_excel(lv_mapping_file_name, index=False)
|
| 56 |
+
|
| 57 |
+
st.session_state.sv_mapping_file_name = lv_mapping_file_name
|
| 58 |
+
else:
|
| 59 |
+
raise Exception(f"Error: {lv_response.json()['error_message']}")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
# Display an error message if there is an exception
|
| 62 |
+
sm.fn_display_status_messages(f"Error: {e}", "Error", ui_status_message)
|
| 63 |
+
|
| 64 |
+
# Function to start the Flask server
|
| 65 |
+
@st.cache_resource
|
| 66 |
+
def start_flask_server():
|
| 67 |
+
"""
|
| 68 |
+
Function to start the Flask server.
|
| 69 |
+
|
| 70 |
+
Returns:
|
| 71 |
+
None
|
| 72 |
+
"""
|
| 73 |
+
lv_proc = subprocess.Popen(["python", "flask_app.py"])
|
| 74 |
+
st.session_state.sv_flask_server_proc = lv_proc
|
| 75 |
+
|
| 76 |
+
# Function to stop the Flask server
|
| 77 |
+
def stop_flask_server():
|
| 78 |
+
"""
|
| 79 |
+
Function to stop the Flask server.
|
| 80 |
+
|
| 81 |
+
Returns:
|
| 82 |
+
None
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
lv_proc = st.session_state.sv_flask_server_proc
|
| 86 |
+
if lv_proc is not None:
|
| 87 |
+
lv_proc.terminate()
|
| 88 |
+
lv_proc.wait(timeout=5)
|
| 89 |
+
if lv_proc.poll() is None:
|
| 90 |
+
lv_proc.kill()
|
| 91 |
+
st.session_state.sv_flask_server_proc = None
|
| 92 |
+
|
| 93 |
+
# Function to configure the sidebar UI elements.
|
| 94 |
+
def fn_sidebar_configuration(ui_status_message,sm):
|
| 95 |
+
"""
|
| 96 |
+
Function to configure the sidebar UI elements.
|
| 97 |
+
|
| 98 |
+
Parameters:
|
| 99 |
+
- ui_status_message (str): The status message to be display in UI.
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
None
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
if not(st.session_state.sv_flask_server_proc):
|
| 107 |
+
# Start Flask Server
|
| 108 |
+
start_flask_server()
|
| 109 |
+
|
| 110 |
+
# Toggle button to enable/disable training mode
|
| 111 |
+
ui_training_indicator = st.toggle("Train Domain Data", value=False)
|
| 112 |
+
|
| 113 |
+
# Container to hold the UI elements
|
| 114 |
+
ui_container = st.container(border=True)
|
| 115 |
+
|
| 116 |
+
with ui_container:
|
| 117 |
+
|
| 118 |
+
# Training new domain
|
| 119 |
+
if ui_training_indicator:
|
| 120 |
+
# File uploader for training file
|
| 121 |
+
ui_training_file = st.file_uploader("Training File", type=["xlsx"], accept_multiple_files=False, key="training_file")
|
| 122 |
+
|
| 123 |
+
# Text input for domain name
|
| 124 |
+
ui_domain_name = st.text_input("Domain Name")
|
| 125 |
+
|
| 126 |
+
# Button to submit the form
|
| 127 |
+
if st.button("Submit"):
|
| 128 |
+
|
| 129 |
+
# Check if the training file is uploaded
|
| 130 |
+
if ui_training_file is not None and ui_domain_name != "":
|
| 131 |
+
lv_file_name = "storage/"+ui_domain_name+".xlsx"
|
| 132 |
+
|
| 133 |
+
# Saving File
|
| 134 |
+
with open(lv_file_name, "wb") as f:
|
| 135 |
+
f.write(ui_training_file.getvalue())
|
| 136 |
+
|
| 137 |
+
# Process the Excel file
|
| 138 |
+
fn_process_training_excel(lv_file_name,ui_domain_name,ui_status_message,sm)
|
| 139 |
+
else:
|
| 140 |
+
# Display an error message if the training file is not uploaded
|
| 141 |
+
sm.fn_display_status_messages("Please upload the training file.", "Error", ui_status_message)
|
| 142 |
+
|
| 143 |
+
# Mapping data to trained domain
|
| 144 |
+
else:
|
| 145 |
+
|
| 146 |
+
# Get all file names that end with ".pak" from the "db" folder
|
| 147 |
+
lv_trained_domain_name = [filename[:-len('_index.faiss')] for filename in os.listdir('db') if filename.endswith('_index.faiss')]
|
| 148 |
+
|
| 149 |
+
# Selectbox for domain selection
|
| 150 |
+
ui_training_vector_db = st.selectbox("Domain", lv_trained_domain_name)
|
| 151 |
+
|
| 152 |
+
# Destination Mapping File
|
| 153 |
+
ui_destination_file = st.file_uploader("Destination File", type=["xlsx"], accept_multiple_files=False, key="training_file")
|
| 154 |
+
|
| 155 |
+
# Button to submit the form
|
| 156 |
+
if st.button("Submit"):
|
| 157 |
+
if ui_destination_file is not None:
|
| 158 |
+
lv_file_name = "storage/"+ui_destination_file.name
|
| 159 |
+
|
| 160 |
+
# Saving File
|
| 161 |
+
with open(lv_file_name, "wb") as f:
|
| 162 |
+
f.write(ui_destination_file.getvalue())
|
| 163 |
+
|
| 164 |
+
# Process the Excel file
|
| 165 |
+
fn_process_mapping_excel(lv_file_name,ui_training_vector_db,ui_status_message,sm)
|
| 166 |
+
|
| 167 |
+
except Exception as e:
|
| 168 |
+
# Display an error message if there is an exception
|
| 169 |
+
sm.fn_display_status_messages(f"Error: {e}", "Error", ui_status_message)
|