Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,9 @@ from io import BytesIO
|
|
| 6 |
from PIL import Image
|
| 7 |
from urllib.parse import urlparse, parse_qs
|
| 8 |
import google.generativeai as genai
|
|
|
|
| 9 |
|
| 10 |
-
# 全
|
| 11 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 12 |
|
| 13 |
if not GEMINI_API_KEY:
|
|
@@ -119,7 +120,6 @@ def generate_image(white_background_url, reference_image_url, prompt):
|
|
| 119 |
output_dir = "generated_images"
|
| 120 |
os.makedirs(output_dir, exist_ok=True)
|
| 121 |
# 使用時間戳或唯一ID來確保檔名唯一
|
| 122 |
-
import time
|
| 123 |
output_path = os.path.join(output_dir, f"generated_{int(time.time())}.png")
|
| 124 |
with open(output_path, "wb") as f:
|
| 125 |
f.write(generated_images_binary[0])
|
|
@@ -163,10 +163,8 @@ def process_sheet_data(sheet_url):
|
|
| 163 |
error_msg = f"Error: Google Sheet has only {df.shape[1]} columns, but 3 are expected (White Background URL, Reference Image URL, Prompt)."
|
| 164 |
raise gr.Error(error_msg)
|
| 165 |
|
| 166 |
-
# 這裡使用 to_dict('records') 來處理,以確保後續 Pandas 處理時的兼容性
|
| 167 |
-
data = df.to_dict('records')
|
| 168 |
data_list = []
|
| 169 |
-
for i, row in
|
| 170 |
if pd.notna(row.iloc[0]) and pd.notna(row.iloc[1]) and pd.notna(row.iloc[2]):
|
| 171 |
data_list.append([i, row.iloc[0], row.iloc[1], row.iloc[2]])
|
| 172 |
|
|
@@ -176,7 +174,6 @@ def process_sheet_data(sheet_url):
|
|
| 176 |
|
| 177 |
def generate_image_for_row(row_index, dataframe_data):
|
| 178 |
"""根據 Gradio DataFrame 的行索引生成圖片。"""
|
| 179 |
-
# 將 Gradio 的 dataframe_data 轉換回 pandas DataFrame
|
| 180 |
df = pd.DataFrame(dataframe_data, columns=["Index", "白背圖URL", "參考圖URL", "提示詞"])
|
| 181 |
|
| 182 |
if not (0 <= row_index < len(df)):
|
|
@@ -222,7 +219,6 @@ if __name__ == "__main__":
|
|
| 222 |
outputs=processed_df_state
|
| 223 |
)
|
| 224 |
|
| 225 |
-
# 修正 click 觸發器,確保傳入正確的參數
|
| 226 |
generate_selected_button.click(
|
| 227 |
fn=generate_image_for_row,
|
| 228 |
inputs=[row_index_input, output_dataframe],
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
from urllib.parse import urlparse, parse_qs
|
| 8 |
import google.generativeai as genai
|
| 9 |
+
import time
|
| 10 |
|
| 11 |
+
# 全域變數來儲存 API 金鑰
|
| 12 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 13 |
|
| 14 |
if not GEMINI_API_KEY:
|
|
|
|
| 120 |
output_dir = "generated_images"
|
| 121 |
os.makedirs(output_dir, exist_ok=True)
|
| 122 |
# 使用時間戳或唯一ID來確保檔名唯一
|
|
|
|
| 123 |
output_path = os.path.join(output_dir, f"generated_{int(time.time())}.png")
|
| 124 |
with open(output_path, "wb") as f:
|
| 125 |
f.write(generated_images_binary[0])
|
|
|
|
| 163 |
error_msg = f"Error: Google Sheet has only {df.shape[1]} columns, but 3 are expected (White Background URL, Reference Image URL, Prompt)."
|
| 164 |
raise gr.Error(error_msg)
|
| 165 |
|
|
|
|
|
|
|
| 166 |
data_list = []
|
| 167 |
+
for i, row in df.iterrows():
|
| 168 |
if pd.notna(row.iloc[0]) and pd.notna(row.iloc[1]) and pd.notna(row.iloc[2]):
|
| 169 |
data_list.append([i, row.iloc[0], row.iloc[1], row.iloc[2]])
|
| 170 |
|
|
|
|
| 174 |
|
| 175 |
def generate_image_for_row(row_index, dataframe_data):
|
| 176 |
"""根據 Gradio DataFrame 的行索引生成圖片。"""
|
|
|
|
| 177 |
df = pd.DataFrame(dataframe_data, columns=["Index", "白背圖URL", "參考圖URL", "提示詞"])
|
| 178 |
|
| 179 |
if not (0 <= row_index < len(df)):
|
|
|
|
| 219 |
outputs=processed_df_state
|
| 220 |
)
|
| 221 |
|
|
|
|
| 222 |
generate_selected_button.click(
|
| 223 |
fn=generate_image_for_row,
|
| 224 |
inputs=[row_index_input, output_dataframe],
|