File size: 3,567 Bytes
2cfbf68
8869031
2cfbf68
 
 
 
a9fe782
5b319d9
8557221
77770c5
ad90944
2cfbf68
 
a9fe782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cfbf68
a9fe782
2cfbf68
 
 
 
 
 
 
 
 
 
 
ebaba5e
2cfbf68
 
 
 
ebaba5e
2cfbf68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8869031
 
ebaba5e
 
 
 
 
 
 
8869031
 
 
 
 
 
 
 
5b319d9
8869031
2cfbf68
 
 
8869031
2cfbf68
 
 
 
 
 
 
 
ebaba5e
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
from fastapi import FastAPI, File, UploadFile, Form
from fastapi import APIRouter, Depends, HTTPException, status
import pandas as pd
from google.cloud import storage
import io
import os
import tempfile
from pydantic import BaseModel

app = FastAPI()

gcs_bucket_name = "ow-stu-us-ce1-ai-platform"

# process of getting credentials
def get_credentials():
    creds_json_str = os.getenv("BOB") # get json credentials stored as a string
    if creds_json_str is None:
        raise ValueError("GOOGLE_APPLICATION_CREDENTIALS_JSON not found in environment")

    # create a temporary file
    with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as temp:
        temp.write(creds_json_str) # write in json format
        temp_filename = temp.name 

    return temp_filename
    
# pass
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]= get_credentials()

# Ensure the GCS bucket exists
gcs_client = storage.Client()
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)
    if exists := blob.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).dropna(how='all')
    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(...)):
    try:
        df = pd.DataFrame([[category, score]], columns=['category', 'score'])
        append_to_gcs_csv(df, gcs_file_path)
        return {"message": "Data uploaded successfully"}
    except Exception as e:
        raise HTTPException(status_code=422, detail=str(e))
    
@app.post("/upload-data-raw/")
async def upload_data_raw(payload: dict):
    # sourcery skip: raise-from-previous-error
    try:
        category = payload['category']
        score = payload['score']
    except KeyError:
        raise HTTPException(status_code=400, detail="Invalid payload format")

    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"}