import json import os import time import uuid import tempfile from PIL import Image, ImageDraw, ImageFont import gradio as gr import base64 import mimetypes from io import BytesIO from google import genai from google.genai import types def generate(text, images, api_key, model="gemini-2.5-flash-image-preview"): # Initialize client using provided api_key (or fallback to env variable) client = genai.Client(api_key=(api_key.strip() if api_key and api_key.strip() != "" else os.environ.get("GEMINI_API_KEY"))) # Prepare contents with images first, then text contents = images + [text] response = client.models.generate_content( model=model, contents=contents, ) text_response = "" image_path = None for part in response.candidates[0].content.parts: if part.text is not None: text_response += part.text + "\n" elif part.inline_data is not None: # Create a temporary file to store the generated image with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp: temp_path = tmp.name generated_image = Image.open(BytesIO(part.inline_data.data)) generated_image.save(temp_path) image_path = temp_path print(f"Generated image saved to: {temp_path} with prompt: {text}") return image_path, text_response def load_uploaded_images(uploaded_files): """Load and display uploaded images immediately""" uploaded_images = [] if uploaded_files: for file in uploaded_files: if file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')): img = Image.open(file.name) if img.mode == "RGBA": img = img.convert("RGBA") uploaded_images.append(img) return uploaded_images def process_image_and_prompt(uploaded_files, prompt, gemini_api_key): try: input_text = prompt model = "gemini-2.5-flash-image-preview" # Load images from uploaded files images = [] uploaded_images = [] if uploaded_files: for file in uploaded_files: if file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')): img = Image.open(file.name) if img.mode == "RGBA": img = img.convert("RGBA") images.append(img) uploaded_images.append(img) if not images: raise gr.Error("Please upload at least one image", duration=5) # Format: [dress_image, model_image, text_input] or [image1, image2, ..., text_input] image_path, text_response = generate(text=input_text, images=images, api_key=gemini_api_key, model=model) if image_path: # Load and convert the image if needed. result_img = Image.open(image_path) if result_img.mode == "RGBA": result_img = result_img.convert("RGBA") return uploaded_images, [result_img], "" # Return uploaded images, generated image, and empty text output. else: # Return uploaded images, no generated image, and the text response. return uploaded_images, None, text_response except Exception as e: raise gr.Error(f"Error Getting {e}", duration=5) # Build a Blocks-based interface with a custom HTML header and CSS with gr.Blocks(css_paths="style.css",) as demo: # Custom HTML header with proper class for styling gr.HTML( """
Powered by Gradio⚡️| Duplicate this Repo | Get an API Key | Follow me on Twitter: Ameerazam18