gcs
Browse files- app.py +7 -0
- config/config.py +22 -0
- storage_service.py +68 -0
app.py
CHANGED
@@ -3,6 +3,13 @@ import os
|
|
3 |
import shutil
|
4 |
import tempfile
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def mock_question_answer(question, history):
|
7 |
# εθ³ζ樑ζ¬εη
|
8 |
answers = {
|
|
|
3 |
import shutil
|
4 |
import tempfile
|
5 |
|
6 |
+
from openai import OpenAI
|
7 |
+
|
8 |
+
from storage_service import GoogleCloudStorage
|
9 |
+
from google.oauth2.service_account import Credentials
|
10 |
+
import vertexai
|
11 |
+
from vertexai.generative_models import GenerativeModel, Part
|
12 |
+
|
13 |
def mock_question_answer(question, history):
|
14 |
# εθ³ζ樑ζ¬εη
|
15 |
answers = {
|
config/config.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# KEY CONFIG
|
6 |
+
is_env_local = os.getenv("IS_ENV_LOCAL", "false") == "true"
|
7 |
+
print(f"is_env_local: {is_env_local}")
|
8 |
+
|
9 |
+
print("===gr__version__===")
|
10 |
+
print(gr.__version__)
|
11 |
+
|
12 |
+
if is_env_local:
|
13 |
+
with open("local_config.json") as f:
|
14 |
+
config = json.load(f)
|
15 |
+
IS_ENV_PROD = "False"
|
16 |
+
PASSWORD = config["PASSWORD"]
|
17 |
+
GOOGLE_CREDENTIALS_KEY = json.dumps(config["GOOGLE_APPLICATION_CREDENTIALS_JSON"])
|
18 |
+
|
19 |
+
else:
|
20 |
+
IS_ENV_PROD = os.getenv("IS_ENV_PROD", "False")
|
21 |
+
PASSWORD = os.getenv("PASSWORD")
|
22 |
+
GOOGLE_CREDENTIALS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
storage_service.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from google.cloud import storage
|
3 |
+
from google.oauth2 import service_account
|
4 |
+
from googleapiclient.http import MediaIoBaseDownload
|
5 |
+
|
6 |
+
|
7 |
+
class GoogleCloudStorage:
|
8 |
+
def __init__(self, service_account_key_string):
|
9 |
+
credentials_dict = json.loads(service_account_key_string)
|
10 |
+
credentials = service_account.Credentials.from_service_account_info(credentials_dict)
|
11 |
+
self.client = storage.Client(credentials=credentials, project=credentials_dict['project_id'])
|
12 |
+
|
13 |
+
def check_file_exists(self, bucket_name, file_name):
|
14 |
+
blob = self.client.bucket(bucket_name).blob(file_name)
|
15 |
+
return blob.exists()
|
16 |
+
|
17 |
+
def upload_file(self, bucket_name, destination_blob_name, file_path):
|
18 |
+
blob = self.client.bucket(bucket_name).blob(destination_blob_name)
|
19 |
+
blob.upload_from_filename(file_path)
|
20 |
+
print(f"File {file_path} uploaded to {destination_blob_name} in GCS.")
|
21 |
+
|
22 |
+
def upload_file_as_string(self, bucket_name, destination_blob_name, content):
|
23 |
+
blob = self.client.bucket(bucket_name).blob(destination_blob_name)
|
24 |
+
blob.upload_from_string(content)
|
25 |
+
print(f"String content uploaded to {destination_blob_name} in GCS.")
|
26 |
+
return None
|
27 |
+
|
28 |
+
def upload_json_string(self, bucket_name, destination_blob_name, json_data):
|
29 |
+
"""Uploads a JSON string to a specified GCS bucket."""
|
30 |
+
blob = self.client.bucket(bucket_name).blob(destination_blob_name)
|
31 |
+
blob.upload_from_string(json_data, content_type='application/json')
|
32 |
+
print(f"JSON string uploaded to {destination_blob_name} in GCS.")
|
33 |
+
|
34 |
+
def download_as_string(self, bucket_name, source_blob_name):
|
35 |
+
blob = self.client.bucket(bucket_name).blob(source_blob_name)
|
36 |
+
return blob.download_as_text()
|
37 |
+
|
38 |
+
def make_blob_public(self, bucket_name, blob_name):
|
39 |
+
blob = self.client.bucket(bucket_name).blob(blob_name)
|
40 |
+
blob.make_public()
|
41 |
+
print(f"Blob {blob_name} is now publicly accessible at {blob.public_url}")
|
42 |
+
|
43 |
+
def get_public_url(self, bucket_name, blob_name):
|
44 |
+
blob = self.client.bucket(bucket_name).blob(blob_name)
|
45 |
+
return blob.public_url
|
46 |
+
|
47 |
+
def upload_image_and_get_public_url(self, bucket_name, file_name, file_path):
|
48 |
+
self.upload_file(bucket_name, file_name, file_path)
|
49 |
+
self.make_blob_public(bucket_name, file_name)
|
50 |
+
return self.get_public_url(bucket_name, file_name)
|
51 |
+
|
52 |
+
def delete_blob(self, bucket_name, blob_name):
|
53 |
+
blob = self.client.bucket(bucket_name).blob(blob_name)
|
54 |
+
blob.delete()
|
55 |
+
print(f"Blob {blob_name} deleted from {bucket_name}.")
|
56 |
+
|
57 |
+
def list_blobs(self, bucket_name, prefix):
|
58 |
+
blobs = self.client.list_blobs(bucket_name, prefix=prefix)
|
59 |
+
return [blob.name for blob in blobs]
|
60 |
+
|
61 |
+
def delete_blobs_by_folder_name(self, bucket_name, folder_name):
|
62 |
+
bucket = self.client.bucket(bucket_name)
|
63 |
+
blobs = list(bucket.list_blobs(prefix=folder_name))
|
64 |
+
if blobs:
|
65 |
+
bucket.delete_blobs(blobs)
|
66 |
+
print(f"ζζδ»₯ {folder_name} ιι ηζͺζ‘ε·²ζΉιεͺι€")
|
67 |
+
else:
|
68 |
+
print(f"ζͺζΎε°δ»₯ {folder_name} ιι ηζͺζ‘")
|