Spaces:
Running
Running
Arif
commited on
Commit
Β·
c85ec07
1
Parent(s):
94b7bfa
Updated app.py to version 13
Browse files
app.py
CHANGED
|
@@ -13,38 +13,69 @@ st.set_page_config(
|
|
| 13 |
st.title("π LLM Data Analyzer")
|
| 14 |
st.write("*Analyze data and chat with AI - Powered by Hugging Face Spaces*")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
prompt_lower = prompt.lower()
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
if
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
elif "what can you do" in prompt_lower or "help" in prompt_lower:
|
| 43 |
-
return "I can
|
| 44 |
elif "thank" in prompt_lower:
|
| 45 |
-
return "You're welcome!
|
| 46 |
else:
|
| 47 |
-
return "
|
| 48 |
|
| 49 |
# Create tabs
|
| 50 |
tab1, tab2, tab3 = st.tabs(["π€ Paste Data", "π¬ Chat", "π About"])
|
|
@@ -55,7 +86,7 @@ tab1, tab2, tab3 = st.tabs(["π€ Paste Data", "π¬ Chat", "π About"])
|
|
| 55 |
with tab1:
|
| 56 |
st.header("π€ Analyze Data")
|
| 57 |
|
| 58 |
-
st.info("π‘
|
| 59 |
|
| 60 |
# Demo mode
|
| 61 |
if st.button("π Load Demo Data (Click to test)", use_container_width=True):
|
|
@@ -66,7 +97,7 @@ 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!
|
| 70 |
|
| 71 |
st.subheader("Or paste your CSV data here:")
|
| 72 |
csv_text = st.text_area(
|
|
@@ -82,6 +113,9 @@ Eve,32,65000,IT,6"""
|
|
| 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
|
|
@@ -114,12 +148,12 @@ Eve,32,65000,IT,6"""
|
|
| 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?
|
| 118 |
key="data_question"
|
| 119 |
)
|
| 120 |
|
| 121 |
if question:
|
| 122 |
-
response = get_ai_response(question)
|
| 123 |
st.success("β
Analysis Complete")
|
| 124 |
st.write(response)
|
| 125 |
|
|
@@ -132,7 +166,14 @@ Eve,32,65000,IT,6"""
|
|
| 132 |
# ============================================================================
|
| 133 |
with tab2:
|
| 134 |
st.header("π¬ Chat with AI Assistant")
|
| 135 |
-
st.write("Have a conversation about data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
# Initialize session state for chat history
|
| 138 |
if "messages" not in st.session_state:
|
|
@@ -146,7 +187,7 @@ with tab2:
|
|
| 146 |
# Chat input
|
| 147 |
user_input = st.text_input(
|
| 148 |
"Type your message:",
|
| 149 |
-
placeholder="Ask me
|
| 150 |
key="chat_input"
|
| 151 |
)
|
| 152 |
|
|
@@ -154,8 +195,8 @@ with tab2:
|
|
| 154 |
# Add user message immediately
|
| 155 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 156 |
|
| 157 |
-
# Get response
|
| 158 |
-
response = get_ai_response(user_input)
|
| 159 |
|
| 160 |
# Add assistant message
|
| 161 |
st.session_state.messages.append({
|
|
@@ -188,17 +229,24 @@ with tab3:
|
|
| 188 |
### β‘ Features
|
| 189 |
|
| 190 |
1. **Data Analysis**: Paste CSV and analyze your data
|
| 191 |
-
2. **Chat**:
|
| 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"
|
| 198 |
-
2. **
|
| 199 |
-
3. **
|
| 200 |
-
4. **
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
### π CSV Format Example
|
| 204 |
|
|
@@ -217,14 +265,13 @@ with tab3:
|
|
| 217 |
|
| 218 |
### π Troubleshooting
|
| 219 |
|
| 220 |
-
**
|
| 221 |
-
-
|
| 222 |
-
-
|
| 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,7 +280,7 @@ with tab3:
|
|
| 233 |
|
| 234 |
---
|
| 235 |
|
| 236 |
-
**Version:** 2.
|
| 237 |
|
| 238 |
-
π‘ **Note:**
|
| 239 |
""")
|
|
|
|
| 13 |
st.title("π LLM Data Analyzer")
|
| 14 |
st.write("*Analyze data and chat with AI - Powered by Hugging Face Spaces*")
|
| 15 |
|
| 16 |
+
# Store dataframe in session state globally
|
| 17 |
+
if "current_df" not in st.session_state:
|
| 18 |
+
st.session_state.current_df = None
|
| 19 |
+
|
| 20 |
+
# AI response function with data awareness
|
| 21 |
+
def get_ai_response(prompt, df=None):
|
| 22 |
+
"""Generate AI responses with data awareness"""
|
| 23 |
prompt_lower = prompt.lower()
|
| 24 |
|
| 25 |
+
# If we have data, provide data-specific responses
|
| 26 |
+
if df is not None and not df.empty:
|
| 27 |
+
# Try to generate data-aware responses
|
| 28 |
+
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
| 29 |
+
|
| 30 |
+
if "average" in prompt_lower or "mean" in prompt_lower:
|
| 31 |
+
if numeric_cols:
|
| 32 |
+
means = df[numeric_cols].mean()
|
| 33 |
+
return f"π **Average values for numeric columns:**\n" + "\n".join([f"- {col}: {means[col]:.2f}" for col in means.index])
|
| 34 |
+
return "The data summary shows average values for numeric columns."
|
| 35 |
+
|
| 36 |
+
elif "max" in prompt_lower or "highest" in prompt_lower:
|
| 37 |
+
if numeric_cols:
|
| 38 |
+
maxes = df[numeric_cols].max()
|
| 39 |
+
return f"π **Maximum values for numeric columns:**\n" + "\n".join([f"- {col}: {maxes[col]}" for col in maxes.index])
|
| 40 |
+
return "No numeric columns found to show max values."
|
| 41 |
+
|
| 42 |
+
elif "min" in prompt_lower or "lowest" in prompt_lower:
|
| 43 |
+
if numeric_cols:
|
| 44 |
+
mins = df[numeric_cols].min()
|
| 45 |
+
return f"π **Minimum values for numeric columns:**\n" + "\n".join([f"- {col}: {mins[col]}" for col in mins.index])
|
| 46 |
+
return "No numeric columns found to show min values."
|
| 47 |
+
|
| 48 |
+
elif "count" in prompt_lower or "rows" in prompt_lower or "how many" in prompt_lower:
|
| 49 |
+
return f"π Your dataset has **{len(df)} rows** and **{len(df.columns)} columns**.\n\nColumns: {', '.join(df.columns.tolist())}"
|
| 50 |
+
|
| 51 |
+
elif "columns" in prompt_lower or "fields" in prompt_lower or "headers" in prompt_lower:
|
| 52 |
+
return f"π **Dataset Columns ({len(df.columns)}):**\n" + "\n".join([f"- {col}" for col in df.columns])
|
| 53 |
+
|
| 54 |
+
elif "summary" in prompt_lower or "overview" in prompt_lower or "describe" in prompt_lower:
|
| 55 |
+
summary = f"π **Data Summary:**\n"
|
| 56 |
+
summary += f"- Rows: {len(df)}\n"
|
| 57 |
+
summary += f"- Columns: {len(df.columns)}\n"
|
| 58 |
+
summary += f"- Column names: {', '.join(df.columns.tolist())}\n"
|
| 59 |
+
if numeric_cols:
|
| 60 |
+
summary += f"- Numeric columns: {', '.join(numeric_cols)}\n"
|
| 61 |
+
return summary
|
| 62 |
+
|
| 63 |
+
elif "trend" in prompt_lower or "pattern" in prompt_lower:
|
| 64 |
+
return "The data shows various patterns. Check the statistics above to identify trends in the numeric columns."
|
| 65 |
+
|
| 66 |
+
else:
|
| 67 |
+
# Generic response with data context
|
| 68 |
+
return f"I have access to your data with {len(df)} rows and {len(df.columns)} columns: {', '.join(df.columns.tolist())}. Ask me specific questions like: What's the average? Show me the max values? How many rows?"
|
| 69 |
+
|
| 70 |
+
# Fallback responses (no data)
|
| 71 |
+
if "hello" in prompt_lower or "hi" in prompt_lower:
|
| 72 |
+
return "Hello! I'm the LLM Data Analyzer. Load some data first, then ask me questions about it!"
|
| 73 |
elif "what can you do" in prompt_lower or "help" in prompt_lower:
|
| 74 |
+
return "I can: 1) Load demo data 2) Analyze your CSV 3) Answer questions about averages, max, min, columns, etc. 4) Chat about your data!"
|
| 75 |
elif "thank" in prompt_lower:
|
| 76 |
+
return "You're welcome! Ask me anything about your data!"
|
| 77 |
else:
|
| 78 |
+
return "Please load some data first (click 'Load Demo Data' or paste CSV), then ask me questions about it!"
|
| 79 |
|
| 80 |
# Create tabs
|
| 81 |
tab1, tab2, tab3 = st.tabs(["π€ Paste Data", "π¬ Chat", "π About"])
|
|
|
|
| 86 |
with tab1:
|
| 87 |
st.header("π€ Analyze Data")
|
| 88 |
|
| 89 |
+
st.info("π‘ Load demo data or paste your CSV to start analyzing!")
|
| 90 |
|
| 91 |
# Demo mode
|
| 92 |
if st.button("π Load Demo Data (Click to test)", use_container_width=True):
|
|
|
|
| 97 |
David,28,55000,Sales,3
|
| 98 |
Eve,32,65000,IT,6"""
|
| 99 |
st.session_state.csv_data = demo_csv
|
| 100 |
+
st.success("β
Demo data loaded! Check the Chat tab to ask questions about it.")
|
| 101 |
|
| 102 |
st.subheader("Or paste your CSV data here:")
|
| 103 |
csv_text = st.text_area(
|
|
|
|
| 113 |
# Parse CSV from text
|
| 114 |
df = pd.read_csv(io.StringIO(csv_text))
|
| 115 |
|
| 116 |
+
# Store in session state for chat to access
|
| 117 |
+
st.session_state.current_df = df
|
| 118 |
+
|
| 119 |
st.success(f"β
Data loaded: {df.shape[0]} rows, {df.shape[1]} columns")
|
| 120 |
|
| 121 |
# Display data preview
|
|
|
|
| 148 |
st.subheader("β Ask AI About Your Data")
|
| 149 |
question = st.text_input(
|
| 150 |
"What would you like to know about this data?",
|
| 151 |
+
placeholder="e.g., What is the average salary? How many rows?",
|
| 152 |
key="data_question"
|
| 153 |
)
|
| 154 |
|
| 155 |
if question:
|
| 156 |
+
response = get_ai_response(question, df)
|
| 157 |
st.success("β
Analysis Complete")
|
| 158 |
st.write(response)
|
| 159 |
|
|
|
|
| 166 |
# ============================================================================
|
| 167 |
with tab2:
|
| 168 |
st.header("π¬ Chat with AI Assistant")
|
| 169 |
+
st.write("Have a conversation about your data.")
|
| 170 |
+
|
| 171 |
+
# Show current data status
|
| 172 |
+
if st.session_state.current_df is not None:
|
| 173 |
+
st.success(f"β
Data loaded: {len(st.session_state.current_df)} rows, {len(st.session_state.current_df.columns)} columns")
|
| 174 |
+
st.write(f"Columns: {', '.join(st.session_state.current_df.columns.tolist())}")
|
| 175 |
+
else:
|
| 176 |
+
st.warning("β οΈ No data loaded yet. Go to 'Paste Data' tab and load data first!")
|
| 177 |
|
| 178 |
# Initialize session state for chat history
|
| 179 |
if "messages" not in st.session_state:
|
|
|
|
| 187 |
# Chat input
|
| 188 |
user_input = st.text_input(
|
| 189 |
"Type your message:",
|
| 190 |
+
placeholder="Ask me about your data...",
|
| 191 |
key="chat_input"
|
| 192 |
)
|
| 193 |
|
|
|
|
| 195 |
# Add user message immediately
|
| 196 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 197 |
|
| 198 |
+
# Get response with data context
|
| 199 |
+
response = get_ai_response(user_input, st.session_state.current_df)
|
| 200 |
|
| 201 |
# Add assistant message
|
| 202 |
st.session_state.messages.append({
|
|
|
|
| 229 |
### β‘ Features
|
| 230 |
|
| 231 |
1. **Data Analysis**: Paste CSV and analyze your data
|
| 232 |
+
2. **Smart Chat**: Chat with AI about your data
|
| 233 |
3. **Statistics**: View comprehensive data summaries
|
| 234 |
4. **Demo Mode**: Test with sample data instantly
|
| 235 |
|
| 236 |
### π How to Use
|
| 237 |
|
| 238 |
+
1. **Load Data** - Click "Load Demo Data" or paste your CSV
|
| 239 |
+
2. **View Preview** - See your data in table format
|
| 240 |
+
3. **Chat** - Go to Chat tab and ask questions about your data
|
| 241 |
+
4. **Get Insights** - AI analyzes and answers questions
|
| 242 |
+
|
| 243 |
+
### π‘ Example Questions
|
| 244 |
+
|
| 245 |
+
- "What's the average salary?"
|
| 246 |
+
- "Show me the maximum values"
|
| 247 |
+
- "How many rows do I have?"
|
| 248 |
+
- "What columns are in the data?"
|
| 249 |
+
- "Give me a summary"
|
| 250 |
|
| 251 |
### π CSV Format Example
|
| 252 |
|
|
|
|
| 265 |
|
| 266 |
### π Troubleshooting
|
| 267 |
|
| 268 |
+
**Chat can't see my data?**
|
| 269 |
+
- Make sure to load data in the "Paste Data" tab first
|
| 270 |
+
- Then go to "Chat" tab - it will show your data status
|
| 271 |
|
| 272 |
**How do I format CSV?**
|
| 273 |
- First line: column headers separated by commas
|
| 274 |
- Following lines: data values separated by commas
|
|
|
|
| 275 |
|
| 276 |
### π Links
|
| 277 |
|
|
|
|
| 280 |
|
| 281 |
---
|
| 282 |
|
| 283 |
+
**Version:** 2.1 | **Last Updated:** Dec 2025
|
| 284 |
|
| 285 |
+
π‘ **Note:** Chat now has access to your data! Load data first, then ask questions.
|
| 286 |
""")
|