Vnmrsharma commited on
Commit
e7f35d3
·
verified ·
1 Parent(s): 78b0ad6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -9,45 +9,40 @@ 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
- text_input = (
37
- f"Hi, this is a picture of a product. The name of the product is {product_name}.",
38
- variation
39
- )
 
 
40
  response = client.models.generate_content(
41
  model=univin_model,
42
- contents=[text_input, input_image],
43
- config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])
 
 
 
44
  )
45
- for part in response.candidates[0].content.parts:
46
- if part.inline_data is not None:
47
- generated_img = Image.open(BytesIO(part.inline_data.data))
48
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
49
- generated_img.save(tmp_file, format="PNG")
50
- return tmp_file.name
 
 
51
  return None
52
 
53
  def generate_images(input_image, product_name):
@@ -70,4 +65,4 @@ with gr.Blocks() as demo:
70
 
71
  generate_button.click(fn=generate_images, inputs=[input_image, product_name], outputs=gallery)
72
 
73
- demo.launch(debug=True)
 
9
 
10
  api_key = os.environ.get("API_KEY")
11
  univin_model = os.environ.get("univin")
 
12
  client = genai.Client(api_key=api_key)
13
 
14
  PROMPT_VARIATIONS = [
15
+ "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.",
 
 
 
16
  "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.",
 
 
17
  "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.",
 
 
18
  "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.",
 
 
19
  "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.",
 
 
20
  "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."
21
  ]
22
 
23
  def process_variation(variation, input_image, product_name):
24
+ buffered = BytesIO()
25
+ input_image.save(buffered, format="PNG")
26
+ image_bytes = buffered.getvalue()
27
+
28
+ prompt_text = f"This is a product named '{product_name}'. {variation}"
29
+
30
  response = client.models.generate_content(
31
  model=univin_model,
32
+ contents=[
33
+ types.Part(text=prompt_text),
34
+ types.Part(inline_data=types.Blob(mime_type="image/png", data=image_bytes))
35
+ ],
36
+ config=types.GenerateContentConfig(response_modalities=['Image'])
37
  )
38
+
39
+ if response.candidates:
40
+ for part in response.candidates[0].content.parts:
41
+ if part.inline_data:
42
+ generated_img = Image.open(BytesIO(part.inline_data.data))
43
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
44
+ generated_img.save(tmp_file, format="PNG")
45
+ return tmp_file.name
46
  return None
47
 
48
  def generate_images(input_image, product_name):
 
65
 
66
  generate_button.click(fn=generate_images, inputs=[input_image, product_name], outputs=gallery)
67
 
68
+ demo.launch(debug=True)