Arif commited on
Commit
ca8b7a3
Β·
1 Parent(s): 9f22029

Updated app.py to v13

Browse files
Files changed (1) hide show
  1. app.py +50 -35
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: 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
@@ -92,19 +105,20 @@ with tab1:
92
  col1, col2, col3 = st.columns(3)
93
 
94
  with col1:
95
- st.metric("Rows", df.shape[0])
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
 
@@ -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 value in column X?",
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"❌ 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
@@ -204,14 +218,15 @@ with tab3:
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
 
@@ -220,7 +235,7 @@ with tab3:
220
 
221
  ---
222
 
223
- **Version:** 1.0 | **Last Updated:** Dec 2025
224
 
225
- πŸ’‘ **Note:** This version uses intelligent pattern matching for responses. For more advanced AI features, you can integrate your own Hugging Face API token.
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
  """)