File size: 3,685 Bytes
84ac28f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import subprocess
import time
import streamlit as st
import pandas as pd


def fn_process_excel(lv_file_name,ui_status_message,sm):
    try:
        # Read the Excel file
        excel_data = pd.read_excel(lv_file_name,sheet_name=None)

        # Iterate over each sheet in the Excel file
        for sheet_name, sheet_data in excel_data.items():
            # Process the data in each sheet
            print(f"Sheet Name: {sheet_name}")
            print(sheet_data.head())

        # Display a success message
        sm.fn_display_status_messages("Excel data processed successfully!", "Success", ui_status_message)

    except Exception as e:
        # Display an error message if there is an exception
        sm.fn_display_status_messages(f"Error: {e}", "Error", ui_status_message)    

# Function to start the Flask server
def start_flask_server():
    """
    Function to start the Flask server.

    Returns:
    None
    """
    lv_proc = subprocess.Popen(["python", "flask_app.py"])
    st.session_state.sv_flask_server_proc = lv_proc

# Function to stop the Flask server
def stop_flask_server():
    """
    Function to stop the Flask server.

    Returns:
    None
    """

    lv_proc = st.session_state.sv_flask_server_proc
    if lv_proc is not None:
        lv_proc.terminate()
        lv_proc.wait(timeout=5)
        if lv_proc.poll() is None:
            lv_proc.kill()
        st.session_state.sv_flask_server_proc = None

# Function to configure the sidebar UI elements.
def fn_sidebar_configuration(ui_status_message,sm):
    """
    Function to configure the sidebar UI elements.

    Parameters:
    - ui_status_message (str): The status message to be display in UI.

    Returns:
    None
    """

    try:
        if not(st.session_state.sv_flask_server_proc):
            # Start Flask Server
            start_flask_server()

        # Toggle button to enable/disable training mode
        ui_training_indicator = st.toggle("Training", value=False)

        # Container to hold the UI elements
        ui_container = st.container(border=True)

        with ui_container:

            # Training new domain
            if ui_training_indicator:
                # File uploader for training file
                ui_training_file = st.file_uploader("Training File", type=["xlsx"], accept_multiple_files=False, key="training_file")

                # Text input for domain name
                ui_domain_name = st.text_input("Domain Name")

                # Button to submit the form
                if st.button("Submit"):
                    
                    # Check if the training file is uploaded
                    if ui_training_file is not None and ui_domain_name != "":
                        lv_file_name = "storage/"+ui_domain_name+".xlsx"

                        # Saving File
                        with open(lv_file_name, "wb") as f:
                            f.write(ui_training_file.getvalue())

                        # Process the Excel file
                        fn_process_excel(lv_file_name, ui_status_message,sm)
                    else:
                        # Display an error message if the training file is not uploaded
                        sm.fn_display_status_messages("Please upload the training file.", "Error", ui_status_message)
            
            # Mapping data to trained domain
            else:
                # Selectbox for domain selection
                ui_training_vector_db = st.selectbox("Domain", ["OFSLL", "OBRL"])
    
    except Exception as e:
        # Display an error message if there is an exception
        sm.fn_display_status_messages(f"Error: {e}", "Error", ui_status_message)