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