Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
-
from fastapi.responses import StreamingResponse
|
3 |
from pydantic import BaseModel
|
4 |
import pandas as pd
|
5 |
import os
|
@@ -87,7 +87,7 @@ generation_params = {
|
|
87 |
def generate_synthetic_data(description, columns):
|
88 |
formatted_prompt = format_prompt(description, columns)
|
89 |
payload = {"inputs": formatted_prompt, "parameters": generation_params}
|
90 |
-
response = requests.post(API_URL, headers={"Authorization": f"Bearer {
|
91 |
return response.json()[0]["generated_text"]
|
92 |
|
93 |
def process_generated_data(csv_data, expected_columns):
|
@@ -127,26 +127,31 @@ def generate_large_synthetic_data(description, columns, num_rows=1000, rows_per_
|
|
127 |
print("No valid data frames to concatenate.")
|
128 |
return pd.DataFrame(columns=columns)
|
129 |
|
|
|
|
|
|
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
columns = data.get('columns')
|
137 |
-
num_rows = data.get('num_rows', 1000)
|
138 |
-
|
139 |
-
if not description or not columns:
|
140 |
-
return jsonify({"error": "Please provide 'description' and 'columns' in the request."}), 400
|
141 |
|
142 |
-
|
|
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.responses import StreamingResponse
|
3 |
from pydantic import BaseModel
|
4 |
import pandas as pd
|
5 |
import os
|
|
|
87 |
def generate_synthetic_data(description, columns):
|
88 |
formatted_prompt = format_prompt(description, columns)
|
89 |
payload = {"inputs": formatted_prompt, "parameters": generation_params}
|
90 |
+
response = requests.post(API_URL, headers={"Authorization": f"Bearer {hf_token}"}, json=payload)
|
91 |
return response.json()[0]["generated_text"]
|
92 |
|
93 |
def process_generated_data(csv_data, expected_columns):
|
|
|
127 |
print("No valid data frames to concatenate.")
|
128 |
return pd.DataFrame(columns=columns)
|
129 |
|
130 |
+
class DataGenerationRequest(BaseModel):
|
131 |
+
description: str
|
132 |
+
columns: list[str]
|
133 |
|
134 |
+
@app.post("/generate/")
|
135 |
+
def generate_data(request: DataGenerationRequest):
|
136 |
+
description = request.description.strip()
|
137 |
+
columns = [col.strip() for col in request.columns]
|
138 |
+
csv_data = generate_large_synthetic_data(description, columns, num_rows=1000, rows_per_generation=100)
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
+
if csv_data.empty:
|
141 |
+
return JSONResponse(content={"error": "No valid data generated"}, status_code=500)
|
142 |
|
143 |
+
# Convert the DataFrame to CSV format
|
144 |
+
csv_buffer = StringIO()
|
145 |
+
csv_data.to_csv(csv_buffer, index=False)
|
146 |
+
csv_buffer.seek(0)
|
147 |
+
|
148 |
+
# Return the CSV data as a downloadable file
|
149 |
+
return StreamingResponse(
|
150 |
+
csv_buffer,
|
151 |
+
media_type="text/csv",
|
152 |
+
headers={"Content-Disposition": "attachment; filename=generated_data.csv"}
|
153 |
+
)
|
154 |
+
|
155 |
+
@app.get("/")
|
156 |
+
def greet_json():
|
157 |
+
return {"Hello": "World!"}
|