Spaces:
Running
Running
| 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_path (str): Path to the input image. | |
| text_prompt (str): Text prompt describing the edit. | |
| Returns: | |
| Image: The modified image. | |
| """ | |
| if image is None or not text_input: | |
| return "Please upload an image and provide an edit prompt.", None | |
| # Generate content with Gemini API | |
| response = client.models.generate_content( | |
| model="gemini-2.0-flash-exp-image-generation", | |
| contents=[text_input, image], | |
| 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) | |