Arif commited on
Commit
c85ec07
Β·
1 Parent(s): 94b7bfa

Updated app.py to version 13

Browse files
Files changed (1) hide show
  1. app.py +94 -47
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
- # Simple AI responses without API calls
17
- def get_ai_response(prompt):
18
- """Generate simple AI-like responses without external API"""
 
 
 
 
19
  prompt_lower = prompt.lower()
20
 
21
- # Data analysis responses
22
- if "average" in prompt_lower or "mean" in prompt_lower:
23
- return "Based on the data summary, the average values can be calculated from the statistical measures shown. For more detailed analysis, look at the mean values in the data description."
24
- elif "trend" in prompt_lower or "pattern" in prompt_lower:
25
- return "The data shows various patterns. Examine the min, max, and std deviation values to understand the distribution and trends in your dataset."
26
- elif "correlation" in prompt_lower or "relationship" in prompt_lower:
27
- return "To understand relationships between columns, look at how values change together. The standard deviation and percentiles in the summary can give insights."
28
- elif "outlier" in prompt_lower or "unusual" in prompt_lower:
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"])
@@ -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("πŸ’‘ 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):
@@ -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! Scroll down to see analysis.")
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? What patterns do you see?",
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 analysis and AI.")
 
 
 
 
 
 
 
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 anything...",
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**: 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
 
@@ -217,14 +265,13 @@ with tab3:
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,7 +280,7 @@ with tab3:
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
  """)
 
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
  """)