feat: time & status to data.jsonl
Browse files- GoogleDrive_API.py +30 -10
- Kaggle_API.py +12 -8
- app.py +4 -1
GoogleDrive_API.py
CHANGED
|
@@ -5,19 +5,39 @@ from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
|
|
| 5 |
|
| 6 |
|
| 7 |
class GoogleDrive_API:
|
| 8 |
-
def __init__(self):
|
| 9 |
self.SCOPES = ["https://www.googleapis.com/auth/drive"]
|
| 10 |
-
self.SERVICE_ACCOUNT_FILE = "service_account.json"
|
| 11 |
self.PARENT_FOLDER_ID = "1r-MlnEpWHx3b1fxHDnHcZ2-Wh_Y89676"
|
| 12 |
-
self.service = self.authenticate()
|
| 13 |
self.clear_files()
|
| 14 |
|
| 15 |
-
def authenticate(self):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
service = build("drive", "v3", credentials=credentials)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return service
|
| 22 |
|
| 23 |
def get_files(self):
|
|
@@ -40,7 +60,7 @@ class GoogleDrive_API:
|
|
| 40 |
file_id = item["id"]
|
| 41 |
self.service.files().delete(fileId=file_id).execute()
|
| 42 |
|
| 43 |
-
def upload_file(self, file_name, file_path):
|
| 44 |
file_metadata = {
|
| 45 |
"name": file_name,
|
| 46 |
"parents": [self.PARENT_FOLDER_ID],
|
|
@@ -53,7 +73,7 @@ class GoogleDrive_API:
|
|
| 53 |
)
|
| 54 |
print(rf"{file_path} uploaded. ID:", file.get("id"))
|
| 55 |
|
| 56 |
-
def download_file(self, file_name, file_path):
|
| 57 |
results = (
|
| 58 |
self.service.files()
|
| 59 |
.list(
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class GoogleDrive_API:
|
| 8 |
+
def __init__(self, SERVICE_ACCOUNT_FILE_path: str = ""):
|
| 9 |
self.SCOPES = ["https://www.googleapis.com/auth/drive"]
|
|
|
|
| 10 |
self.PARENT_FOLDER_ID = "1r-MlnEpWHx3b1fxHDnHcZ2-Wh_Y89676"
|
| 11 |
+
self.service = self.authenticate(SERVICE_ACCOUNT_FILE_path)
|
| 12 |
self.clear_files()
|
| 13 |
|
| 14 |
+
def authenticate(self, SERVICE_ACCOUNT_FILE):
|
| 15 |
+
if len(SERVICE_ACCOUNT_FILE) > 0:
|
| 16 |
+
if not os.path.exists(SERVICE_ACCOUNT_FILE):
|
| 17 |
+
raise rf"Google SERVICE_ACCOUNT_FILE Not Found, Path = {SERVICE_ACCOUNT_FILE}"
|
|
|
|
| 18 |
|
| 19 |
+
credentials = service_account.Credentials.from_service_account_file(
|
| 20 |
+
SERVICE_ACCOUNT_FILE, scopes=self.SCOPES
|
| 21 |
+
)
|
| 22 |
+
else:
|
| 23 |
+
service_account_info = {
|
| 24 |
+
"type": "service_account",
|
| 25 |
+
"project_id": os.environ["project_id"],
|
| 26 |
+
"private_key_id": os.environ["private_key_id"],
|
| 27 |
+
"private_key": os.environ["private_key"],
|
| 28 |
+
"client_email": os.environ["client_email"],
|
| 29 |
+
"client_id": os.environ["client_id"],
|
| 30 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 31 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
| 32 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
| 33 |
+
"client_x509_cert_url": os.environ["client_x509_cert_url"],
|
| 34 |
+
"universe_domain": "googleapis.com",
|
| 35 |
+
}
|
| 36 |
+
credentials = service_account.Credentials.from_service_account_info(
|
| 37 |
+
service_account_info, scopes=self.SCOPES
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
service = build("drive", "v3", credentials=credentials)
|
| 41 |
return service
|
| 42 |
|
| 43 |
def get_files(self):
|
|
|
|
| 60 |
file_id = item["id"]
|
| 61 |
self.service.files().delete(fileId=file_id).execute()
|
| 62 |
|
| 63 |
+
def upload_file(self, file_name: str, file_path: str):
|
| 64 |
file_metadata = {
|
| 65 |
"name": file_name,
|
| 66 |
"parents": [self.PARENT_FOLDER_ID],
|
|
|
|
| 73 |
)
|
| 74 |
print(rf"{file_path} uploaded. ID:", file.get("id"))
|
| 75 |
|
| 76 |
+
def download_file(self, file_name: str, file_path: str):
|
| 77 |
results = (
|
| 78 |
self.service.files()
|
| 79 |
.list(
|
Kaggle_API.py
CHANGED
|
@@ -3,16 +3,17 @@ import shutil
|
|
| 3 |
import subprocess
|
| 4 |
import json
|
| 5 |
import time
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
-
from GoogleDrive_API import GoogleDrive_API
|
| 9 |
-
|
| 10 |
|
| 11 |
class API_Connection:
|
| 12 |
-
def __init__(self, kaggle_username: str = "", kaggle_key=""):
|
| 13 |
os.environ["KAGGLE_USERNAME"] = kaggle_username
|
| 14 |
os.environ["KAGGLE_KEY"] = kaggle_key
|
| 15 |
|
|
|
|
|
|
|
| 16 |
self.PROJECT_PATH = r""
|
| 17 |
self.NOTEBOOK_ID = "amirmoris/pix2pix"
|
| 18 |
self.DATASET_NAME = "dataset"
|
|
@@ -94,8 +95,6 @@ class API_Connection:
|
|
| 94 |
if len(output_image_name) == 0:
|
| 95 |
return False, rf"Missing Output"
|
| 96 |
|
| 97 |
-
GoogleDrive_connection = GoogleDrive_API()
|
| 98 |
-
|
| 99 |
dataset_path = self.correct_path(rf"{self.PROJECT_PATH}\{self.DATASET_NAME}")
|
| 100 |
notebook_path = self.correct_path(rf"{self.PROJECT_PATH}\notebook")
|
| 101 |
|
|
@@ -109,6 +108,8 @@ class API_Connection:
|
|
| 109 |
|
| 110 |
data = [
|
| 111 |
{
|
|
|
|
|
|
|
| 112 |
"edit": edit_instruction,
|
| 113 |
"input_image_path": input_image_name,
|
| 114 |
"output_image_path": output_image_name,
|
|
@@ -117,10 +118,10 @@ class API_Connection:
|
|
| 117 |
|
| 118 |
self.write_file(data, dataset_path, "data.jsonl")
|
| 119 |
# update dataset
|
| 120 |
-
GoogleDrive_connection.upload_file(
|
| 121 |
"data.jsonl", rf"{self.DATASET_NAME}\data.jsonl"
|
| 122 |
)
|
| 123 |
-
GoogleDrive_connection.upload_file(
|
| 124 |
input_image_name, rf"{self.DATASET_NAME}\{input_image_name}"
|
| 125 |
)
|
| 126 |
|
|
@@ -142,7 +143,7 @@ class API_Connection:
|
|
| 142 |
time.sleep(120)
|
| 143 |
|
| 144 |
# get output
|
| 145 |
-
GoogleDrive_connection.download_file(
|
| 146 |
output_image_name, rf"{dataset_path}\{output_image_name}"
|
| 147 |
)
|
| 148 |
output_image = self.read_image(rf"{dataset_path}\{output_image_name}")
|
|
@@ -153,6 +154,9 @@ class API_Connection:
|
|
| 153 |
|
| 154 |
return True, output_image
|
| 155 |
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
def main():
|
| 158 |
pass
|
|
|
|
| 3 |
import subprocess
|
| 4 |
import json
|
| 5 |
import time
|
| 6 |
+
from datetime import datetime
|
| 7 |
from PIL import Image
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class API_Connection:
|
| 11 |
+
def __init__(self, GD_connection, kaggle_username: str = "", kaggle_key: str = ""):
|
| 12 |
os.environ["KAGGLE_USERNAME"] = kaggle_username
|
| 13 |
os.environ["KAGGLE_KEY"] = kaggle_key
|
| 14 |
|
| 15 |
+
self.GoogleDrive_connection = GD_connection
|
| 16 |
+
|
| 17 |
self.PROJECT_PATH = r""
|
| 18 |
self.NOTEBOOK_ID = "amirmoris/pix2pix"
|
| 19 |
self.DATASET_NAME = "dataset"
|
|
|
|
| 95 |
if len(output_image_name) == 0:
|
| 96 |
return False, rf"Missing Output"
|
| 97 |
|
|
|
|
|
|
|
| 98 |
dataset_path = self.correct_path(rf"{self.PROJECT_PATH}\{self.DATASET_NAME}")
|
| 99 |
notebook_path = self.correct_path(rf"{self.PROJECT_PATH}\notebook")
|
| 100 |
|
|
|
|
| 108 |
|
| 109 |
data = [
|
| 110 |
{
|
| 111 |
+
"status": "IDLE",
|
| 112 |
+
"time": self.get_current_time(),
|
| 113 |
"edit": edit_instruction,
|
| 114 |
"input_image_path": input_image_name,
|
| 115 |
"output_image_path": output_image_name,
|
|
|
|
| 118 |
|
| 119 |
self.write_file(data, dataset_path, "data.jsonl")
|
| 120 |
# update dataset
|
| 121 |
+
self.GoogleDrive_connection.upload_file(
|
| 122 |
"data.jsonl", rf"{self.DATASET_NAME}\data.jsonl"
|
| 123 |
)
|
| 124 |
+
self.GoogleDrive_connection.upload_file(
|
| 125 |
input_image_name, rf"{self.DATASET_NAME}\{input_image_name}"
|
| 126 |
)
|
| 127 |
|
|
|
|
| 143 |
time.sleep(120)
|
| 144 |
|
| 145 |
# get output
|
| 146 |
+
self.GoogleDrive_connection.download_file(
|
| 147 |
output_image_name, rf"{dataset_path}\{output_image_name}"
|
| 148 |
)
|
| 149 |
output_image = self.read_image(rf"{dataset_path}\{output_image_name}")
|
|
|
|
| 154 |
|
| 155 |
return True, output_image
|
| 156 |
|
| 157 |
+
def get_current_time(self):
|
| 158 |
+
return str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
| 159 |
+
|
| 160 |
|
| 161 |
def main():
|
| 162 |
pass
|
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from PIL import Image
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
from Kaggle_API import API_Connection
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
def generate_button_clicked(input_image: Image = None, edit_instruction: str = ""):
|
|
@@ -16,7 +17,9 @@ def generate_button_clicked(input_image: Image = None, edit_instruction: str = "
|
|
| 16 |
kaggle_username = os.environ["kaggle_username"]
|
| 17 |
kaggle_key = os.environ["kaggle_key"]
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
api_connection.create_folder(rf"{api_connection.PROJECT_PATH}\local_dataset")
|
| 21 |
|
| 22 |
image_ID = ""
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
from Kaggle_API import API_Connection
|
| 7 |
+
from GoogleDrive_API import GoogleDrive_API
|
| 8 |
|
| 9 |
|
| 10 |
def generate_button_clicked(input_image: Image = None, edit_instruction: str = ""):
|
|
|
|
| 17 |
kaggle_username = os.environ["kaggle_username"]
|
| 18 |
kaggle_key = os.environ["kaggle_key"]
|
| 19 |
|
| 20 |
+
GoogleDrive_connection = GoogleDrive_API()
|
| 21 |
+
api_connection = API_Connection(GoogleDrive_connection, kaggle_username, kaggle_key)
|
| 22 |
+
|
| 23 |
api_connection.create_folder(rf"{api_connection.PROJECT_PATH}\local_dataset")
|
| 24 |
|
| 25 |
image_ID = ""
|