-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
175 lines (141 loc) · 5.39 KB
/
app.py
File metadata and controls
175 lines (141 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from flask import Flask, request, jsonify, render_template
from werkzeug.utils import secure_filename
import pandas as pd
import os
from src.visualize import DataVisualizer
from src.chatsection import DataChatBot
from src.statisticsanalysis import DataAnalyzer
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 200 * 1024 * 1024 # 200MB max file size
app.config['UPLOAD_FOLDER'] = 'uploads'
# Initialize chatbot
chatbot = DataChatBot()
@app.route('/')
def index():
return render_template('landing.html')
@app.route('/dashboard')
def dashboard():
return render_template('dashboard.html')
@app.route('/learn')
def learn():
return render_template('learn.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file and file.filename.endswith('.csv'):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
# Read dataset
df = pd.read_csv(filepath)
# Initialize analyzers
visualizer = DataVisualizer(df)
analyzer = DataAnalyzer(df)
chatbot.set_dataset_info(df)
# Get initial insights and available plots
insights = chatbot.generate_initial_insights()
available_plots = visualizer.get_available_plots()
basic_stats = analyzer.get_basic_stats()
return jsonify({
'success': True,
'insights': insights,
'available_plots': available_plots,
'basic_stats': basic_stats.to_dict(),
'missing_values': analyzer.missing_values_analysis()
})
return jsonify({'error': 'Only CSV files are allowed'}), 400
@app.route('/visualize', methods=['POST'])
def create_visualization():
data = request.json
plot_type = data['plot_type']
params = data.get('params', {})
# Load the most recent dataset from the upload folder
uploaded_files = os.listdir(app.config['UPLOAD_FOLDER'])
if not uploaded_files:
return jsonify({'error': 'No dataset available for visualization'}), 400
latest_file = max(
[os.path.join(app.config['UPLOAD_FOLDER'], f) for f in uploaded_files],
key=os.path.getctime
)
df = pd.read_csv(latest_file)
visualizer = DataVisualizer(df)
plot_functions = {
'histogram': visualizer.create_histogram,
'boxplot': visualizer.create_boxplot,
'scatter': visualizer.create_scatter,
'correlation': visualizer.create_correlation_heatmap,
'bar': visualizer.create_bar_plot,
'line': visualizer.create_line_plot,
'pie': visualizer.create_pie_chart,
'violin': visualizer.create_violin,
'QQ': visualizer.create_QQ
}
if plot_type not in plot_functions:
return jsonify({'error': 'Invalid plot type'}), 400
try:
plot_base64 = plot_functions[plot_type](**params)
return jsonify({'success': True, 'plot': plot_base64})
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/stats', methods=['POST'])
def get_statistics():
data = request.json
column = data.get('column')
stat_type = data.get('stat_type', 'basic')
# Load the most recent dataset from the upload folder
uploaded_files = os.listdir(app.config['UPLOAD_FOLDER'])
if not uploaded_files:
return jsonify({'error': 'No dataset available for analysis'}), 400
latest_file = max(
[os.path.join(app.config['UPLOAD_FOLDER'], f) for f in uploaded_files],
key=os.path.getctime
)
df = pd.read_csv(latest_file)
analyzer = DataAnalyzer(df)
if stat_type == 'basic':
stats = analyzer.get_basic_stats(column)
elif stat_type == 'normality':
stats = analyzer.check_normality(column)
elif stat_type == 'correlation':
stats = analyzer.correlation_analysis()
elif stat_type == 'outliers':
stats = analyzer.get_outliers(column)
else:
return jsonify({'error': 'Invalid statistics type'}), 400
return jsonify({'success': True, 'statistics': stats})
@app.route('/ai-query', methods=['POST'])
def ai_query():
try:
data = request.json
if not data or 'query' not in data:
return jsonify({'error': 'No query provided'}), 400
query = data['query']
# Load the most recent dataset
uploaded_files = os.listdir(app.config['UPLOAD_FOLDER'])
if not uploaded_files:
return jsonify({'error': 'No dataset available'}), 400
latest_file = max(
[os.path.join(app.config['UPLOAD_FOLDER'], f) for f in uploaded_files],
key=os.path.getctime
)
df = pd.read_csv(latest_file)
analyzer = DataAnalyzer(df)
df_summary = analyzer.get_basic_stats().to_string()
# Process the query and get response
response = chatbot.process_query(query, df_summary)
return jsonify({
'success': True,
'response': response
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
if __name__ == '__main__':
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
app.run(debug=True, host="0.0.0.0", threaded=True)