Tamannathakur commited on
Commit
e9db767
·
verified ·
1 Parent(s): 7052a18

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -312
app.py DELETED
@@ -1,312 +0,0 @@
1
- import gradio as gr
2
- import pandas as pd
3
- import os
4
- from data_engine import (
5
- clean_numeric, run_analysis, create_visualization, handle_missing_data,
6
- undo_last_change, undo_all_changes, download_dataset,
7
- display_data_format, display_text_format
8
- )
9
- from ai_agent import initialize_llm, analyze_question
10
- from prompts import SAMPLE_QUESTIONS
11
-
12
- llm = None
13
- uploaded_df = None
14
- original_df = None
15
- dataset_name = None
16
- change_history = []
17
-
18
- def upload_dataset(file):
19
- global uploaded_df, original_df, dataset_name
20
- if file is None:
21
- return "No file uploaded", gr.update(visible=False), gr.update(choices=[]), gr.update(visible=False)
22
- try:
23
- dataset_name = os.path.basename(file.name)
24
- if file.name.endswith('.csv'):
25
- uploaded_df = pd.read_csv(file.name)
26
- elif file.name.endswith(('.xlsx', '.xls')):
27
- uploaded_df = pd.read_excel(file.name)
28
- else:
29
- return "Unsupported file format. Please upload CSV or Excel files.", gr.update(visible=False), gr.update(choices=[]), gr.update(visible=False)
30
- uploaded_df = clean_numeric(uploaded_df)
31
- original_df = uploaded_df.copy()
32
- info_text = f" Dataset Loaded: {dataset_name} ({uploaded_df.shape[0]} rows × {uploaded_df.shape[1]} columns)"
33
- return info_text, gr.update(visible=False), gr.update(choices=list(uploaded_df.columns), value=[]), gr.update(visible=True)
34
- except Exception as e:
35
- return f"Error loading file: {str(e)}", gr.update(visible=False), gr.update(choices=[]), gr.update(visible=False)
36
-
37
- def clear_dataset():
38
- global uploaded_df, original_df, dataset_name, change_history
39
- uploaded_df = None
40
- original_df = None
41
- dataset_name = None
42
- change_history = []
43
- return "Dataset cleared. Please upload a new file.", gr.update(visible=False), gr.update(choices=[], value=[]), gr.update(visible=False)
44
-
45
- def update_preview(format_type, selected_columns):
46
- if format_type == "None":
47
- return None, "", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
48
- elif format_type == "DataFrame":
49
- return display_data_format(format_type, selected_columns, uploaded_df), "", gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
50
- else:
51
- return None, display_text_format(format_type, selected_columns, uploaded_df), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
52
-
53
- def handle_analysis_change(analysis_type, selected_columns):
54
- result_text, data_table = run_analysis(analysis_type, selected_columns, uploaded_df)
55
- if result_text and result_text.strip() and analysis_type != "None":
56
- if data_table is not None:
57
- return gr.update(value=result_text, visible=True), gr.update(visible=True), gr.update(value=data_table, visible=True)
58
- else:
59
- return gr.update(value=result_text, visible=True), gr.update(visible=True), gr.update(visible=False)
60
- else:
61
- return gr.update(value="", visible=False), gr.update(visible=False), gr.update(visible=False)
62
-
63
- def handle_viz_change(viz_type, selected_columns):
64
- result = create_visualization(viz_type, selected_columns, uploaded_df)
65
- if result and len(result) == 3:
66
- fig, explanation, chart_obj = result
67
- if explanation and fig is not None:
68
- return fig, gr.update(visible=True), explanation, gr.update(visible=True)
69
- else:
70
- return None, gr.update(visible=False), explanation or "Error in visualization", gr.update(visible=False)
71
- else:
72
- return None, gr.update(visible=False), "Error in visualization", gr.update(visible=False)
73
-
74
- def show_constant_input(method):
75
- return gr.update(visible=(method == "Constant Fill"))
76
-
77
- def handle_data_and_refresh(method, selected_columns, constant_value, analysis_type):
78
- global uploaded_df, change_history
79
-
80
- result, uploaded_df, change_history = handle_missing_data(method, selected_columns, constant_value, uploaded_df, change_history)
81
-
82
- if analysis_type == "Missing Values" and uploaded_df is not None:
83
- analysis_result = "Missing Values Analysis:\n" + "=" * 30 + "\n\n"
84
-
85
- patterns = ['UNKNOWN', 'unknown', 'ERROR', 'error', 'NULL', 'null', 'NA', 'na', 'N/A',
86
- 'Not Given', 'not given', 'NOT GIVEN', '', ' ', '-', '?', 'NaN', 'nan',
87
- 'None', 'none', 'NONE', '#N/A', 'n/a', 'N.A.', 'n.a.']
88
-
89
- for col in uploaded_df.columns:
90
- nan_count = uploaded_df[col].isnull().sum()
91
- pseudo_missing_count = 0
92
-
93
- non_null_data = uploaded_df[col].dropna()
94
- if len(non_null_data) > 0:
95
- col_str = non_null_data.astype(str).str.strip()
96
- empty_count = (col_str == '').sum()
97
- pattern_count = 0
98
- for pattern in patterns:
99
- if pattern != '':
100
- pattern_count += (col_str.str.lower() == pattern.lower()).sum()
101
- pseudo_missing_count = empty_count + pattern_count
102
-
103
- total_missing = nan_count + pseudo_missing_count
104
- missing_percent = (total_missing / len(uploaded_df)) * 100
105
-
106
- if total_missing > 0:
107
- details = []
108
- if nan_count > 0:
109
- details.append(f"{nan_count} NaN")
110
- if pseudo_missing_count > 0:
111
- details.append(f"{pseudo_missing_count} text-missing")
112
- detail_str = f" ({', '.join(details)})"
113
- else:
114
- detail_str = ""
115
-
116
- analysis_result += f"{col}: {total_missing} missing ({missing_percent:.2f}%){detail_str}\n"
117
-
118
- return result, gr.update(visible=True), gr.update(choices=list(uploaded_df.columns), value=[]), gr.update(value=analysis_result, visible=True), gr.update(visible=True)
119
-
120
- return result, gr.update(visible=True), gr.update(choices=list(uploaded_df.columns), value=[]), gr.update(), gr.update()
121
-
122
- def handle_undo_and_refresh(analysis_type, is_undo_all=False):
123
- global uploaded_df, change_history
124
-
125
- if is_undo_all:
126
- result, uploaded_df, change_history = undo_all_changes(original_df, change_history)
127
- else:
128
- result, uploaded_df, change_history = undo_last_change(uploaded_df, change_history)
129
-
130
- if analysis_type == "Missing Values" and uploaded_df is not None:
131
- result_text, data_table = run_analysis(analysis_type, [], uploaded_df)
132
- return result, gr.update(visible=True), gr.update(choices=list(uploaded_df.columns), value=[]), gr.update(value=result_text, visible=True), gr.update(visible=True)
133
-
134
- return result, gr.update(visible=True), gr.update(choices=list(uploaded_df.columns), value=[]), gr.update(), gr.update()
135
-
136
- def handle_question_analysis(question, selected_columns):
137
- return analyze_question(question, selected_columns, uploaded_df, llm)
138
-
139
- custom_css = """
140
- .gradio-container {
141
- max-width: 1400px !important;
142
- margin: 0 auto !important;
143
- }
144
- .header-box {
145
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
146
- border-radius: 15px;
147
- padding: 25px;
148
- margin: 20px auto;
149
- text-align: center;
150
- box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
151
- }
152
- .header-title {
153
- font-size: 36px;
154
- font-weight: bold;
155
- color: white;
156
- margin: 0;
157
- text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
158
- }
159
- .section-box {
160
- background-color: #f8f9fa;
161
- padding: 20px;
162
- border-radius: 12px;
163
- margin: 15px 0;
164
- border: 1px solid #e9ecef;
165
- }
166
- """
167
-
168
- with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
169
-
170
- gr.HTML("""
171
- <div class="header-box">
172
- <h1 class="header-title">SparkNova</h1>
173
- <p style="color: white; font-size: 18px; margin: 10px 0 0 0;">Advanced Data Analysis Platform</p>
174
- </div>
175
- """)
176
-
177
- with gr.Row():
178
- with gr.Column(scale=1):
179
- gr.Markdown("### Upload Dataset")
180
- file_input = gr.File(label="Choose CSV or Excel File", file_types=[".csv", ".xlsx", ".xls"])
181
- dataset_info = gr.Markdown()
182
-
183
- with gr.Row():
184
- clear_btn = gr.Button("Clear Dataset", variant="secondary", size="sm")
185
-
186
- column_selector = gr.CheckboxGroup(
187
- label="Select Columns (optional - for multi-column charts)",
188
- choices=[],
189
- visible=False
190
- )
191
-
192
- format_selector = gr.Dropdown(
193
- choices=["None", "DataFrame", "JSON", "Dictionary"],
194
- value="None",
195
- label="Display Format"
196
- )
197
-
198
- gr.Markdown("### Choose an Analysis Type")
199
- analysis_selector = gr.Dropdown(
200
- choices=["None", "Summary", "Describe", "Top 5 Rows", "Bottom 5 Rows", "Missing Values", "Group & Aggregate", "Calculate Expressions", "Highest Correlation"],
201
- value="None",
202
- label="Analysis Type"
203
- )
204
-
205
- gr.Markdown("### Visualization Types")
206
- viz_selector = gr.Dropdown(
207
- choices=["None", "Bar Chart", "Line Chart", "Scatter Plot", "Pie Chart", "Histogram", "Box Plot", "Heat Map"],
208
- value="None",
209
- label="Chart Type"
210
- )
211
-
212
- gr.Markdown("### Handling Data")
213
- data_handler = gr.Dropdown(
214
- choices=["None", "Forward Fill", "Backward Fill", "Constant Fill", "Mean Fill", "Median Fill", "Mode Fill", "Drop Columns"],
215
- value="None",
216
- label="Data Handling Method"
217
- )
218
-
219
- constant_input = gr.Textbox(
220
- label="Constant Value (for Constant Fill)",
221
- placeholder="Enter value to fill missing data",
222
- visible=False
223
- )
224
-
225
- with gr.Row():
226
- apply_btn = gr.Button("Apply Change", variant="primary", size="sm")
227
- undo_last_btn = gr.Button("Undo Last", variant="secondary", size="sm")
228
-
229
- with gr.Row():
230
- undo_all_btn = gr.Button("Undo All", variant="secondary", size="sm")
231
- download_btn = gr.Button("Download", variant="secondary", size="sm")
232
-
233
- data_handling_output = gr.Textbox(label="Data Handling Results", lines=3, visible=False, interactive=False)
234
- download_file = gr.File(label="Download Modified Dataset", visible=False)
235
-
236
- with gr.Column(scale=2):
237
- preview_heading = gr.Markdown("### Dataset Preview", visible=False)
238
- dataset_preview = gr.Dataframe(wrap=True, visible=False)
239
- text_preview = gr.Textbox(label="Text Preview", lines=15, visible=False)
240
-
241
- analysis_heading = gr.Markdown("### Analysis Results", visible=False)
242
- analysis_output = gr.Textbox(label="Analysis Output", lines=10, visible=False, interactive=False)
243
- analysis_data_table = gr.Dataframe(label="Data Table", visible=False, wrap=True)
244
- chart_output_new = gr.Plot(label="Chart", visible=False)
245
- chart_explanation = gr.Textbox(label="Chart Analysis", lines=5, visible=False, interactive=False)
246
-
247
- gr.Markdown("### Sample Questions")
248
- with gr.Row():
249
- for i in range(0, len(SAMPLE_QUESTIONS), 3):
250
- with gr.Column():
251
- for j in range(3):
252
- if i + j < len(SAMPLE_QUESTIONS):
253
- gr.Markdown(f"• {SAMPLE_QUESTIONS[i + j]}")
254
-
255
- gr.Markdown("### Ask Your Question")
256
- user_question = gr.Textbox(
257
- label="Enter your question",
258
- placeholder="Ask anything about your data...",
259
- lines=3
260
- )
261
- submit_btn = gr.Button("Analyze", variant="primary", size="lg")
262
-
263
- gr.Markdown("### Analysis Results")
264
- with gr.Tabs():
265
- with gr.Tab("Response"):
266
- output_text = gr.Textbox(
267
- label="Analysis Response",
268
- interactive=False,
269
- lines=15,
270
- show_copy_button=True
271
- )
272
-
273
- with gr.Tab("Visualization"):
274
- chart_output = gr.Plot(label="Generated Chart")
275
-
276
- with gr.Tab("Data"):
277
- result_table = gr.Dataframe(label="Result Data", wrap=True)
278
-
279
- file_input.change(upload_dataset, inputs=file_input, outputs=[dataset_info, dataset_preview, column_selector, column_selector])
280
- clear_btn.click(clear_dataset, outputs=[dataset_info, dataset_preview, column_selector, column_selector])
281
-
282
- format_selector.change(update_preview, inputs=[format_selector, column_selector], outputs=[dataset_preview, text_preview, dataset_preview, text_preview, preview_heading])
283
- column_selector.change(update_preview, inputs=[format_selector, column_selector], outputs=[dataset_preview, text_preview, dataset_preview, text_preview, preview_heading])
284
-
285
- analysis_selector.change(handle_analysis_change, inputs=[analysis_selector, column_selector], outputs=[analysis_output, analysis_heading, analysis_data_table])
286
- column_selector.change(handle_analysis_change, inputs=[analysis_selector, column_selector], outputs=[analysis_output, analysis_heading, analysis_data_table])
287
-
288
- viz_selector.change(handle_viz_change, inputs=[viz_selector, column_selector], outputs=[chart_output_new, chart_output_new, chart_explanation, chart_explanation])
289
- column_selector.change(handle_viz_change, inputs=[viz_selector, column_selector], outputs=[chart_output_new, chart_output_new, chart_explanation, chart_explanation])
290
-
291
- submit_btn.click(handle_question_analysis, inputs=[user_question, column_selector], outputs=[output_text, chart_output, result_table])
292
-
293
- data_handler.change(show_constant_input, inputs=data_handler, outputs=constant_input)
294
- apply_btn.click(handle_data_and_refresh, inputs=[data_handler, column_selector, constant_input, analysis_selector], outputs=[data_handling_output, data_handling_output, column_selector, analysis_output, analysis_heading])
295
-
296
- undo_last_btn.click(lambda analysis_type: handle_undo_and_refresh(analysis_type, False), inputs=[analysis_selector], outputs=[data_handling_output, data_handling_output, column_selector, analysis_output, analysis_heading])
297
- undo_all_btn.click(lambda analysis_type: handle_undo_and_refresh(analysis_type, True), inputs=[analysis_selector], outputs=[data_handling_output, data_handling_output, column_selector, analysis_output, analysis_heading])
298
-
299
- def handle_download():
300
- filepath = download_dataset(uploaded_df, dataset_name)
301
- return gr.File(value=filepath, visible=bool(filepath))
302
-
303
- download_btn.click(handle_download, outputs=download_file)
304
-
305
- gr.HTML("<div style='text-align: center; margin-top: 20px; color: #666;'>Powered by GROQ LLM & Gradio</div>")
306
-
307
- if __name__ == "__main__":
308
- llm = initialize_llm()
309
- if not llm:
310
- print("Warning: Failed to initialize GROQ API")
311
-
312
- demo.launch(show_error=True, share=False)