File size: 3,521 Bytes
fae86c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from utils.sql_utils import create_schema

MAX_FILES = 5

if "uploaded_dataframes" not in st.session_state:
    st.session_state.uploaded_dataframes = {}

st.header("Upload your CSV or Excel files")
st.caption("Maximum 5 files can be uploaded.")

num_uploaded_files = len(st.session_state.uploaded_dataframes)

disabled = num_uploaded_files >= MAX_FILES

uploaded_files = st.file_uploader(
    "Choose files",
    type=["csv", "xlsx"],
    accept_multiple_files=True,
    disabled=disabled,
)

if uploaded_files and not disabled:
    uploaded_count = 0
    for uploaded_file in uploaded_files:
        if len(st.session_state.uploaded_dataframes) < MAX_FILES:
            df = None
            try:
                if uploaded_file.name.endswith(".csv"):
                    df = pd.read_csv(uploaded_file)
                elif uploaded_file.name.endswith(".xlsx"):
                    df = pd.read_excel(uploaded_file)

                if uploaded_file.name in st.session_state.uploaded_dataframes:
                    st.toast(
                        f"File {uploaded_file.name} already uploaded. Skipping...",
                        icon="⚠️",
                    )
                else:
                    key = (
                        uploaded_file.name.lower()
                        .strip()
                        .replace(" ", "_")
                        .replace(".csv", "")
                        .replace(".xlsx", "")
                    )
                    st.session_state.uploaded_dataframes[key] = df
                    uploaded_count += 1
                print(f"Uploaded file: {str(uploaded_file.name)}")
            except Exception as e:
                st.error(f"Error reading file {uploaded_file.name}: {e}")
        else:
            st.warning(
                f"Maximum number of files({MAX_FILES}) reached. Cannot upload more files."
            )
            break
    if uploaded_count > 0:
        st.success(
            f"{uploaded_count} File(s) uploaded successfully. Total {len(st.session_state.uploaded_dataframes)} File(s)"
        )


if len(st.session_state.uploaded_dataframes) >= MAX_FILES:
    st.warning(
        f"Maximum number of files({MAX_FILES}) reached. Cannot upload more files."
    )


dataframes = st.session_state.uploaded_dataframes

if dataframes:
    st.header("Schema of Uploaded Files📚")
    schema = create_schema(dataframes)
    if "uploaded_df_schema" not in st.session_state:
        st.session_state.uploaded_df_schema = schema
    pretty_schema, markdown = st.tabs(["Schema", "Copy Schema in Markdown"])
    with pretty_schema:
        st.info(
            "You can copy this schema, and give it to any state of the art LLM models like (Gemini /ChatGPT /Claude etc) to cross check your answers.\n You can run the queries directly here, by using ***Manual Query Executer*** in the sidebar and download your results 😊",
            icon="ℹ️",
        )
        st.markdown(schema, unsafe_allow_html=True)
    with markdown:
        st.info(
            "You can copy this schema, and give it to any state of the art LLM models like (Gemini /ChatGPT /Claude etc) to cross check your answers.\n You can run the queries directly here, by using ***Manual Query Executer*** in the sidebar and download your results 😊",
            icon="ℹ️",
        )
        st.markdown(f"```\n{schema}\n```")