Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -33,54 +33,41 @@ PROMPT_VARIATIONS = [
|
|
33 |
]
|
34 |
|
35 |
def process_variation(variation, input_image, product_name):
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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=
|
46 |
-
config=types.GenerateContentConfig(response_modalities=['Image'])
|
47 |
)
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
generated_img =
|
53 |
-
|
54 |
-
generated_img.save(tmp_file, format="PNG")
|
55 |
-
return tmp_file.name
|
56 |
return None
|
57 |
|
58 |
-
def generate_images(
|
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,
|
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
|
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 |
-
|
81 |
-
|
82 |
-
|
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)
|