Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,75 @@
|
|
| 1 |
-
|
| 2 |
import os
|
| 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 |
-
Parameters:
|
| 17 |
-
image_path (str): Path to the input image.
|
| 18 |
-
text_prompt (str): Text prompt describing the edit.
|
| 19 |
-
Returns:
|
| 20 |
-
Image: The modified image.
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
if image is None or not text_input:
|
| 24 |
return "Please upload an image and provide an edit prompt.", None
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
for part in response.candidates[0].content.parts:
|
| 37 |
if part.inline_data is not None:
|
| 38 |
edited_image = Image.open(BytesIO(part.inline_data.data))
|
| 39 |
return "Here is your edited image:", edited_image
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
return "No image was generated. Try modifying your prompt.", None
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
# Gradio App
|
| 46 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 47 |
gr.Markdown("# ✨ AI-Powered Image Editor with Gemini 2.0 Flash Experimental")
|
| 48 |
gr.Markdown("Upload an image and describe the edits you want!")
|
| 49 |
-
|
| 50 |
with gr.Row():
|
| 51 |
image_input = gr.Image(type="pil", label="Upload Image")
|
| 52 |
text_input = gr.Textbox(placeholder="Describe your edit...", label="Edit Prompt")
|
| 53 |
-
|
|
|
|
| 54 |
output_text = gr.Textbox(label="Status", interactive=False)
|
| 55 |
output_image = gr.Image(label="Edited Image")
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
submit_btn = gr.Button("Generate Edit")
|
| 59 |
clear_btn = gr.Button("Clear")
|
|
|
|
| 60 |
text_input.submit(edit_image_with_gemini, [image_input, text_input], [output_text, output_image])
|
| 61 |
submit_btn.click(edit_image_with_gemini, [image_input, text_input], [output_text, output_image])
|
| 62 |
clear_btn.click(lambda: (None, None), None, [output_text, output_image])
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
# Launch the app
|
| 65 |
if __name__ == "__main__":
|
| 66 |
demo.launch(debug=True)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from google import genai
|
| 3 |
from google.genai import types
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
import gradio as gr
|
|
|
|
| 7 |
|
| 8 |
+
# Set up your API key
|
| 9 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 10 |
client = genai.Client(api_key=API_KEY)
|
| 11 |
|
| 12 |
def edit_image_with_gemini(image, text_input):
|
| 13 |
"""
|
| 14 |
Edits an image using Gemini 2.0 Flash Experimental API based on a given text prompt.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
|
|
|
| 16 |
if image is None or not text_input:
|
| 17 |
return "Please upload an image and provide an edit prompt.", None
|
| 18 |
|
| 19 |
+
# Wrap the text prompt as a tuple (as shown in the docs)
|
| 20 |
+
text_prompt_tuple = (text_input,)
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
response = client.models.generate_content(
|
| 24 |
+
model="gemini-2.0-flash-exp-image-generation",
|
| 25 |
+
contents=[text_prompt_tuple, image],
|
| 26 |
+
config=types.GenerateContentConfig(
|
| 27 |
+
response_modalities=['Text', 'Image']
|
| 28 |
+
)
|
| 29 |
)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"API call error: {e}", None
|
| 32 |
|
| 33 |
+
# Process the response: check for both image and text output
|
| 34 |
for part in response.candidates[0].content.parts:
|
| 35 |
if part.inline_data is not None:
|
| 36 |
edited_image = Image.open(BytesIO(part.inline_data.data))
|
| 37 |
return "Here is your edited image:", edited_image
|
| 38 |
+
elif part.text is not None:
|
| 39 |
+
# Optionally, print or log the text output
|
| 40 |
+
print("Text output:", part.text)
|
| 41 |
|
| 42 |
return "No image was generated. Try modifying your prompt.", None
|
| 43 |
|
| 44 |
+
def generate_thumbnail_prompt():
|
| 45 |
+
"""
|
| 46 |
+
Returns a predefined optimized prompt for creating a YouTube thumbnail.
|
| 47 |
+
"""
|
| 48 |
+
return ("Generate a bold, eye-catching YouTube thumbnail with vibrant colors, "
|
| 49 |
+
"large text, and a strong contrast. Make sure it stands out and is attention-grabbing.")
|
| 50 |
|
| 51 |
+
# Build the Gradio interface
|
|
|
|
| 52 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 53 |
gr.Markdown("# ✨ AI-Powered Image Editor with Gemini 2.0 Flash Experimental")
|
| 54 |
gr.Markdown("Upload an image and describe the edits you want!")
|
| 55 |
+
|
| 56 |
with gr.Row():
|
| 57 |
image_input = gr.Image(type="pil", label="Upload Image")
|
| 58 |
text_input = gr.Textbox(placeholder="Describe your edit...", label="Edit Prompt")
|
| 59 |
+
|
| 60 |
+
thumbnail_master_btn = gr.Button("🎨 Thumbnail Master")
|
| 61 |
output_text = gr.Textbox(label="Status", interactive=False)
|
| 62 |
output_image = gr.Image(label="Edited Image")
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
submit_btn = gr.Button("Generate Edit")
|
| 66 |
clear_btn = gr.Button("Clear")
|
| 67 |
+
|
| 68 |
text_input.submit(edit_image_with_gemini, [image_input, text_input], [output_text, output_image])
|
| 69 |
submit_btn.click(edit_image_with_gemini, [image_input, text_input], [output_text, output_image])
|
| 70 |
clear_btn.click(lambda: (None, None), None, [output_text, output_image])
|
| 71 |
+
# Use inputs=None since generate_thumbnail_prompt requires no input
|
| 72 |
+
thumbnail_master_btn.click(generate_thumbnail_prompt, inputs=None, outputs=text_input)
|
| 73 |
|
|
|
|
| 74 |
if __name__ == "__main__":
|
| 75 |
demo.launch(debug=True)
|