File size: 2,382 Bytes
5f95fb3
bb7dffb
 
 
 
 
506b623
bb7dffb
 
 
 
 
 
 
506b623
3c5850d
 
506b623
3c5850d
bb7dffb
 
 
 
3c5850d
 
 
506b623
 
 
3c5850d
506b623
 
bb7dffb
506b623
bb7dffb
506b623
bb7dffb
 
 
 
 
 
 
 
506b623
bb7dffb
 
 
506b623
bb7dffb
 
 
506b623
bb7dffb
 
 
 
 
 
 
 
 
16a8a60
506b623
bb7dffb
bcc6b54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import gradio as gr
import PIL.Image

API_KEY = os.getenv("GOOGLE_API_KEY")
client = genai.Client(api_key=API_KEY)

def edit_image_with_gemini(image, text_input):
    """
    Edits an image using Gemini 2.0 Flash Experimental API based on a given text prompt.
    Parameters:
        image (PIL.Image): The input image.
        text_input (str): Text prompt describing the edit.
    Returns:
        A tuple of status message and the modified image (or None).
    """
    if image is None or not text_input:
        return "Please upload an image and provide an edit prompt.", None

    # Wrap text_input in a tuple to match the expected format
    contents = [(text_input,), image]

    # Generate content with Gemini API
    response = client.models.generate_content(
        model="gemini-2.0-flash-exp-image-generation",
        contents=contents,
        config=types.GenerateContentConfig(
            response_modalities=['Text', 'Image']
        )
    )

    # Extract and display the generated image
    for part in response.candidates[0].content.parts:
        if part.inline_data is not None:
            edited_image = Image.open(BytesIO(part.inline_data.data))
            return "Here is your edited image:", edited_image

    return "No image was generated. Try modifying your prompt.", None


# Gradio App
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# ✨ AI-Powered Image Editor with Gemini 2.0 Flash Experimental")
    gr.Markdown("Upload an image and describe the edits you want!")

    with gr.Row():
        image_input = gr.Image(type="pil", label="Upload Image")
        text_input = gr.Textbox(placeholder="Describe your edit...", label="Edit Prompt")

    output_text = gr.Textbox(label="Status", interactive=False)
    output_image = gr.Image(label="Edited Image")

    with gr.Row():
        submit_btn = gr.Button("Generate Edit")
        clear_btn = gr.Button("Clear")
    text_input.submit(edit_image_with_gemini, [image_input, text_input], [output_text, output_image])
    submit_btn.click(edit_image_with_gemini, [image_input, text_input], [output_text, output_image])
    clear_btn.click(lambda: (None, None), None, [output_text, output_image])

# Launch the app
if __name__ == "__main__":
    demo.launch(debug=True)