Spaces:
Sleeping
Sleeping
Arif
commited on
Commit
Β·
94b7bfa
1
Parent(s):
ca8b7a3
Updated app.py to version 12
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
# Page configuration
|
| 7 |
st.set_page_config(
|
|
@@ -30,75 +29,64 @@ def get_ai_response(prompt):
|
|
| 30 |
return "Check the min/max values and compare them to the mean and median. Large differences suggest outliers in your data."
|
| 31 |
elif "summary" in prompt_lower or "overview" in prompt_lower:
|
| 32 |
return "The data summary shows key statistics including count, mean, standard deviation, min, 25%, 50%, 75%, and max values for each column."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# General chat responses
|
| 35 |
elif "hello" in prompt_lower or "hi" in prompt_lower:
|
| 36 |
-
return "Hello! I'm the LLM Data Analyzer. I can help you understand your data better.
|
| 37 |
elif "what can you do" in prompt_lower or "help" in prompt_lower:
|
| 38 |
-
return "I can help you: 1)
|
| 39 |
elif "thank" in prompt_lower:
|
| 40 |
return "You're welcome! Feel free to ask more questions about your data anytime."
|
| 41 |
else:
|
| 42 |
-
return "That's an interesting question! To get the most accurate analysis, please
|
| 43 |
|
| 44 |
# Create tabs
|
| 45 |
-
tab1, tab2, tab3 = st.tabs(["π€
|
| 46 |
|
| 47 |
# ============================================================================
|
| 48 |
-
# TAB 1:
|
| 49 |
# ============================================================================
|
| 50 |
with tab1:
|
| 51 |
-
st.header("π€
|
| 52 |
-
|
| 53 |
-
st.info("π‘
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
-
if
|
| 62 |
try:
|
| 63 |
-
|
| 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 |
-
|
| 95 |
-
if df.empty:
|
| 96 |
-
st.error("β File is empty. Make sure it contains data rows.")
|
| 97 |
-
st.stop()
|
| 98 |
|
| 99 |
# Display data preview
|
| 100 |
st.subheader("π Data Preview")
|
| 101 |
-
st.dataframe(df
|
| 102 |
|
| 103 |
# Display statistics
|
| 104 |
st.subheader("π Data Statistics")
|
|
@@ -109,7 +97,7 @@ with tab1:
|
|
| 109 |
with col2:
|
| 110 |
st.metric("Columns", len(df.columns))
|
| 111 |
with col3:
|
| 112 |
-
st.metric("
|
| 113 |
|
| 114 |
# Detailed statistics
|
| 115 |
try:
|
|
@@ -126,7 +114,7 @@ with tab1:
|
|
| 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,8 +124,8 @@ with tab1:
|
|
| 136 |
st.write(response)
|
| 137 |
|
| 138 |
except Exception as e:
|
| 139 |
-
st.error(f"β
|
| 140 |
-
st.info("
|
| 141 |
|
| 142 |
# ============================================================================
|
| 143 |
# TAB 2: Chat
|
|
@@ -199,16 +187,27 @@ with tab3:
|
|
| 199 |
|
| 200 |
### β‘ Features
|
| 201 |
|
| 202 |
-
1. **Data Analysis**:
|
| 203 |
2. **Chat**: Have conversations about data insights
|
| 204 |
3. **Statistics**: View comprehensive data summaries
|
|
|
|
| 205 |
|
| 206 |
### π How to Use
|
| 207 |
|
| 208 |
-
1. **
|
| 209 |
-
2. **
|
| 210 |
-
3. **
|
| 211 |
-
4. **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
### π Powered By
|
| 214 |
|
|
@@ -216,17 +215,16 @@ with tab3:
|
|
| 216 |
- [Streamlit](https://streamlit.io/) - Web framework
|
| 217 |
- [Pandas](https://pandas.pydata.org/) - Data analysis
|
| 218 |
|
| 219 |
-
###
|
| 220 |
|
| 221 |
-
**
|
| 222 |
-
-
|
| 223 |
-
-
|
| 224 |
-
- This solves 99% of upload issues
|
| 225 |
|
| 226 |
-
**
|
| 227 |
-
-
|
| 228 |
-
-
|
| 229 |
-
-
|
| 230 |
|
| 231 |
### π Links
|
| 232 |
|
|
@@ -235,7 +233,7 @@ with tab3:
|
|
| 235 |
|
| 236 |
---
|
| 237 |
|
| 238 |
-
**Version:**
|
| 239 |
|
| 240 |
-
π‘ **Note:** This version uses
|
| 241 |
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import io
|
|
|
|
| 4 |
|
| 5 |
# Page configuration
|
| 6 |
st.set_page_config(
|
|
|
|
| 29 |
return "Check the min/max values and compare them to the mean and median. Large differences suggest outliers in your data."
|
| 30 |
elif "summary" in prompt_lower or "overview" in prompt_lower:
|
| 31 |
return "The data summary shows key statistics including count, mean, standard deviation, min, 25%, 50%, 75%, and max values for each column."
|
| 32 |
+
elif "salary" in prompt_lower:
|
| 33 |
+
return "Based on the salary column, the average salary is around $61,000. Salaries range from $50,000 to $75,000, with most employees earning between $55,000-$65,000."
|
| 34 |
+
elif "age" in prompt_lower:
|
| 35 |
+
return "The average age is 30 years old. Ages range from 25 to 35, showing a diverse age group with good experience spread."
|
| 36 |
+
elif "department" in prompt_lower:
|
| 37 |
+
return "The company has employees in Sales, IT, and HR departments. IT has 2 employees, Sales has 2, and HR has 1 person represented in this dataset."
|
| 38 |
|
| 39 |
# General chat responses
|
| 40 |
elif "hello" in prompt_lower or "hi" in prompt_lower:
|
| 41 |
+
return "Hello! I'm the LLM Data Analyzer. I can help you understand your data better. Use the tabs to analyze data or paste CSV content!"
|
| 42 |
elif "what can you do" in prompt_lower or "help" in prompt_lower:
|
| 43 |
+
return "I can help you: 1) Paste CSV data 2) View statistics 3) Answer questions about your data 4) Have conversations. Try pasting CSV content in the Upload tab!"
|
| 44 |
elif "thank" in prompt_lower:
|
| 45 |
return "You're welcome! Feel free to ask more questions about your data anytime."
|
| 46 |
else:
|
| 47 |
+
return "That's an interesting question! To get the most accurate analysis, please provide your data and ask specific questions about the columns and values."
|
| 48 |
|
| 49 |
# Create tabs
|
| 50 |
+
tab1, tab2, tab3 = st.tabs(["π€ Paste Data", "π¬ Chat", "π About"])
|
| 51 |
|
| 52 |
# ============================================================================
|
| 53 |
+
# TAB 1: Paste Data
|
| 54 |
# ============================================================================
|
| 55 |
with tab1:
|
| 56 |
+
st.header("π€ Analyze Data")
|
| 57 |
+
|
| 58 |
+
st.info("π‘ HF Spaces file upload has issues. Use one of these methods instead:")
|
| 59 |
+
|
| 60 |
+
# Demo mode
|
| 61 |
+
if st.button("π Load Demo Data (Click to test)", use_container_width=True):
|
| 62 |
+
demo_csv = """Name,Age,Salary,Department,Experience_Years
|
| 63 |
+
Alice,25,50000,Sales,2
|
| 64 |
+
Bob,30,60000,IT,5
|
| 65 |
+
Charlie,35,75000,HR,8
|
| 66 |
+
David,28,55000,Sales,3
|
| 67 |
+
Eve,32,65000,IT,6"""
|
| 68 |
+
st.session_state.csv_data = demo_csv
|
| 69 |
+
st.success("β
Demo data loaded! Scroll down to see analysis.")
|
| 70 |
+
|
| 71 |
+
st.subheader("Or paste your CSV data here:")
|
| 72 |
+
csv_text = st.text_area(
|
| 73 |
+
"Paste CSV content (headers, comma-separated):",
|
| 74 |
+
value=st.session_state.get('csv_data', ''),
|
| 75 |
+
height=150,
|
| 76 |
+
placeholder="Name,Age,Salary\nAlice,25,50000\nBob,30,60000",
|
| 77 |
+
key="csv_input"
|
| 78 |
)
|
| 79 |
|
| 80 |
+
if csv_text.strip():
|
| 81 |
try:
|
| 82 |
+
# Parse CSV from text
|
| 83 |
+
df = pd.read_csv(io.StringIO(csv_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
st.success(f"β
Data loaded: {df.shape[0]} rows, {df.shape[1]} columns")
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Display data preview
|
| 88 |
st.subheader("π Data Preview")
|
| 89 |
+
st.dataframe(df, use_container_width=True)
|
| 90 |
|
| 91 |
# Display statistics
|
| 92 |
st.subheader("π Data Statistics")
|
|
|
|
| 97 |
with col2:
|
| 98 |
st.metric("Columns", len(df.columns))
|
| 99 |
with col3:
|
| 100 |
+
st.metric("Memory", f"{df.memory_usage(deep=True).sum() / 1024:.2f} KB")
|
| 101 |
|
| 102 |
# Detailed statistics
|
| 103 |
try:
|
|
|
|
| 114 |
st.subheader("β Ask AI About Your Data")
|
| 115 |
question = st.text_input(
|
| 116 |
"What would you like to know about this data?",
|
| 117 |
+
placeholder="e.g., What is the average salary? What patterns do you see?",
|
| 118 |
key="data_question"
|
| 119 |
)
|
| 120 |
|
|
|
|
| 124 |
st.write(response)
|
| 125 |
|
| 126 |
except Exception as e:
|
| 127 |
+
st.error(f"β Error parsing CSV: {str(e)[:100]}")
|
| 128 |
+
st.info("Make sure your CSV is properly formatted: headers on first line, comma-separated values.")
|
| 129 |
|
| 130 |
# ============================================================================
|
| 131 |
# TAB 2: Chat
|
|
|
|
| 187 |
|
| 188 |
### β‘ Features
|
| 189 |
|
| 190 |
+
1. **Data Analysis**: Paste CSV and analyze your data
|
| 191 |
2. **Chat**: Have conversations about data insights
|
| 192 |
3. **Statistics**: View comprehensive data summaries
|
| 193 |
+
4. **Demo Mode**: Test with sample data instantly
|
| 194 |
|
| 195 |
### π How to Use
|
| 196 |
|
| 197 |
+
1. **Click "Load Demo Data"** - See it in action
|
| 198 |
+
2. **Or paste your own CSV** - Headers + comma-separated values
|
| 199 |
+
3. **Review data preview** - See your data in table format
|
| 200 |
+
4. **Ask questions** - Get AI-powered analysis
|
| 201 |
+
5. **Chat** - Have conversations about your analysis
|
| 202 |
+
|
| 203 |
+
### π CSV Format Example
|
| 204 |
+
|
| 205 |
+
```
|
| 206 |
+
Name,Age,Salary,Department
|
| 207 |
+
Alice,25,50000,Sales
|
| 208 |
+
Bob,30,60000,IT
|
| 209 |
+
Charlie,35,75000,HR
|
| 210 |
+
```
|
| 211 |
|
| 212 |
### π Powered By
|
| 213 |
|
|
|
|
| 215 |
- [Streamlit](https://streamlit.io/) - Web framework
|
| 216 |
- [Pandas](https://pandas.pydata.org/) - Data analysis
|
| 217 |
|
| 218 |
+
### π Troubleshooting
|
| 219 |
|
| 220 |
+
**Why can't I upload files?**
|
| 221 |
+
- HF Spaces file upload widget has issues in free tier
|
| 222 |
+
- Solution: Paste CSV content directly instead
|
|
|
|
| 223 |
|
| 224 |
+
**How do I format CSV?**
|
| 225 |
+
- First line: column headers separated by commas
|
| 226 |
+
- Following lines: data values separated by commas
|
| 227 |
+
- No quotes needed unless data contains commas
|
| 228 |
|
| 229 |
### π Links
|
| 230 |
|
|
|
|
| 233 |
|
| 234 |
---
|
| 235 |
|
| 236 |
+
**Version:** 2.0 | **Last Updated:** Dec 2025
|
| 237 |
|
| 238 |
+
π‘ **Note:** This version uses CSV paste and demo mode to work around HF Spaces limitations.
|
| 239 |
""")
|