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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -7,8 +7,9 @@ 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
  client = genai.Client(api_key=api_key)
13
 
14
  PROMPT_VARIATIONS = [
@@ -21,31 +22,25 @@ PROMPT_VARIATIONS = [
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):
 
49
  with concurrent.futures.ThreadPoolExecutor() as executor:
50
  futures = [
51
  executor.submit(process_variation, variation, input_image, product_name)
@@ -60,9 +55,9 @@ with gr.Blocks() as demo:
60
  input_image = gr.Image(type="pil", label="Upload Image")
61
  product_name = gr.Textbox(label="Product Name", placeholder="Enter the product name")
62
  generate_button = gr.Button("Generate Images")
63
-
64
  gallery = gr.Gallery(label="Generated Images", elem_id="gallery", columns=4, height="auto", object_fit="contain", interactive=True)
65
-
66
  generate_button.click(fn=generate_images, inputs=[input_image, product_name], outputs=gallery)
67
-
68
- demo.launch(debug=True)
 
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 = [
 
22
  ]
23
 
24
  def process_variation(variation, input_image, product_name):
25
+ text_input = (
26
+ f"Hi, this is a picture of a product. The name of the product is {product_name}.",
27
+ variation
28
+ )
 
 
29
  response = client.models.generate_content(
30
+ model=univin_model,
31
+ contents=[text_input, input_image],
32
+ config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])
 
 
 
33
  )
34
+ for part in response.candidates[0].content.parts:
35
+ if part.inline_data is not None:
36
+ generated_img = Image.open(BytesIO(part.inline_data.data))
37
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
38
+ generated_img.save(tmp_file, format="PNG")
39
+ return tmp_file.name
 
 
40
  return None
41
 
42
  def generate_images(input_image, product_name):
43
+
44
  with concurrent.futures.ThreadPoolExecutor() as executor:
45
  futures = [
46
  executor.submit(process_variation, variation, input_image, product_name)
 
55
  input_image = gr.Image(type="pil", label="Upload Image")
56
  product_name = gr.Textbox(label="Product Name", placeholder="Enter the product name")
57
  generate_button = gr.Button("Generate Images")
58
+
59
  gallery = gr.Gallery(label="Generated Images", elem_id="gallery", columns=4, height="auto", object_fit="contain", interactive=True)
60
+
61
  generate_button.click(fn=generate_images, inputs=[input_image, product_name], outputs=gallery)
62
+
63
+ demo.launch()