Spaces:
Sleeping
Sleeping
Arif
commited on
Commit
Β·
9f22029
1
Parent(s):
ca37c17
Updated app.py to v12
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
|
| 4 |
# Page configuration
|
| 5 |
st.set_page_config(
|
|
@@ -48,20 +49,39 @@ tab1, tab2, tab3 = st.tabs(["π€ Upload & Analyze", "π¬ Chat", "π About"])
|
|
| 48 |
with tab1:
|
| 49 |
st.header("π€ Upload and Analyze Data")
|
| 50 |
|
|
|
|
|
|
|
| 51 |
uploaded_file = st.file_uploader(
|
| 52 |
"Upload a CSV or Excel file",
|
| 53 |
type=["csv", "xlsx", "xls"],
|
| 54 |
-
help="Supported formats: CSV, Excel"
|
| 55 |
)
|
| 56 |
|
| 57 |
if uploaded_file is not None:
|
| 58 |
-
st.success(f"β
File uploaded: {uploaded_file.name}")
|
| 59 |
-
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Display data preview
|
| 67 |
st.subheader("π Data Preview")
|
|
@@ -76,10 +96,17 @@ with tab1:
|
|
| 76 |
with col2:
|
| 77 |
st.metric("Columns", df.shape[1])
|
| 78 |
with col3:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Detailed statistics
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Ask AI about the data
|
| 85 |
st.subheader("β Ask AI About Your Data")
|
|
@@ -95,7 +122,8 @@ with tab1:
|
|
| 95 |
st.write(response)
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
-
st.error(f"Error
|
|
|
|
| 99 |
|
| 100 |
# ============================================================================
|
| 101 |
# TAB 2: Chat
|
|
@@ -174,12 +202,16 @@ with tab3:
|
|
| 174 |
- [Streamlit](https://streamlit.io/) - Web framework
|
| 175 |
- [Pandas](https://pandas.pydata.org/) - Data analysis
|
| 176 |
|
| 177 |
-
### π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
-
|
| 180 |
-
-
|
| 181 |
-
-
|
| 182 |
-
- Questions about averages, trends, outliers work best
|
| 183 |
|
| 184 |
### π Links
|
| 185 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
# Page configuration
|
| 6 |
st.set_page_config(
|
|
|
|
| 49 |
with tab1:
|
| 50 |
st.header("π€ Upload and Analyze Data")
|
| 51 |
|
| 52 |
+
st.info("π‘ Tip: For best results, use smaller files (< 10MB). Try CSV format if Excel gives issues.")
|
| 53 |
+
|
| 54 |
uploaded_file = st.file_uploader(
|
| 55 |
"Upload a CSV or Excel file",
|
| 56 |
type=["csv", "xlsx", "xls"],
|
| 57 |
+
help="Supported formats: CSV, Excel (Max 200MB)"
|
| 58 |
)
|
| 59 |
|
| 60 |
if uploaded_file is not None:
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
+
# Check file size
|
| 63 |
+
file_size = uploaded_file.size / (1024 * 1024) # Convert to MB
|
| 64 |
+
|
| 65 |
+
if file_size > 100:
|
| 66 |
+
st.warning(f"β οΈ File is {file_size:.2f}MB. Large files may take longer to process.")
|
| 67 |
+
|
| 68 |
+
st.success(f"β
File uploaded: {uploaded_file.name} ({file_size:.2f}MB)")
|
| 69 |
+
|
| 70 |
+
# Read file with error handling
|
| 71 |
+
try:
|
| 72 |
+
if uploaded_file.name.endswith('.csv'):
|
| 73 |
+
df = pd.read_csv(uploaded_file, on_bad_lines='skip')
|
| 74 |
+
else:
|
| 75 |
+
df = pd.read_excel(uploaded_file, engine='openpyxl')
|
| 76 |
+
except Exception as read_error:
|
| 77 |
+
st.error(f"β Error reading file. Try converting to CSV first.")
|
| 78 |
+
st.info(f"Details: {str(read_error)[:100]}")
|
| 79 |
+
st.stop()
|
| 80 |
+
|
| 81 |
+
# Validate dataframe
|
| 82 |
+
if df.empty:
|
| 83 |
+
st.error("β File is empty or has no readable data.")
|
| 84 |
+
st.stop()
|
| 85 |
|
| 86 |
# Display data preview
|
| 87 |
st.subheader("π Data Preview")
|
|
|
|
| 96 |
with col2:
|
| 97 |
st.metric("Columns", df.shape[1])
|
| 98 |
with col3:
|
| 99 |
+
try:
|
| 100 |
+
memory = df.memory_usage(deep=True).sum() / 1024
|
| 101 |
+
st.metric("Memory", f"{memory:.2f} KB")
|
| 102 |
+
except:
|
| 103 |
+
st.metric("Memory", "N/A")
|
| 104 |
|
| 105 |
# Detailed statistics
|
| 106 |
+
try:
|
| 107 |
+
st.write(df.describe().T)
|
| 108 |
+
except:
|
| 109 |
+
st.info("Could not generate statistics for this data.")
|
| 110 |
|
| 111 |
# Ask AI about the data
|
| 112 |
st.subheader("β Ask AI About Your Data")
|
|
|
|
| 122 |
st.write(response)
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
+
st.error(f"β Error processing file: {str(e)[:100]}")
|
| 126 |
+
st.info("Try uploading a smaller file or converting to CSV format.")
|
| 127 |
|
| 128 |
# ============================================================================
|
| 129 |
# TAB 2: Chat
|
|
|
|
| 202 |
- [Streamlit](https://streamlit.io/) - Web framework
|
| 203 |
- [Pandas](https://pandas.pydata.org/) - Data analysis
|
| 204 |
|
| 205 |
+
### π Troubleshooting
|
| 206 |
+
|
| 207 |
+
**File upload fails?**
|
| 208 |
+
- Try converting Excel to CSV first
|
| 209 |
+
- Use smaller files (< 10MB)
|
| 210 |
+
- Check file format is valid
|
| 211 |
|
| 212 |
+
**Data preview is empty?**
|
| 213 |
+
- File may be corrupted
|
| 214 |
+
- Try opening in Excel and resaving as CSV
|
|
|
|
| 215 |
|
| 216 |
### π Links
|
| 217 |
|