Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,19 @@ import gspread
|
|
| 11 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 12 |
import requests
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# --- Google Sheets 相關函式 ---
|
| 18 |
def get_sheet_data(spreadsheet_url):
|
|
@@ -33,7 +44,7 @@ def get_sheet_data(spreadsheet_url):
|
|
| 33 |
headers = list_of_lists[0]
|
| 34 |
data = []
|
| 35 |
for i, row in enumerate(list_of_lists[1:], start=2):
|
| 36 |
-
if not any(row):
|
| 37 |
continue
|
| 38 |
row_dict = dict(zip(headers, row))
|
| 39 |
data.append({
|
|
@@ -85,7 +96,7 @@ def get_row_data(spreadsheet_url, row_number):
|
|
| 85 |
except Exception as e:
|
| 86 |
raise gr.Error(f"讀取指定行時發生錯誤: {e}", duration=10)
|
| 87 |
|
| 88 |
-
# --- 下載圖片函式
|
| 89 |
def load_image_from_url(url: str):
|
| 90 |
"""
|
| 91 |
從 URL 下載圖片並以 PIL Image 格式回傳,使用更穩健的請求標頭。
|
|
@@ -93,17 +104,16 @@ def load_image_from_url(url: str):
|
|
| 93 |
if not url:
|
| 94 |
return None
|
| 95 |
try:
|
| 96 |
-
# 使用更完整的標頭來模擬瀏覽器請求
|
| 97 |
headers = {
|
| 98 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
| 99 |
-
'Referer': 'https://www.google.com/',
|
| 100 |
'Accept-Encoding': 'gzip, deflate, br',
|
| 101 |
'Accept-Language': 'en-US,en;q=0.9',
|
| 102 |
'Connection': 'keep-alive',
|
| 103 |
}
|
| 104 |
|
| 105 |
response = requests.get(url, timeout=20, headers=headers)
|
| 106 |
-
response.raise_for_status()
|
| 107 |
|
| 108 |
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 109 |
print(f"Debug: Successfully loaded image from URL: {url}")
|
|
@@ -172,7 +182,6 @@ def generate_image_from_row(sheet_url, row_number, gemini_api_key):
|
|
| 172 |
|
| 173 |
images_for_gemini = []
|
| 174 |
|
| 175 |
-
# 使用新的函式
|
| 176 |
if white_back_url:
|
| 177 |
wb_img = load_image_from_url(white_back_url)
|
| 178 |
if wb_img:
|
|
|
|
| 11 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 12 |
import requests
|
| 13 |
|
| 14 |
+
# Hugging Face Spaces 上從 Secrets 讀取服務帳戶金鑰
|
| 15 |
+
try:
|
| 16 |
+
google_sheets_creds_json = os.environ.get("GOOGLE_SHEETS_SERVICE_ACCOUNT")
|
| 17 |
+
if not google_sheets_creds_json:
|
| 18 |
+
raise ValueError("GOOGLE_SHEETS_SERVICE_ACCOUNT environment variable is not set.")
|
| 19 |
+
|
| 20 |
+
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as temp_key_file:
|
| 21 |
+
temp_key_file.write(google_sheets_creds_json)
|
| 22 |
+
SERVICE_ACCOUNT_FILE = temp_key_file.name
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Failed to load Google Sheets credentials from environment variable: {e}")
|
| 25 |
+
# Fallback to local file for local testing
|
| 26 |
+
SERVICE_ACCOUNT_FILE = "service_account.json"
|
| 27 |
|
| 28 |
# --- Google Sheets 相關函式 ---
|
| 29 |
def get_sheet_data(spreadsheet_url):
|
|
|
|
| 44 |
headers = list_of_lists[0]
|
| 45 |
data = []
|
| 46 |
for i, row in enumerate(list_of_lists[1:], start=2):
|
| 47 |
+
if not any(row):
|
| 48 |
continue
|
| 49 |
row_dict = dict(zip(headers, row))
|
| 50 |
data.append({
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
raise gr.Error(f"讀取指定行時發生錯誤: {e}", duration=10)
|
| 98 |
|
| 99 |
+
# --- 下載圖片函式 ---
|
| 100 |
def load_image_from_url(url: str):
|
| 101 |
"""
|
| 102 |
從 URL 下載圖片並以 PIL Image 格式回傳,使用更穩健的請求標頭。
|
|
|
|
| 104 |
if not url:
|
| 105 |
return None
|
| 106 |
try:
|
|
|
|
| 107 |
headers = {
|
| 108 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
| 109 |
+
'Referer': 'https://www.google.com/',
|
| 110 |
'Accept-Encoding': 'gzip, deflate, br',
|
| 111 |
'Accept-Language': 'en-US,en;q=0.9',
|
| 112 |
'Connection': 'keep-alive',
|
| 113 |
}
|
| 114 |
|
| 115 |
response = requests.get(url, timeout=20, headers=headers)
|
| 116 |
+
response.raise_for_status()
|
| 117 |
|
| 118 |
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 119 |
print(f"Debug: Successfully loaded image from URL: {url}")
|
|
|
|
| 182 |
|
| 183 |
images_for_gemini = []
|
| 184 |
|
|
|
|
| 185 |
if white_back_url:
|
| 186 |
wb_img = load_image_from_url(white_back_url)
|
| 187 |
if wb_img:
|