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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -31
app.py CHANGED
@@ -33,54 +33,41 @@ PROMPT_VARIATIONS = [
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)
 
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):
 
 
 
54
  with concurrent.futures.ThreadPoolExecutor() as executor:
55
  futures = [
56
+ executor.submit(process_variation, variation, input_image, product_name)
57
  for variation in PROMPT_VARIATIONS
58
  ]
59
  output_files = [future.result() for future in futures if future.result() is not None]
 
60
  return output_files
61
 
62
  with gr.Blocks() as demo:
63
  gr.Markdown("# Uni-Imaginator")
64
  with gr.Row():
65
+ input_image = gr.Image(type="pil", label="Upload Image")
66
  product_name = gr.Textbox(label="Product Name", placeholder="Enter the product name")
 
67
  generate_button = gr.Button("Generate Images")
 
68
 
69
+ gallery = gr.Gallery(label="Generated Images", elem_id="gallery", columns=4, height="auto", object_fit="contain", interactive=True)
70
+
71
+ generate_button.click(fn=generate_images, inputs=[input_image, product_name], outputs=gallery)
 
 
72
 
73
  demo.launch(debug=True)