forked from 22aiml053malak/SyntheticDataGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
184 lines (145 loc) · 6.7 KB
/
app.py
File metadata and controls
184 lines (145 loc) · 6.7 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
176
177
178
179
180
181
182
183
184
from flask import Flask, render_template, request, send_file, redirect, url_for, flash
import pandas as pd
import os
import uuid
import logging
import stat
from sdv.metadata import SingleTableMetadata
from sdv.single_table import CTGANSynthesizer
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', 'dev-secret-key') # Needed for flash messages
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'outputs'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@app.route('/health')
def health():
return {'status': 'healthy'}, 200
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
flash("No file uploaded", "danger")
return redirect(url_for('index'))
file = request.files['file']
if file.filename == '':
flash("No selected file", "danger")
return redirect(url_for('index'))
column_names = request.form.get('column_names', '')
categorical_columns = [col.strip() for col in column_names.split(',') if col.strip()]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filepath)
df = pd.read_csv(filepath)
df[categorical_columns] = df[categorical_columns].astype(str)
# Metadata detection
metadata = SingleTableMetadata()
metadata.detect_from_dataframe(df)
# Train model
try:
logging.info("Training CTGAN model...")
model = CTGANSynthesizer(metadata)
model.fit(df)
model_path = os.path.join(OUTPUT_FOLDER, "ctgan_model.pkl")
model.save(model_path)
logging.info("Model training completed and saved.")
except Exception as e:
logging.error(f"Error training model: {str(e)}")
flash(f"Error training model: {str(e)}", "danger")
return redirect(url_for('index'))
flash("File uploaded and model trained successfully!", "success")
return redirect(url_for('generate_page'))
@app.route('/manual_entry', methods=['GET', 'POST'])
def manual_entry():
if request.method == 'POST':
try:
num_columns = int(request.form.get('num_columns', 0))
num_rows = int(request.form.get('num_rows', 0))
column_names = request.form.getlist('column_names') or request.form.get('column_names', '').split(',')
if num_columns != len(column_names) or not column_names:
flash("Error: Column names are missing or do not match the column count.", "danger")
return redirect(url_for('manual_entry'))
return render_template('manual_entry.html', num_columns=num_columns, column_names=column_names, num_rows=num_rows)
except Exception as e:
logging.error(f"Error processing manual entry form: {str(e)}")
flash("Invalid input format!", "danger")
return redirect(url_for('manual_entry'))
return render_template('manual_entry.html')
@app.route('/submit_manual_data', methods=['POST'])
def submit_manual_data():
try:
num_rows = int(request.form.get('num_rows', 0))
column_names = request.form.getlist('column_names')
if not column_names or all(name.strip() == "" for name in column_names):
flash("Error: Column names are missing or empty!", "danger")
return redirect(url_for('manual_entry'))
data = []
for i in range(num_rows):
row = [request.form.get(f'row_{i}_{j}', '').strip() for j in range(len(column_names))]
if any(cell != "" for cell in row):
data.append(row)
if not data:
flash("Error: No valid data entered! Please fill at least one row.", "danger")
return redirect(url_for('manual_entry'))
df = pd.DataFrame(data, columns=column_names)
csv_path = os.path.join(UPLOAD_FOLDER, "manual_data.csv")
df.to_csv(csv_path, index=False)
logging.info(f"Saved manual data to: {csv_path}")
# Train Model
metadata = SingleTableMetadata()
metadata.detect_from_dataframe(df)
logging.info("Training CTGAN model with manual data...")
model = CTGANSynthesizer(metadata)
model.fit(df)
model_path = os.path.join(OUTPUT_FOLDER, "ctgan_model.pkl")
model.save(model_path)
logging.info("Model training completed with manual data.")
flash("Data successfully saved and model trained!", "success")
return redirect(url_for('generate_page'))
except Exception as e:
logging.error(f"Error processing manual data: {str(e)}")
flash(f"Error processing manual data: {str(e)}", "danger")
return redirect(url_for('manual_entry'))
@app.route('/generate_page')
def generate_page():
return render_template('generate.html')
@app.route('/generate', methods=['POST'])
def generate_data():
try:
num_rows = int(request.form['num_rows'])
if num_rows <= 0:
flash("Error: Number of rows must be greater than zero.", "danger")
return redirect(url_for('generate_page'))
model_path = os.path.join(OUTPUT_FOLDER, "ctgan_model.pkl")
if not os.path.exists(model_path):
flash("Error: Model file not found. Please train the model first.", "danger")
return redirect(url_for('generate_page'))
logging.info("Loading trained CTGAN model...")
model = CTGANSynthesizer.load(model_path)
logging.info(f"Generating {num_rows} rows of synthetic data...")
synthetic_data = model.sample(num_rows)
if synthetic_data.shape[0] == 0:
flash("Error: Generated data is empty. Try training again with a different dataset.", "danger")
return redirect(url_for('generate_page'))
output_file = os.path.join(OUTPUT_FOLDER, f"synthetic_data_{uuid.uuid4().hex}.csv")
synthetic_data.to_csv(output_file, index=False)
logging.info(f"Synthetic data generated successfully: {output_file}")
return send_file(output_file, as_attachment=True)
except Exception as e:
logging.error(f"Error generating data: {str(e)}")
flash(f"Error generating data: {str(e)}", "danger")
return redirect(url_for('generate_page'))
<<<<<<< HEAD
if __name__ == '__main__':
debug_mode = os.environ.get('FLASK_DEBUG', 'False').lower() == 'true'
app.run(debug=debug_mode)
=======
import os
if __name__ == "__main__":
port = int(os.environ.get("PORT", 10000)) # Use PORT variable from Render
app.run(host="0.0.0.0", port=port)
>>>>>>> 1d1a0353d57cb6780caedaf550502de0331866ea