Spaces:
Sleeping
Sleeping
Commit
·
ebaba5e
1
Parent(s):
8869031
adding files
Browse files
main.py
CHANGED
|
@@ -40,15 +40,12 @@ def append_to_gcs_csv(new_data, gcs_file_path):
|
|
| 40 |
|
| 41 |
# Check if the file exists in GCS bucket
|
| 42 |
blob = gcs_bucket.blob(gcs_file_path)
|
| 43 |
-
exists
|
| 44 |
-
|
| 45 |
-
# If file exists, read it into a DataFrame
|
| 46 |
-
if exists:
|
| 47 |
existing_data = pd.read_csv(io.BytesIO(blob.download_as_bytes()))
|
| 48 |
# Ensure existing data has the right columns
|
| 49 |
existing_data = existing_data[['category', 'score']]
|
| 50 |
# Append new data to existing data
|
| 51 |
-
combined_data = pd.concat([existing_data, new_data], ignore_index=True)
|
| 52 |
else:
|
| 53 |
combined_data = new_data
|
| 54 |
|
|
@@ -68,10 +65,13 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 68 |
|
| 69 |
@app.post("/upload-data/")
|
| 70 |
async def upload_data(category: str = Form(...), score: float = Form(...)):
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
@app.post("/upload-data-raw/")
|
| 76 |
async def upload_data_raw(payload: dict):
|
| 77 |
# sourcery skip: raise-from-previous-error
|
|
@@ -94,4 +94,4 @@ async def clear_data():
|
|
| 94 |
csv_data = empty_df.to_csv(index=False).encode('utf-8')
|
| 95 |
# Overwrite the existing file in GCS with the empty CSV data
|
| 96 |
gcs_bucket.blob(gcs_file_path).upload_from_string(csv_data, content_type='text/csv')
|
| 97 |
-
return {"message": "Data cleared successfully"}
|
|
|
|
| 40 |
|
| 41 |
# Check if the file exists in GCS bucket
|
| 42 |
blob = gcs_bucket.blob(gcs_file_path)
|
| 43 |
+
if exists := blob.exists():
|
|
|
|
|
|
|
|
|
|
| 44 |
existing_data = pd.read_csv(io.BytesIO(blob.download_as_bytes()))
|
| 45 |
# Ensure existing data has the right columns
|
| 46 |
existing_data = existing_data[['category', 'score']]
|
| 47 |
# Append new data to existing data
|
| 48 |
+
combined_data = pd.concat([existing_data, new_data], ignore_index=True).dropna(how='all')
|
| 49 |
else:
|
| 50 |
combined_data = new_data
|
| 51 |
|
|
|
|
| 65 |
|
| 66 |
@app.post("/upload-data/")
|
| 67 |
async def upload_data(category: str = Form(...), score: float = Form(...)):
|
| 68 |
+
try:
|
| 69 |
+
df = pd.DataFrame([[category, score]], columns=['category', 'score'])
|
| 70 |
+
append_to_gcs_csv(df, gcs_file_path)
|
| 71 |
+
return {"message": "Data uploaded successfully"}
|
| 72 |
+
except Exception as e:
|
| 73 |
+
raise HTTPException(status_code=422, detail=str(e))
|
| 74 |
+
|
| 75 |
@app.post("/upload-data-raw/")
|
| 76 |
async def upload_data_raw(payload: dict):
|
| 77 |
# sourcery skip: raise-from-previous-error
|
|
|
|
| 94 |
csv_data = empty_df.to_csv(index=False).encode('utf-8')
|
| 95 |
# Overwrite the existing file in GCS with the empty CSV data
|
| 96 |
gcs_bucket.blob(gcs_file_path).upload_from_string(csv_data, content_type='text/csv')
|
| 97 |
+
return {"message": "Data cleared successfully"}
|