Spaces:
Sleeping
Sleeping
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) | |