Spaces:
Sleeping
Sleeping
Commit
·
bd2139a
1
Parent(s):
ebaba5e
new code
Browse files
main.py
CHANGED
|
@@ -1,14 +1,19 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from google.cloud import storage
|
| 5 |
-
import io
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
| 7 |
import tempfile
|
| 8 |
-
from
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
gcs_bucket_name = "ow-stu-us-ce1-ai-platform"
|
| 13 |
|
| 14 |
# process of getting credentials
|
|
@@ -34,64 +39,45 @@ gcs_bucket = gcs_client.bucket(gcs_bucket_name)
|
|
| 34 |
# File path in GCS bucket
|
| 35 |
gcs_file_path = "deepak_6593/db.csv"
|
| 36 |
|
| 37 |
-
def append_to_gcs_csv(new_data, gcs_file_path):
|
| 38 |
-
# Standardize column names for new data
|
| 39 |
-
new_data.columns = ['category', 'score']
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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 |
-
|
| 52 |
-
# Convert combined DataFrame to CSV and upload it
|
| 53 |
-
csv_data = combined_data.to_csv(index=False).encode('utf-8')
|
| 54 |
-
blob.upload_from_string(csv_data, content_type='text/csv')
|
| 55 |
-
|
| 56 |
-
def read_from_gcs_csv(gcs_file_path):
|
| 57 |
blob = gcs_bucket.blob(gcs_file_path)
|
| 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 |
-
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"}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
+
import csv
|
|
|
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
+
from threading import Thread
|
| 5 |
+
import time
|
| 6 |
+
import pandas as pd
|
| 7 |
import tempfile
|
| 8 |
+
from google.cloud import storage
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
data_location = None
|
| 13 |
+
reading_thread = None
|
| 14 |
+
stop_reading = False
|
| 15 |
+
counter = 0
|
| 16 |
+
|
| 17 |
gcs_bucket_name = "ow-stu-us-ce1-ai-platform"
|
| 18 |
|
| 19 |
# process of getting credentials
|
|
|
|
| 39 |
# File path in GCS bucket
|
| 40 |
gcs_file_path = "deepak_6593/db.csv"
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
def read_csv(location="chennai"):
|
| 44 |
+
global stop_reading
|
| 45 |
+
global data_location
|
| 46 |
+
global counter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
blob = gcs_bucket.blob(gcs_file_path)
|
| 48 |
+
while not stop_reading:
|
| 49 |
+
file_path = f"https://storage.googleapis.com/dev.openweaver.com/demo/ai/{location}.csv"
|
| 50 |
+
data = pd.read_csv(file_path)
|
| 51 |
+
csv_data = data[0:counter].to_csv(index=False).encode('utf-8')
|
| 52 |
+
blob.upload_from_string(csv_data, content_type='text/csv')
|
| 53 |
+
time.sleep(5)
|
| 54 |
+
counter += 1
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def start_reading(location):
|
| 58 |
+
global reading_thread
|
| 59 |
+
reading_thread = Thread(target=read_csv, args=(location,))
|
| 60 |
+
reading_thread.start()
|
| 61 |
+
|
| 62 |
+
def stop_reading_thread():
|
| 63 |
+
global stop_reading
|
| 64 |
+
global reading_thread
|
| 65 |
+
stop_reading = True
|
| 66 |
+
if reading_thread is not None:
|
| 67 |
+
reading_thread.join()
|
| 68 |
+
|
| 69 |
+
@app.get("/update_location/")
|
| 70 |
+
async def update_location(location: str = Query("chennai")):
|
| 71 |
+
global reading_thread
|
| 72 |
+
global stop_reading
|
| 73 |
+
|
| 74 |
+
if location not in ["chennai", "hyderabad"]:
|
| 75 |
+
return {"error": "Invalid location"}
|
| 76 |
+
|
| 77 |
+
if reading_thread is not None and reading_thread.is_alive():
|
| 78 |
+
stop_reading_thread()
|
| 79 |
+
|
| 80 |
+
stop_reading = False
|
| 81 |
+
start_reading(location)
|
| 82 |
+
|
| 83 |
+
return {"message": f"Location updated to {location}"}
|
|
|
|
|
|
|
|
|
|
|
|