Spaces:
Running
Running
v0
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from google.colab import userdata
|
3 |
+
from google import genai
|
4 |
+
from google.genai import types
|
5 |
+
from PIL import Image
|
6 |
+
from io import BytesIO
|
7 |
+
import gradio as gr
|
8 |
+
import PIL.Image
|
9 |
+
|
10 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
11 |
+
client = genai.Client(api_key=API_KEY)
|
12 |
+
|
13 |
+
def edit_image_with_gemini(image, text_input):
|
14 |
+
"""
|
15 |
+
Edits an image using Gemini 2.0 Flash Experimental API based on a given text prompt.
|
16 |
+
|
17 |
+
Parameters:
|
18 |
+
image_path (str): Path to the input image.
|
19 |
+
text_prompt (str): Text prompt describing the edit.
|
20 |
+
|
21 |
+
Returns:
|
22 |
+
Image: The modified image.
|
23 |
+
"""
|
24 |
+
|
25 |
+
if image is None or not text_input:
|
26 |
+
return "Please upload an image and provide an edit prompt.", None
|
27 |
+
|
28 |
+
# Generate content with Gemini API
|
29 |
+
response = client.models.generate_content(
|
30 |
+
model="gemini-2.0-flash-exp-image-generation",
|
31 |
+
contents=[text_input, image],
|
32 |
+
config=types.GenerateContentConfig(
|
33 |
+
response_modalities=['Text', 'Image']
|
34 |
+
)
|
35 |
+
)
|
36 |
+
|
37 |
+
# Extract and display the generated image
|
38 |
+
for part in response.candidates[0].content.parts:
|
39 |
+
if part.inline_data is not None:
|
40 |
+
edited_image = Image.open(BytesIO(part.inline_data.data))
|
41 |
+
return "Here is your edited image:", edited_image
|
42 |
+
|
43 |
+
return "No image was generated. Try modifying your prompt.", None
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
# Gradio App
|
48 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
49 |
+
gr.Markdown("# ✨ AI-Powered Image Editor with Gemini 2.0 Flash Experimental")
|
50 |
+
gr.Markdown("Upload an image and describe the edits you want!")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
54 |
+
text_input = gr.Textbox(placeholder="Describe your edit...", label="Edit Prompt")
|
55 |
+
|
56 |
+
output_text = gr.Textbox(label="Status", interactive=False)
|
57 |
+
output_image = gr.Image(label="Edited Image")
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
submit_btn = gr.Button("Generate Edit")
|
61 |
+
clear_btn = gr.Button("Clear")
|
62 |
+
text_input.submit(edit_image_with_gemini, [image_input, text_input], [output_text, output_image])
|
63 |
+
submit_btn.click(edit_image_with_gemini, [image_input, text_input], [output_text, output_image])
|
64 |
+
clear_btn.click(lambda: (None, None), None, [output_text, output_image])
|
65 |
+
|
66 |
+
# Launch the app
|
67 |
+
if __name__ == "__main__":
|
68 |
+
demo.launch(debug=True)
|