mlAPI / main.py
deepak6593's picture
adding files
7b4ca7a
raw
history blame
2.46 kB
from fastapi import FastAPI, File, UploadFile, Form
import pandas as pd
from google.cloud import storage
import io
import os
app = FastAPI()
# GCS credentials
gcs_credentials_file = "ow-stu-us-ce1-dev.json"
gcs_bucket_name = "ow-stu-us-ce1-ai-platform"
# Ensure the GCS bucket exists
gcs_client = storage.Client.from_service_account_json(gcs_credentials_file)
gcs_bucket = gcs_client.bucket(gcs_bucket_name)
# File path in GCS bucket
gcs_file_path = "deepak_6593/db.csv"
def append_to_gcs_csv(new_data, gcs_file_path):
# Standardize column names for new data
new_data.columns = ['category', 'score']
# Check if the file exists in GCS bucket
blob = gcs_bucket.blob(gcs_file_path)
exists = blob.exists()
# If file exists, read it into a DataFrame
if exists:
existing_data = pd.read_csv(io.BytesIO(blob.download_as_bytes()))
# Ensure existing data has the right columns
existing_data = existing_data[['category', 'score']]
# Append new data to existing data
combined_data = pd.concat([existing_data, new_data], ignore_index=True)
else:
combined_data = new_data
# Convert combined DataFrame to CSV and upload it
csv_data = combined_data.to_csv(index=False).encode('utf-8')
blob.upload_from_string(csv_data, content_type='text/csv')
def read_from_gcs_csv(gcs_file_path):
blob = gcs_bucket.blob(gcs_file_path)
return pd.read_csv(io.BytesIO(blob.download_as_text()))
@app.post("/upload-file/")
async def upload_file(file: UploadFile = File(...)):
df = pd.read_csv(io.StringIO((await file.read()).decode('utf-8')))
append_to_gcs_csv(df, gcs_file_path)
return {"message": "File uploaded successfully"}
@app.post("/upload-data/")
async def upload_data(category: str = Form(...), score: float = Form(...)):
df = pd.DataFrame([[category, score]], columns=['category', 'score'])
append_to_gcs_csv(df, gcs_file_path)
return {"message": "Data uploaded successfully"}
@app.post("/clear-data/")
async def clear_data():
# Create an empty DataFrame with the same columns
empty_df = pd.DataFrame(columns=['category', 'score'])
# Convert the empty DataFrame to CSV
csv_data = empty_df.to_csv(index=False).encode('utf-8')
# Overwrite the existing file in GCS with the empty CSV data
gcs_bucket.blob(gcs_file_path).upload_from_string(csv_data, content_type='text/csv')
return {"message": "Data cleared successfully"}