rahgadda commited on
Commit
e001c27
1 Parent(s): ce0579f

Initial Draft

Browse files
Files changed (2) hide show
  1. lib/ui/main.py +15 -0
  2. 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 time
3
- import streamlit as st
4
- import pandas as pd
5
-
6
-
7
- def fn_process_excel(lv_file_name,ui_status_message,sm):
8
- try:
9
- # Read the Excel file
10
- excel_data = pd.read_excel(lv_file_name,sheet_name=None)
11
-
12
- # Iterate over each sheet in the Excel file
13
- for sheet_name, sheet_data in excel_data.items():
14
- # Process the data in each sheet
15
- print(f"Sheet Name: {sheet_name}")
16
- print(sheet_data.head())
17
-
18
- # Display a success message
19
- sm.fn_display_status_messages("Excel data processed successfully!", "Success", ui_status_message)
20
-
21
- except Exception as e:
22
- # Display an error message if there is an exception
23
- sm.fn_display_status_messages(f"Error: {e}", "Error", ui_status_message)
24
-
25
- # Function to start the Flask server
26
- def start_flask_server():
27
- """
28
- Function to start the Flask server.
29
-
30
- Returns:
31
- None
32
- """
33
- lv_proc = subprocess.Popen(["python", "flask_app.py"])
34
- st.session_state.sv_flask_server_proc = lv_proc
35
-
36
- # Function to stop the Flask server
37
- def stop_flask_server():
38
- """
39
- Function to stop the Flask server.
40
-
41
- Returns:
42
- None
43
- """
44
-
45
- lv_proc = st.session_state.sv_flask_server_proc
46
- if lv_proc is not None:
47
- lv_proc.terminate()
48
- lv_proc.wait(timeout=5)
49
- if lv_proc.poll() is None:
50
- lv_proc.kill()
51
- st.session_state.sv_flask_server_proc = None
52
-
53
- # Function to configure the sidebar UI elements.
54
- def fn_sidebar_configuration(ui_status_message,sm):
55
- """
56
- Function to configure the sidebar UI elements.
57
-
58
- Parameters:
59
- - ui_status_message (str): The status message to be display in UI.
60
-
61
- Returns:
62
- None
63
- """
64
-
65
- try:
66
- if not(st.session_state.sv_flask_server_proc):
67
- # Start Flask Server
68
- start_flask_server()
69
-
70
- # Toggle button to enable/disable training mode
71
- ui_training_indicator = st.toggle("Training", value=False)
72
-
73
- # Container to hold the UI elements
74
- ui_container = st.container(border=True)
75
-
76
- with ui_container:
77
-
78
- # Training new domain
79
- if ui_training_indicator:
80
- # File uploader for training file
81
- ui_training_file = st.file_uploader("Training File", type=["xlsx"], accept_multiple_files=False, key="training_file")
82
-
83
- # Text input for domain name
84
- ui_domain_name = st.text_input("Domain Name")
85
-
86
- # Button to submit the form
87
- if st.button("Submit"):
88
-
89
- # Check if the training file is uploaded
90
- if ui_training_file is not None and ui_domain_name != "":
91
- lv_file_name = "storage/"+ui_domain_name+".xlsx"
92
-
93
- # Saving File
94
- with open(lv_file_name, "wb") as f:
95
- f.write(ui_training_file.getvalue())
96
-
97
- # Process the Excel file
98
- fn_process_excel(lv_file_name, ui_status_message,sm)
99
- else:
100
- # Display an error message if the training file is not uploaded
101
- sm.fn_display_status_messages("Please upload the training file.", "Error", ui_status_message)
102
-
103
- # Mapping data to trained domain
104
- else:
105
- # Selectbox for domain selection
106
- ui_training_vector_db = st.selectbox("Domain", ["OFSLL", "OBRL"])
107
-
108
- except Exception as e:
109
- # Display an error message if there is an exception
110
- sm.fn_display_status_messages(f"Error: {e}", "Error", ui_status_message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)