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