Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,197 +1,194 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
-
import time
|
| 4 |
-
import uuid
|
| 5 |
-
import tempfile
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import gradio as gr
|
| 8 |
-
import base64
|
| 9 |
-
import mimetypes
|
| 10 |
-
from io import BytesIO
|
| 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 |
-
temp_path
|
| 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 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
image_path
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
"""
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
["data/1.
|
| 182 |
-
["data/
|
| 183 |
-
["data/
|
| 184 |
-
["data/
|
| 185 |
-
["data/
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
elem_id="examples-grid"
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
demo.queue(max_size=50).launch(mcp_server=True, share=True)
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
import uuid
|
| 5 |
+
import tempfile
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import base64
|
| 9 |
+
import mimetypes
|
| 10 |
+
from io import BytesIO
|
| 11 |
+
from google import genai
|
| 12 |
+
|
| 13 |
+
def generate(text, images, api_key=None, model="gemini-2.5-flash-image-preview"):
|
| 14 |
+
# Initialize client using provided api_key or fallback to environment variable
|
| 15 |
+
api_key = api_key.strip() if api_key and api_key.strip() != "" else os.environ.get("GEMINI_API_KEY", "AIzaSyDL5Rilo7ptJpUOZdY6wy8PJYUcVcnDADs")
|
| 16 |
+
client = genai.Client(api_key=api_key)
|
| 17 |
+
|
| 18 |
+
# Prepare contents with images first, then text
|
| 19 |
+
contents = images + [text]
|
| 20 |
+
|
| 21 |
+
response = client.models.generate_content(
|
| 22 |
+
model=model,
|
| 23 |
+
contents=contents,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
text_response = ""
|
| 27 |
+
image_path = None
|
| 28 |
+
|
| 29 |
+
for part in response.candidates[0].content.parts:
|
| 30 |
+
if part.text is not None:
|
| 31 |
+
text_response += part.text + "\n"
|
| 32 |
+
elif part.inline_data is not None:
|
| 33 |
+
# Create a temporary file to store the generated image
|
| 34 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 35 |
+
temp_path = tmp.name
|
| 36 |
+
generated_image = Image.open(BytesIO(part.inline_data.data))
|
| 37 |
+
generated_image.save(temp_path)
|
| 38 |
+
image_path = temp_path
|
| 39 |
+
print(f"Generated image saved to: {temp_path} with prompt: {text}")
|
| 40 |
+
|
| 41 |
+
return image_path, text_response
|
| 42 |
+
|
| 43 |
+
def load_uploaded_images(uploaded_files):
|
| 44 |
+
"""Load and display uploaded images immediately"""
|
| 45 |
+
uploaded_images = []
|
| 46 |
+
if uploaded_files:
|
| 47 |
+
for file in uploaded_files:
|
| 48 |
+
if file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 49 |
+
img = Image.open(file.name)
|
| 50 |
+
if img.mode == "RGBA":
|
| 51 |
+
img = img.convert("RGBA")
|
| 52 |
+
uploaded_images.append(img)
|
| 53 |
+
return uploaded_images
|
| 54 |
+
|
| 55 |
+
def process_image_and_prompt(uploaded_files, prompt, gemini_api_key):
|
| 56 |
+
try:
|
| 57 |
+
input_text = prompt
|
| 58 |
+
model = "gemini-2.5-flash-image-preview"
|
| 59 |
+
|
| 60 |
+
# Load images from uploaded files
|
| 61 |
+
images = []
|
| 62 |
+
uploaded_images = []
|
| 63 |
+
if uploaded_files:
|
| 64 |
+
for file in uploaded_files:
|
| 65 |
+
if file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 66 |
+
img = Image.open(file.name)
|
| 67 |
+
if img.mode == "RGBA":
|
| 68 |
+
img = img.convert("RGBA")
|
| 69 |
+
images.append(img)
|
| 70 |
+
uploaded_images.append(img)
|
| 71 |
+
|
| 72 |
+
if not images:
|
| 73 |
+
raise gr.Error("Please upload at least one image", duration=5)
|
| 74 |
+
|
| 75 |
+
# Call generate with images and prompt
|
| 76 |
+
image_path, text_response = generate(text=input_text, images=images, api_key=gemini_api_key, model=model)
|
| 77 |
+
|
| 78 |
+
if image_path:
|
| 79 |
+
# Load and convert the generated image if needed
|
| 80 |
+
result_img = Image.open(image_path)
|
| 81 |
+
if result_img.mode == "RGBA":
|
| 82 |
+
result_img = result_img.convert("RGBA")
|
| 83 |
+
return uploaded_images, [result_img], "" # Return uploaded images, generated image, and empty text output
|
| 84 |
+
else:
|
| 85 |
+
# Return uploaded images, no generated image, and the text response
|
| 86 |
+
return uploaded_images, None, text_response
|
| 87 |
+
except Exception as e:
|
| 88 |
+
raise gr.Error(f"Error: {e}", duration=5)
|
| 89 |
+
|
| 90 |
+
# Build a Blocks-based interface with a custom HTML header and CSS
|
| 91 |
+
with gr.Blocks(css="style.css") as demo:
|
| 92 |
+
# Custom HTML header with proper class for styling
|
| 93 |
+
gr.HTML(
|
| 94 |
+
"""
|
| 95 |
+
<div class="header-container">
|
| 96 |
+
<div>
|
| 97 |
+
<img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" alt="Gemini logo">
|
| 98 |
+
</div>
|
| 99 |
+
<div>
|
| 100 |
+
<h1>Gemini for Image Editing</h1>
|
| 101 |
+
<p>Powered by <a href="https://gradio.app/">Gradio</a> |
|
| 102 |
+
<a href="https://huggingface.co/spaces/ameerazam08/Gemini-Image-Edit?duplicate=true">Duplicate</a> this Repo |
|
| 103 |
+
<a href="https://aistudio.google.com/apikey">Get an API Key</a> |
|
| 104 |
+
Follow me on Twitter: <a href="https://x.com/Ameerazam18">Ameerazam18</a></p>
|
| 105 |
+
</div>
|
| 106 |
+
</div>
|
| 107 |
+
"""
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
with gr.Accordion("API Configuration", open=False, elem_classes="config-accordion"):
|
| 111 |
+
gr.Markdown("""
|
| 112 |
+
- **Issue:** Sometimes the model returns text instead of an image.
|
| 113 |
+
### Steps to Address:
|
| 114 |
+
1. **Duplicate the Repository**
|
| 115 |
+
- Create a separate copy for modifications.
|
| 116 |
+
2. **Use Your Own Gemini API Key**
|
| 117 |
+
- You **must** configure your own Gemini key for generation!
|
| 118 |
+
""")
|
| 119 |
+
|
| 120 |
+
with gr.Accordion("Usage Instructions", open=False, elem_classes="instructions-accordion"):
|
| 121 |
+
gr.Markdown("""
|
| 122 |
+
### Usage
|
| 123 |
+
- Upload an image and enter a prompt to generate outputs.
|
| 124 |
+
- If text is returned instead of an image, it will appear in the text output.
|
| 125 |
+
- Upload only PNG, JPG, JPEG, or WEBP images.
|
| 126 |
+
- **Do not use NSFW images!**
|
| 127 |
+
""")
|
| 128 |
+
|
| 129 |
+
with gr.Row(elem_classes="main-content"):
|
| 130 |
+
with gr.Column(elem_classes="input-column"):
|
| 131 |
+
image_input = gr.File(
|
| 132 |
+
file_types=["image"],
|
| 133 |
+
file_count="multiple",
|
| 134 |
+
label="Upload Images",
|
| 135 |
+
elem_id="image-input",
|
| 136 |
+
elem_classes="upload-box"
|
| 137 |
+
)
|
| 138 |
+
gemini_api_key = gr.Textbox(
|
| 139 |
+
lines=1,
|
| 140 |
+
placeholder="Enter Gemini API Key (optional)",
|
| 141 |
+
label="Gemini API Key (optional)",
|
| 142 |
+
elem_classes="api-key-input"
|
| 143 |
+
)
|
| 144 |
+
prompt_input = gr.Textbox(
|
| 145 |
+
lines=2,
|
| 146 |
+
placeholder="Enter prompt here...",
|
| 147 |
+
label="Prompt",
|
| 148 |
+
elem_classes="prompt-input"
|
| 149 |
+
)
|
| 150 |
+
submit_btn = gr.Button("Generate", elem_classes="generate-btn")
|
| 151 |
+
|
| 152 |
+
with gr.Column(elem_classes="output-column"):
|
| 153 |
+
uploaded_gallery = gr.Gallery(label="Uploaded Images", elem_classes="uploaded-gallery")
|
| 154 |
+
output_gallery = gr.Gallery(label="Generated Outputs", elem_classes="output-gallery")
|
| 155 |
+
output_text = gr.Textbox(
|
| 156 |
+
label="Gemini Output",
|
| 157 |
+
placeholder="Text response will appear here if no image is generated.",
|
| 158 |
+
elem_classes="output-text"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
# Set up the interaction with three outputs
|
| 162 |
+
submit_btn.click(
|
| 163 |
+
fn=process_image_and_prompt,
|
| 164 |
+
inputs=[image_input, prompt_input, gemini_api_key],
|
| 165 |
+
outputs=[uploaded_gallery, output_gallery, output_text],
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
# Update uploaded gallery immediately when files are uploaded
|
| 169 |
+
image_input.upload(
|
| 170 |
+
fn=load_uploaded_images,
|
| 171 |
+
inputs=[image_input],
|
| 172 |
+
outputs=[uploaded_gallery],
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
gr.Markdown("## Try these examples", elem_classes="gr-examples-header")
|
| 176 |
+
|
| 177 |
+
examples = [
|
| 178 |
+
["data/1.webp", 'change text to "AMEER"'],
|
| 179 |
+
["data/2.webp", "remove the spoon from hand only"],
|
| 180 |
+
["data/3.webp", 'change text to "Make it "'],
|
| 181 |
+
["data/1.jpg", "add joker style only on face"],
|
| 182 |
+
["data/1777043.jpg", "add joker style only on face"],
|
| 183 |
+
["data/2807615.jpg", "add lipstick on lip only"],
|
| 184 |
+
["data/76860.jpg", "add lipstick on lip only"],
|
| 185 |
+
["data/2807615.jpg", "make it happy looking face only"],
|
| 186 |
+
]
|
| 187 |
+
|
| 188 |
+
gr.Examples(
|
| 189 |
+
examples=examples,
|
| 190 |
+
inputs=[image_input, prompt_input],
|
| 191 |
+
elem_id="examples-grid"
|
| 192 |
+
)
|
| 193 |
+
|
|
|
|
|
|
|
|
|
|
| 194 |
demo.queue(max_size=50).launch(mcp_server=True, share=True)
|