Update app.py
Browse files
app.py
CHANGED
@@ -95,11 +95,30 @@ def generate_synthetic_data(description, columns):
|
|
95 |
|
96 |
return response_data[0]["generated_text"]
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
def process_generated_data(csv_data, expected_columns):
|
99 |
try:
|
100 |
-
|
101 |
-
|
102 |
-
data = StringIO(cleaned_data)
|
103 |
|
104 |
# Read the CSV data
|
105 |
df = pd.read_csv(data, delimiter=',')
|
@@ -148,6 +167,7 @@ def generate_data(request: DataGenerationRequest):
|
|
148 |
headers={"Content-Disposition": "attachment; filename=generated_data.csv"}
|
149 |
)
|
150 |
|
|
|
151 |
@app.get("/")
|
152 |
def greet_json():
|
153 |
return {"Hello": "World!"}
|
|
|
95 |
|
96 |
return response_data[0]["generated_text"]
|
97 |
|
98 |
+
def extract_valid_csv(csv_data, expected_columns):
|
99 |
+
# Attempt to extract valid CSV data from the response
|
100 |
+
lines = csv_data.split('\n')
|
101 |
+
valid_lines = []
|
102 |
+
|
103 |
+
# Find the start of the valid CSV data by looking for the column headers
|
104 |
+
header_found = False
|
105 |
+
for line in lines:
|
106 |
+
if header_found:
|
107 |
+
if line.strip() == '':
|
108 |
+
continue
|
109 |
+
valid_lines.append(line)
|
110 |
+
elif set(line.split(',')) == set(expected_columns):
|
111 |
+
header_found = True
|
112 |
+
valid_lines.append(line)
|
113 |
+
|
114 |
+
# Combine valid lines into a single string
|
115 |
+
valid_csv_data = '\n'.join(valid_lines)
|
116 |
+
return valid_csv_data
|
117 |
+
|
118 |
def process_generated_data(csv_data, expected_columns):
|
119 |
try:
|
120 |
+
valid_csv_data = extract_valid_csv(csv_data, expected_columns)
|
121 |
+
data = StringIO(valid_csv_data)
|
|
|
122 |
|
123 |
# Read the CSV data
|
124 |
df = pd.read_csv(data, delimiter=',')
|
|
|
167 |
headers={"Content-Disposition": "attachment; filename=generated_data.csv"}
|
168 |
)
|
169 |
|
170 |
+
|
171 |
@app.get("/")
|
172 |
def greet_json():
|
173 |
return {"Hello": "World!"}
|