youngtsai commited on
Commit
d279d21
1 Parent(s): 6c5d60c

os.getenv("OPEN_AI_KEY")

Browse files
Files changed (3) hide show
  1. app.py +12 -2
  2. requirements.txt +7 -3
  3. storage_service.py +44 -0
app.py CHANGED
@@ -1,13 +1,18 @@
1
  import gradio as gr
2
- from google.cloud import storage
3
  import gspread
4
  from oauth2client.service_account import ServiceAccountCredentials
5
  from openai import OpenAI
6
  import os
7
  import time
 
 
 
 
 
 
8
 
9
  # 初始化API和認證
10
- OPEN_AI_KEY = config["OPEN_AI_KEY"]
11
  OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
12
 
13
  GCS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
@@ -20,6 +25,11 @@ scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/au
20
  creds = ServiceAccountCredentials.from_json_keyfile_name('your_google_sheets_credentials.json', scope)
21
  sheets_client = gspread.authorize(creds)
22
 
 
 
 
 
 
23
  # 函数定义
24
  def upload_image_to_gcs(image_file, bucket):
25
  # 为了避免文件名冲突,使用时间戳创建一个唯一的文件名
 
1
  import gradio as gr
 
2
  import gspread
3
  from oauth2client.service_account import ServiceAccountCredentials
4
  from openai import OpenAI
5
  import os
6
  import time
7
+ import json
8
+ from google.cloud import storage
9
+ from google.oauth2 import service_account
10
+ from googleapiclient.http import MediaIoBaseDownload
11
+ from storage_service import GoogleCloudStorage
12
+
13
 
14
  # 初始化API和認證
15
+ OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
16
  OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
17
 
18
  GCS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
 
25
  creds = ServiceAccountCredentials.from_json_keyfile_name('your_google_sheets_credentials.json', scope)
26
  sheets_client = gspread.authorize(creds)
27
 
28
+ GSHEET_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
29
+ GSHEET_KEY_DICT = json.loads(GSHEET_KEY)
30
+ sheets_client = gspread.service_account_from_dict(GSHEET_KEY_DICT)
31
+ sheet_url = "your_google_sheets_url"
32
+
33
  # 函数定义
34
  def upload_image_to_gcs(image_file, bucket):
35
  # 为了避免文件名冲突,使用时间戳创建一个唯一的文件名
requirements.txt CHANGED
@@ -1,5 +1,9 @@
1
  gradio
2
- google-cloud-storage
3
  gspread
4
- oauth2client
5
- openai
 
 
 
 
 
 
1
  gradio
 
2
  gspread
3
+ openai
4
+
5
+ google-auth
6
+ google-api-python-client
7
+ google-auth-httplib2
8
+ google-auth-oauthlib
9
+ google-cloud-storage
storage_service.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 download_as_string(self, bucket_name, source_blob_name):
29
+ blob = self.client.bucket(bucket_name).blob(source_blob_name)
30
+ return blob.download_as_text()
31
+
32
+ def make_blob_public(self, bucket_name, blob_name):
33
+ blob = self.client.bucket(bucket_name).blob(blob_name)
34
+ blob.make_public()
35
+ print(f"Blob {blob_name} is now publicly accessible at {blob.public_url}")
36
+
37
+ def get_public_url(self, bucket_name, blob_name):
38
+ blob = self.client.bucket(bucket_name).blob(blob_name)
39
+ return blob.public_url
40
+
41
+ def upload_image_and_get_public_url(self, bucket_name, file_name, file_path):
42
+ self.upload_file(bucket_name, file_name, file_path)
43
+ self.make_blob_public(bucket_name, file_name)
44
+ return self.get_public_url(bucket_name, file_name)