Spaces:
Sleeping
Sleeping
Main py file with model calls
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from google import genai
|
3 |
+
from google.genai import types
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
import tempfile
|
7 |
+
import concurrent.futures
|
8 |
+
|
9 |
+
client = genai.Client(API_KEY)
|
10 |
+
PROMPT_VARIATIONS = [
|
11 |
+
"Generate a high quality image with a minimalist white background, bright natural light, and an open lid. Keep the product text unchanged.",
|
12 |
+
"Generate a high quality image of a beautiful woman who is using the product. The product and its text must remain exactly as in the original image.",
|
13 |
+
"Generate a high quality image from a slightly elevated angle, with a modern indoor setting, warm lighting, and an open lid. Ensure the product and product text is preserved.",
|
14 |
+
"Generate a high quality image with a low angle shot, cool ambient lighting, and a contemporary studio background, featuring an open lid. Keep the product text intact.",
|
15 |
+
"Generate a high quality image with a vibrant outdoor background, natural sunlight, and an open lid. The product text should remain the same.",
|
16 |
+
"Generate a high quality image with a subtle gradient background, soft diffused lighting, and an open lid. Ensure the product and product text match the original.",
|
17 |
+
"Generate a high quality image with an artistic abstract background, high contrast lighting, and an open lid. Maintain the original product text.",
|
18 |
+
"Generate a high quality image with a dynamic angle shot, colorful background, balanced lighting, and an open lid. Preserve the product text.",
|
19 |
+
"Generate a high quality image with a scenic nature background, golden hour lighting, and an open lid. Keep the product text unchanged.",
|
20 |
+
"Generate a high quality image where a beautiful woman is using the product. The product and its text must remain exactly as in the original image."
|
21 |
+
]
|
22 |
+
|
23 |
+
def process_variation(variation, input_image, product_name):
|
24 |
+
"""Process one variation: call the API, save the generated image to a temporary PNG file, and return its path."""
|
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, # Ensure this is the correct model identifier.
|
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 |
+
"""Generate images concurrently for the defined prompt variations (exactly 10 calls)."""
|
44 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
45 |
+
futures = [
|
46 |
+
executor.submit(process_variation, variation, input_image, product_name)
|
47 |
+
for variation in PROMPT_VARIATIONS
|
48 |
+
]
|
49 |
+
output_files = [future.result() for future in futures if future.result() is not None]
|
50 |
+
return output_files
|
51 |
+
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown("# Uni-Imaginator")
|
54 |
+
with gr.Row():
|
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 |
+
# The Gallery displays file paths so that each image downloads as a PNG file.
|
60 |
+
gallery = gr.Gallery(label="Generated Images", elem_id="gallery", columns=4, height="auto", object_fit="contain", interactive=True)
|
61 |
+
|
62 |
+
generate_button.click(fn=generate_images, inputs=[input_image, product_name], outputs=gallery)
|
63 |
+
|
64 |
+
demo.launch()
|