Vnmrsharma commited on
Commit
b8dbac5
·
verified ·
1 Parent(s): 9b2a490
Files changed (1) hide show
  1. app.py +47 -36
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
- "Generate a crisp, professional studio-quality image of the original product placed precisely centered against a pure, seamless white background. Ensure the lighting is neutral, diffused, and evenly distributed to clearly accentuate all original details, including exact shape, precise color reproduction, authentic texture, printing clarity, and visibility of text and logos exactly as provided in the input image. Maintain the highest resolution for immediate use on a professional e-commerce website.",
18
 
19
  # 2. Luxurious Spa Setting (Beauty, Skincare, Haircare)
20
- "Create an elegant, luxurious scene featuring the original beauty, skincare, or haircare product placed gently atop a polished marble surface in a high-end spa environment. Surround with subtle, natural spa accessories (e.g., stones, plants, or towels) slightly blurred to keep the product in sharp focus. Employ soft, natural lighting that highlights the product's original shape, authentic color, rich textures, and precise logos and printed text without alteration. Convey a serene, premium aesthetic ideal for upscale beauty websites.",
21
 
22
- # 3. Professional Model Holding Product (Skincare, Beauty)
23
- "Produce a sophisticated, professional image showing a confident woman model gently holding the original skincare or beauty product in her hands. Frame the shot to focus primarily on the product, clearly displaying the model’s well-groomed hands and subtle styling without overshadowing the product. Ensure every original aspect of the product—shape, exact color, texture, and printed text or logos—is strictly preserved as in the original image. Opt for soft, flattering studio lighting, evoking elegance and authenticity suitable for premium skincare or beauty product pages.",
24
 
25
- # 4. Sleek, Modern Tech Scene (Personal-use Electronics)
26
- "Render an ultra-modern, technology-inspired scene showcasing the original personal-use electronic product on a sleek, reflective black or dark-gray surface. Include subtle ambient backlighting or minimal neon accents, creating a sophisticated, high-tech atmosphere. Precisely maintain the product’s original shape, exact color, texture details, and clearly readable text and logos exactly as in the original image. The aesthetic should evoke precision, innovation, and premium quality, perfect for professional electronics presentations online.",
27
 
28
- # 5. Flat-lay with minimal styling
29
- "Craft a warm, inviting lifestyle image featuring the original candle or soft toy professionally held by a model in a natural yet sophisticated home environment. If Candle: A woman's hands gently holding the lit candle, illuminating softly textured cozy fabrics or home décor elements around her. If Soft Toy: A smiling child warmly hugging or gently holding the soft toy in comfortable surroundings such as a bedroom or living space, subtly blurred in the background. Ensure meticulous preservation of the products original shape, accurate color, texture fidelity, and intact printed text and logos without any modification. The image should communicate warmth, authenticity, and premium comfort suited for a lifestyle-oriented web presentation.",
30
 
31
- # 6. High-contrast studio art shot
32
- "Develop a vibrant, refreshing composition featuring the original haircare or skincare product prominently placed among fresh botanical elements (such as leaves, flowers, or gentle water droplets), set on a reflective or softly textured neutral surface. Incorporate bright, clean lighting to accentuate freshness and purity. Strictly retain the product’s original details—shape, exact colors, textures, clearly legible printed text, and logos exactly as presented. The final image should reflect a natural, premium aesthetic appealing directly to consumers seeking high-quality, nature-inspired personal care products."
33
  ]
34
 
35
-
36
  def process_variation(variation, input_image, product_name):
37
- text_input = (
38
- f"Hi, this is a picture of a product. The name of the product is {product_name}.",
39
- variation
40
- )
 
 
 
41
  response = client.models.generate_content(
42
- model=univin_model,
43
- contents=[text_input, input_image],
44
- config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])
45
  )
46
- for part in response.candidates[0].content.parts:
47
- if part.inline_data is not None:
48
- generated_img = Image.open(BytesIO(part.inline_data.data))
49
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
50
- generated_img.save(tmp_file, format="PNG")
51
- return tmp_file.name
 
 
52
  return None
53
 
54
- def generate_images(input_image, product_name):
55
-
 
 
56
  with concurrent.futures.ThreadPoolExecutor() as executor:
57
  futures = [
58
- executor.submit(process_variation, variation, input_image, product_name)
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
- gallery = gr.Gallery(label="Generated Images", elem_id="gallery", columns=4, height="auto", object_fit="contain", interactive=True)
72
-
73
- generate_button.click(fn=generate_images, inputs=[input_image, product_name], outputs=gallery)
74
-
75
- demo.launch()
 
 
 
 
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)