Spaces:
Running
Running
Push_Code
Browse files
app.py
CHANGED
@@ -7,69 +7,80 @@ import tempfile
|
|
7 |
import concurrent.futures
|
8 |
import os
|
9 |
|
10 |
-
api_key = os.environ.get("API_KEY")
|
11 |
-
univin_model = os.environ.get("univin")
|
12 |
|
13 |
client = genai.Client(api_key=api_key)
|
14 |
|
15 |
PROMPT_VARIATIONS = [
|
16 |
# 1. Professional White Background (Universal)
|
17 |
-
"
|
18 |
|
19 |
# 2. Luxurious Spa Setting (Beauty, Skincare, Haircare)
|
20 |
-
"
|
21 |
|
22 |
-
# 3. Professional Model Holding Product (Skincare
|
23 |
-
"
|
24 |
|
25 |
-
# 4.
|
26 |
-
"
|
27 |
|
28 |
-
# 5.
|
29 |
-
"
|
30 |
|
31 |
-
# 6.
|
32 |
-
"
|
33 |
]
|
34 |
|
35 |
-
|
36 |
def process_variation(variation, input_image, product_name):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
response = client.models.generate_content(
|
42 |
-
model=univin_model,
|
43 |
-
contents=
|
44 |
-
config=types.GenerateContentConfig(response_modalities=['
|
45 |
)
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
generated_img.
|
51 |
-
|
|
|
|
|
52 |
return None
|
53 |
|
54 |
-
def generate_images(
|
55 |
-
|
|
|
|
|
56 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
57 |
futures = [
|
58 |
-
executor.submit(process_variation, variation,
|
59 |
for variation in PROMPT_VARIATIONS
|
60 |
]
|
61 |
output_files = [future.result() for future in futures if future.result() is not None]
|
|
|
62 |
return output_files
|
63 |
|
64 |
with gr.Blocks() as demo:
|
65 |
gr.Markdown("# Uni-Imaginator")
|
66 |
with gr.Row():
|
67 |
-
input_image = gr.Image(type="pil", label="Upload Image")
|
68 |
product_name = gr.Textbox(label="Product Name", placeholder="Enter the product name")
|
|
|
69 |
generate_button = gr.Button("Generate Images")
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
7 |
import concurrent.futures
|
8 |
import os
|
9 |
|
10 |
+
api_key = os.environ.get("API_KEY")
|
11 |
+
univin_model = os.environ.get("univin")
|
12 |
|
13 |
client = genai.Client(api_key=api_key)
|
14 |
|
15 |
PROMPT_VARIATIONS = [
|
16 |
# 1. Professional White Background (Universal)
|
17 |
+
"A high-resolution professional image of the product perfectly centered on a pure white seamless background, preserving exact shape, color, texture, printed text, and logo precisely as in the input image. Ideal for professional e-commerce.",
|
18 |
|
19 |
# 2. Luxurious Spa Setting (Beauty, Skincare, Haircare)
|
20 |
+
"A luxurious spa-themed image showcasing the original beauty or skincare product elegantly positioned on polished marble surrounded by spa accessories (stones, towels, plants). Preserve all original product details exactly as input.",
|
21 |
|
22 |
+
# 3. Professional Model Holding Product (Beauty/Skincare)
|
23 |
+
"Professional image of a sophisticated woman model holding the original beauty or skincare product gently and elegantly, focusing sharply on product details while maintaining precise original shape, color, texture, text, and logos.",
|
24 |
|
25 |
+
# 4. Modern Tech Scene (Personal Electronics)
|
26 |
+
"A sleek, modern technology-oriented image featuring the electronic product placed on a reflective black or dark-gray surface with subtle neon lighting, strictly maintaining original shape, color, texture, printed text, and logos.",
|
27 |
|
28 |
+
# 5. Lifestyle with Model (Candles/Soft Toys)
|
29 |
+
"Warm lifestyle image featuring either a woman professionally holding a lit candle or a child warmly hugging a soft toy. Ensure exact preservation of the original product's shape, color, texture, printed text, and logos as provided.",
|
30 |
|
31 |
+
# 6. Natural Refreshing Composition (Haircare & Skincare)
|
32 |
+
"Refreshing natural composition featuring the original skincare or haircare product among botanical elements and water droplets. Precisely preserve original product shape, color, texture, printed text, and logos from the input image."
|
33 |
]
|
34 |
|
|
|
35 |
def process_variation(variation, input_image, product_name):
|
36 |
+
prompt_text = f"Product Name: {product_name}\n{variation}"
|
37 |
+
|
38 |
+
request_content = [
|
39 |
+
types.Part(text=prompt_text),
|
40 |
+
types.Part(inline_data=types.Blob(mime_type="image/png", data=input_image.getvalue()))
|
41 |
+
]
|
42 |
+
|
43 |
response = client.models.generate_content(
|
44 |
+
model=univin_model,
|
45 |
+
contents=request_content,
|
46 |
+
config=types.GenerateContentConfig(response_modalities=['Image'])
|
47 |
)
|
48 |
+
|
49 |
+
if response.candidates:
|
50 |
+
for part in response.candidates[0].content.parts:
|
51 |
+
if part.inline_data:
|
52 |
+
generated_img = Image.open(BytesIO(part.inline_data.data))
|
53 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
54 |
+
generated_img.save(tmp_file, format="PNG")
|
55 |
+
return tmp_file.name
|
56 |
return None
|
57 |
|
58 |
+
def generate_images(input_pil_image, product_name):
|
59 |
+
input_buffer = BytesIO()
|
60 |
+
input_pil_image.save(input_buffer, format="PNG")
|
61 |
+
|
62 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
63 |
futures = [
|
64 |
+
executor.submit(process_variation, variation, input_buffer, product_name)
|
65 |
for variation in PROMPT_VARIATIONS
|
66 |
]
|
67 |
output_files = [future.result() for future in futures if future.result() is not None]
|
68 |
+
|
69 |
return output_files
|
70 |
|
71 |
with gr.Blocks() as demo:
|
72 |
gr.Markdown("# Uni-Imaginator")
|
73 |
with gr.Row():
|
74 |
+
input_image = gr.Image(type="pil", label="Upload Product Image")
|
75 |
product_name = gr.Textbox(label="Product Name", placeholder="Enter the product name")
|
76 |
+
|
77 |
generate_button = gr.Button("Generate Images")
|
78 |
+
gallery = gr.Gallery(label="Generated Images", columns=3, height="auto", interactive=True)
|
79 |
+
|
80 |
+
generate_button.click(
|
81 |
+
fn=generate_images,
|
82 |
+
inputs=[input_image, product_name],
|
83 |
+
outputs=gallery
|
84 |
+
)
|
85 |
+
|
86 |
+
demo.launch(debug=True)
|