Kims12 commited on
Commit
9a736d4
ยท
verified ยท
1 Parent(s): fd7e6af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -18
app.py CHANGED
@@ -5,7 +5,7 @@ import tempfile
5
  from PIL import Image
6
  import gradio as gr
7
  import logging
8
- from io import BytesIO
9
 
10
  from google import genai
11
  from google.genai import types
@@ -27,6 +27,12 @@ def save_binary_file(file_name, data):
27
  logger.debug(f"ํŒŒ์ผ ์ €์žฅ ์™„๋ฃŒ: {file_name}")
28
 
29
 
 
 
 
 
 
 
30
  def merge_images(person_img_path, product_img_path, background_img_path, prompt, model="gemini-2.0-flash-exp-image-generation"):
31
  logger.debug(f"merge_images ํ•จ์ˆ˜ ์‹œ์ž‘ - ํ”„๋กฌํ”„ํŠธ: '{prompt}'")
32
 
@@ -42,37 +48,50 @@ def merge_images(person_img_path, product_img_path, background_img_path, prompt,
42
  client = genai.Client(api_key=effective_api_key)
43
  logger.debug("Gemini ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์™„๋ฃŒ.")
44
 
45
- # PIL ์ด๋ฏธ์ง€ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
46
- person_img = Image.open(person_img_path)
47
- product_img = Image.open(product_img_path)
 
 
 
48
 
49
  # ์ปจํ…์ธ  ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
50
- contents = []
51
 
52
  # ์‚ฌ๋žŒ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€
53
- contents.append(
54
- types.Part.from_data(data=person_img, mime_type="image/jpeg")
 
 
 
55
  )
56
 
57
  # ์ƒํ’ˆ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€
58
- contents.append(
59
- types.Part.from_data(data=product_img, mime_type="image/jpeg")
 
 
 
60
  )
61
 
62
  # ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์œผ๋ฉด ์ถ”๊ฐ€
63
  if background_img_path:
64
- background_img = Image.open(background_img_path)
65
- contents.append(
66
- types.Part.from_data(data=background_img, mime_type="image/jpeg")
 
 
 
 
67
  )
68
  logger.debug("๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€๋จ")
69
 
70
  # ๋งˆ์ง€๋ง‰์— ํ”„๋กฌํ”„ํŠธ ์ถ”๊ฐ€
71
- contents.append(
72
  types.Part.from_text(text=prompt)
73
  )
74
 
75
- logger.debug(f"์ปจํ…์ธ  ๊ฐ์ฒด ์ƒ์„ฑ ์™„๋ฃŒ: {len(contents)} ์•„์ดํ…œ")
76
 
77
  # ์ƒ์„ฑ ์„ค์ •
78
  generate_content_config = types.GenerateContentConfig(
@@ -88,13 +107,13 @@ def merge_images(person_img_path, product_img_path, background_img_path, prompt,
88
  temp_path = tmp.name
89
  logger.debug(f"์ž„์‹œ ํŒŒ์ผ ์ƒ์„ฑ๋จ: {temp_path}")
90
 
91
- # ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ˆ˜์‹  (์›๋ž˜ ์ฝ”๋“œ ๋ฐฉ์‹์œผ๋กœ ๋ณต์›)
92
  response_stream = client.models.generate_content_stream(
93
  model=model,
94
  contents=[
95
  types.Content(
96
  role="user",
97
- parts=contents,
98
  ),
99
  ],
100
  config=generate_content_config,
@@ -110,11 +129,11 @@ def merge_images(person_img_path, product_img_path, background_img_path, prompt,
110
  logger.debug(f"chunk ์ˆ˜์‹ : {chunk}")
111
 
112
  # ์‘๋‹ต ๊ฒ€์ฆ
113
- if not hasattr(chunk, 'candidates') or not chunk.candidates:
114
  logger.warning("chunk์— candidates๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
115
  continue
116
 
117
- if not hasattr(chunk.candidates[0], 'content') or not chunk.candidates[0].content:
118
  logger.warning("chunk.candidates[0]์— content๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
119
  continue
120
 
 
5
  from PIL import Image
6
  import gradio as gr
7
  import logging
8
+ import io
9
 
10
  from google import genai
11
  from google.genai import types
 
27
  logger.debug(f"ํŒŒ์ผ ์ €์žฅ ์™„๋ฃŒ: {file_name}")
28
 
29
 
30
+ def image_to_bytes(img_path):
31
+ """ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ์ด๋ฏธ์ง€ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ"""
32
+ with open(img_path, "rb") as f:
33
+ return f.read()
34
+
35
+
36
  def merge_images(person_img_path, product_img_path, background_img_path, prompt, model="gemini-2.0-flash-exp-image-generation"):
37
  logger.debug(f"merge_images ํ•จ์ˆ˜ ์‹œ์ž‘ - ํ”„๋กฌํ”„ํŠธ: '{prompt}'")
38
 
 
48
  client = genai.Client(api_key=effective_api_key)
49
  logger.debug("Gemini ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์™„๋ฃŒ.")
50
 
51
+ # ์ด๋ฏธ์ง€ ํŒŒ์ผ ์—…๋กœ๋“œ
52
+ person_file = client.files.upload(file=person_img_path)
53
+ logger.debug(f"์‚ฌ๋žŒ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์™„๋ฃŒ: {person_file.uri}")
54
+
55
+ product_file = client.files.upload(file=product_img_path)
56
+ logger.debug(f"์ƒํ’ˆ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์™„๋ฃŒ: {product_file.uri}")
57
 
58
  # ์ปจํ…์ธ  ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
59
+ parts = []
60
 
61
  # ์‚ฌ๋žŒ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€
62
+ parts.append(
63
+ types.Part.from_uri(
64
+ file_uri=person_file.uri,
65
+ mime_type=person_file.mime_type
66
+ )
67
  )
68
 
69
  # ์ƒํ’ˆ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€
70
+ parts.append(
71
+ types.Part.from_uri(
72
+ file_uri=product_file.uri,
73
+ mime_type=product_file.mime_type
74
+ )
75
  )
76
 
77
  # ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์œผ๋ฉด ์ถ”๊ฐ€
78
  if background_img_path:
79
+ background_file = client.files.upload(file=background_img_path)
80
+ logger.debug(f"๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์™„๋ฃŒ: {background_file.uri}")
81
+ parts.append(
82
+ types.Part.from_uri(
83
+ file_uri=background_file.uri,
84
+ mime_type=background_file.mime_type
85
+ )
86
  )
87
  logger.debug("๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€๋จ")
88
 
89
  # ๋งˆ์ง€๋ง‰์— ํ”„๋กฌํ”„ํŠธ ์ถ”๊ฐ€
90
+ parts.append(
91
  types.Part.from_text(text=prompt)
92
  )
93
 
94
+ logger.debug(f"์ปจํ…์ธ  ๊ฐ์ฒด ์ƒ์„ฑ ์™„๋ฃŒ: {len(parts)} ์•„์ดํ…œ")
95
 
96
  # ์ƒ์„ฑ ์„ค์ •
97
  generate_content_config = types.GenerateContentConfig(
 
107
  temp_path = tmp.name
108
  logger.debug(f"์ž„์‹œ ํŒŒ์ผ ์ƒ์„ฑ๋จ: {temp_path}")
109
 
110
+ # ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ˆ˜์‹ 
111
  response_stream = client.models.generate_content_stream(
112
  model=model,
113
  contents=[
114
  types.Content(
115
  role="user",
116
+ parts=parts,
117
  ),
118
  ],
119
  config=generate_content_config,
 
129
  logger.debug(f"chunk ์ˆ˜์‹ : {chunk}")
130
 
131
  # ์‘๋‹ต ๊ฒ€์ฆ
132
+ if not hasattr(chunk, 'candidates') or not chunk.candidates or len(chunk.candidates) == 0:
133
  logger.warning("chunk์— candidates๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
134
  continue
135
 
136
+ if not hasattr(chunk.candidates[0], 'content') or chunk.candidates[0].content is None:
137
  logger.warning("chunk.candidates[0]์— content๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
138
  continue
139