Spaces:
Sleeping
Sleeping
Arif
commited on
Commit
Β·
ca8b7a3
1
Parent(s):
9f22029
Updated app.py to v13
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# Page configuration
|
|
@@ -49,38 +50,50 @@ tab1, tab2, tab3 = st.tabs(["π€ Upload & Analyze", "π¬ Chat", "π About"])
|
|
| 49 |
with tab1:
|
| 50 |
st.header("π€ Upload and Analyze Data")
|
| 51 |
|
| 52 |
-
st.info("π‘ Tip:
|
| 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
|
| 58 |
)
|
| 59 |
|
| 60 |
if uploaded_file is not None:
|
| 61 |
try:
|
| 62 |
-
|
| 63 |
-
file_size = uploaded_file.size / (1024 * 1024) # Convert to MB
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# Read file with error handling
|
| 71 |
try:
|
| 72 |
-
if uploaded_file.name.endswith('.csv'):
|
| 73 |
-
df = pd.read_csv(
|
| 74 |
else:
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as read_error:
|
| 77 |
-
st.error(
|
| 78 |
-
st.info(
|
| 79 |
st.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Validate dataframe
|
| 82 |
if df.empty:
|
| 83 |
-
st.error("β File is empty
|
| 84 |
st.stop()
|
| 85 |
|
| 86 |
# Display data preview
|
|
@@ -92,19 +105,20 @@ with tab1:
|
|
| 92 |
col1, col2, col3 = st.columns(3)
|
| 93 |
|
| 94 |
with col1:
|
| 95 |
-
st.metric("Rows", df
|
| 96 |
with col2:
|
| 97 |
-
st.metric("Columns", df.
|
| 98 |
with col3:
|
| 99 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
except:
|
| 109 |
st.info("Could not generate statistics for this data.")
|
| 110 |
|
|
@@ -112,7 +126,7 @@ with tab1:
|
|
| 112 |
st.subheader("β Ask AI About Your Data")
|
| 113 |
question = st.text_input(
|
| 114 |
"What would you like to know about this data?",
|
| 115 |
-
placeholder="e.g., What is the average
|
| 116 |
key="data_question"
|
| 117 |
)
|
| 118 |
|
|
@@ -122,8 +136,8 @@ with tab1:
|
|
| 122 |
st.write(response)
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
-
st.error(f"β
|
| 126 |
-
st.info("Try
|
| 127 |
|
| 128 |
# ============================================================================
|
| 129 |
# TAB 2: Chat
|
|
@@ -204,14 +218,15 @@ with tab3:
|
|
| 204 |
|
| 205 |
### π Troubleshooting
|
| 206 |
|
| 207 |
-
**File upload fails?**
|
| 208 |
-
-
|
| 209 |
-
-
|
| 210 |
-
-
|
| 211 |
|
| 212 |
-
**
|
| 213 |
-
-
|
| 214 |
-
-
|
|
|
|
| 215 |
|
| 216 |
### π Links
|
| 217 |
|
|
@@ -220,7 +235,7 @@ with tab3:
|
|
| 220 |
|
| 221 |
---
|
| 222 |
|
| 223 |
-
**Version:** 1.
|
| 224 |
|
| 225 |
-
π‘ **Note:** This version uses intelligent pattern matching for responses.
|
| 226 |
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import tempfile
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Page configuration
|
|
|
|
| 50 |
with tab1:
|
| 51 |
st.header("π€ Upload and Analyze Data")
|
| 52 |
|
| 53 |
+
st.info("π‘ Tip: CSV files work best. If upload fails, try saving your Excel file as CSV first.")
|
| 54 |
|
| 55 |
uploaded_file = st.file_uploader(
|
| 56 |
"Upload a CSV or Excel file",
|
| 57 |
type=["csv", "xlsx", "xls"],
|
| 58 |
+
help="Supported formats: CSV, Excel"
|
| 59 |
)
|
| 60 |
|
| 61 |
if uploaded_file is not None:
|
| 62 |
try:
|
| 63 |
+
st.success(f"β
File received: {uploaded_file.name}")
|
|
|
|
| 64 |
|
| 65 |
+
# Save to temp file to avoid streaming issues
|
| 66 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file:
|
| 67 |
+
tmp_file.write(uploaded_file.getbuffer())
|
| 68 |
+
tmp_path = tmp_file.name
|
| 69 |
|
| 70 |
+
# Read file
|
|
|
|
|
|
|
| 71 |
try:
|
| 72 |
+
if uploaded_file.name.lower().endswith('.csv'):
|
| 73 |
+
df = pd.read_csv(tmp_path, on_bad_lines='skip')
|
| 74 |
else:
|
| 75 |
+
# Try multiple engines for Excel
|
| 76 |
+
try:
|
| 77 |
+
df = pd.read_excel(tmp_path, engine='openpyxl')
|
| 78 |
+
except:
|
| 79 |
+
try:
|
| 80 |
+
df = pd.read_excel(tmp_path, engine='xlrd')
|
| 81 |
+
except:
|
| 82 |
+
df = pd.read_excel(tmp_path)
|
| 83 |
except Exception as read_error:
|
| 84 |
+
st.error("β Could not read file. Try converting to CSV format.")
|
| 85 |
+
st.info("**Solution:** Open in Excel β File β Save As β CSV β Upload again")
|
| 86 |
st.stop()
|
| 87 |
+
finally:
|
| 88 |
+
# Clean up temp file
|
| 89 |
+
try:
|
| 90 |
+
os.unlink(tmp_path)
|
| 91 |
+
except:
|
| 92 |
+
pass
|
| 93 |
|
| 94 |
# Validate dataframe
|
| 95 |
if df.empty:
|
| 96 |
+
st.error("β File is empty. Make sure it contains data rows.")
|
| 97 |
st.stop()
|
| 98 |
|
| 99 |
# Display data preview
|
|
|
|
| 105 |
col1, col2, col3 = st.columns(3)
|
| 106 |
|
| 107 |
with col1:
|
| 108 |
+
st.metric("Rows", len(df))
|
| 109 |
with col2:
|
| 110 |
+
st.metric("Columns", len(df.columns))
|
| 111 |
with col3:
|
| 112 |
+
st.metric("Columns", ", ".join(df.columns[:3].tolist()) + "...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# Detailed statistics
|
| 115 |
try:
|
| 116 |
+
numeric_df = df.select_dtypes(include=['number'])
|
| 117 |
+
if not numeric_df.empty:
|
| 118 |
+
st.write("### Numeric Columns Summary")
|
| 119 |
+
st.write(numeric_df.describe().T)
|
| 120 |
+
else:
|
| 121 |
+
st.info("No numeric columns found in dataset.")
|
| 122 |
except:
|
| 123 |
st.info("Could not generate statistics for this data.")
|
| 124 |
|
|
|
|
| 126 |
st.subheader("β Ask AI About Your Data")
|
| 127 |
question = st.text_input(
|
| 128 |
"What would you like to know about this data?",
|
| 129 |
+
placeholder="e.g., What is the average? What patterns do you see?",
|
| 130 |
key="data_question"
|
| 131 |
)
|
| 132 |
|
|
|
|
| 136 |
st.write(response)
|
| 137 |
|
| 138 |
except Exception as e:
|
| 139 |
+
st.error(f"β Unexpected error: {str(e)[:50]}")
|
| 140 |
+
st.info("**Try this:** Save your Excel file as CSV, then upload again.")
|
| 141 |
|
| 142 |
# ============================================================================
|
| 143 |
# TAB 2: Chat
|
|
|
|
| 218 |
|
| 219 |
### π Troubleshooting
|
| 220 |
|
| 221 |
+
**File upload fails with 403 error?**
|
| 222 |
+
- Convert Excel to CSV first (File β Save As β CSV format)
|
| 223 |
+
- Upload the CSV file instead
|
| 224 |
+
- This solves 99% of upload issues
|
| 225 |
|
| 226 |
+
**Still having issues?**
|
| 227 |
+
- Make sure file has valid data
|
| 228 |
+
- File size should be under 50MB
|
| 229 |
+
- Try a simpler file first to test
|
| 230 |
|
| 231 |
### π Links
|
| 232 |
|
|
|
|
| 235 |
|
| 236 |
---
|
| 237 |
|
| 238 |
+
**Version:** 1.1 | **Last Updated:** Dec 2025
|
| 239 |
|
| 240 |
+
π‘ **Note:** This version uses intelligent pattern matching for responses.
|
| 241 |
""")
|