Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ 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
|
|
@@ -20,10 +20,8 @@ def generate(text, file_name, api_key, model="gemini-2.0-flash-exp"):
|
|
| 20 |
client = genai.Client(api_key=(api_key.strip() if api_key and api_key.strip() != ""
|
| 21 |
else os.environ.get("GEMINI_API_KEY")))
|
| 22 |
|
| 23 |
-
files = [
|
| 24 |
-
|
| 25 |
-
]
|
| 26 |
-
|
| 27 |
contents = [
|
| 28 |
types.Content(
|
| 29 |
role="user",
|
|
@@ -41,13 +39,13 @@ def generate(text, file_name, api_key, model="gemini-2.0-flash-exp"):
|
|
| 41 |
top_p=0.95,
|
| 42 |
top_k=40,
|
| 43 |
max_output_tokens=8192,
|
| 44 |
-
response_modalities=[
|
| 45 |
-
"image",
|
| 46 |
-
"text",
|
| 47 |
-
],
|
| 48 |
response_mime_type="text/plain",
|
| 49 |
)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 52 |
temp_path = tmp.name
|
| 53 |
for chunk in client.models.generate_content_stream(
|
|
@@ -57,19 +55,20 @@ def generate(text, file_name, api_key, model="gemini-2.0-flash-exp"):
|
|
| 57 |
):
|
| 58 |
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
| 59 |
continue
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
else:
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
del files
|
| 71 |
-
return
|
| 72 |
-
|
| 73 |
|
| 74 |
def process_image_and_prompt(composite_pil, prompt, gemini_api_key):
|
| 75 |
try:
|
|
@@ -77,61 +76,59 @@ def process_image_and_prompt(composite_pil, prompt, gemini_api_key):
|
|
| 77 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 78 |
composite_path = tmp.name
|
| 79 |
composite_pil.save(composite_path)
|
| 80 |
-
|
| 81 |
file_name = composite_path
|
| 82 |
input_text = prompt
|
| 83 |
model = "gemini-2.0-flash-exp"
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
result_img =
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
raise gr.Error(f"Error Getting {e}", duration=5)
|
| 93 |
|
| 94 |
-
# Build a Blocks-based interface
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
-
# HTML Header for the application.
|
| 97 |
gr.HTML(
|
| 98 |
"""
|
| 99 |
<div style='display: flex; align-items: center; justify-content: center; gap: 20px'>
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
</div>
|
| 112 |
"""
|
| 113 |
)
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
["data/2807615.jpg", "make it happy looking face only"],
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
]
|
| 133 |
-
|
| 134 |
-
gr.Markdown("Upload an image and enter a prompt to generate outputs in the gallery. Do not Use NFSW Images")
|
| 135 |
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column():
|
|
@@ -143,7 +140,7 @@ with gr.Blocks() as demo:
|
|
| 143 |
gemini_api_key = gr.Textbox(
|
| 144 |
lines=1,
|
| 145 |
placeholder="Enter Gemini API Key (optional)",
|
| 146 |
-
label="Gemini API Key (optional)
|
| 147 |
)
|
| 148 |
prompt_input = gr.Textbox(
|
| 149 |
lines=2,
|
|
@@ -153,18 +150,30 @@ with gr.Blocks() as demo:
|
|
| 153 |
submit_btn = gr.Button("Generate")
|
| 154 |
with gr.Column():
|
| 155 |
output_gallery = gr.Gallery(label="Generated Outputs")
|
|
|
|
| 156 |
|
| 157 |
-
# Set up the interaction.
|
| 158 |
submit_btn.click(
|
| 159 |
fn=process_image_and_prompt,
|
| 160 |
inputs=[image_input, prompt_input, gemini_api_key],
|
| 161 |
-
outputs=output_gallery,
|
| 162 |
-
|
| 163 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
gr.Examples(
|
| 165 |
examples=examples,
|
| 166 |
inputs=[image_input, prompt_input, gemini_api_key],
|
| 167 |
label="Try these examples"
|
| 168 |
)
|
| 169 |
|
| 170 |
-
demo.queue(max_size=500).launch()
|
|
|
|
| 3 |
import time
|
| 4 |
import uuid
|
| 5 |
import tempfile
|
| 6 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
import gradio as gr
|
| 8 |
import base64
|
| 9 |
import mimetypes
|
|
|
|
| 20 |
client = genai.Client(api_key=(api_key.strip() if api_key and api_key.strip() != ""
|
| 21 |
else os.environ.get("GEMINI_API_KEY")))
|
| 22 |
|
| 23 |
+
files = [ client.files.upload(file=file_name) ]
|
| 24 |
+
|
|
|
|
|
|
|
| 25 |
contents = [
|
| 26 |
types.Content(
|
| 27 |
role="user",
|
|
|
|
| 39 |
top_p=0.95,
|
| 40 |
top_k=40,
|
| 41 |
max_output_tokens=8192,
|
| 42 |
+
response_modalities=["image", "text"],
|
|
|
|
|
|
|
|
|
|
| 43 |
response_mime_type="text/plain",
|
| 44 |
)
|
| 45 |
|
| 46 |
+
text_response = ""
|
| 47 |
+
image_path = None
|
| 48 |
+
# Create a temporary file to potentially store image data.
|
| 49 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 50 |
temp_path = tmp.name
|
| 51 |
for chunk in client.models.generate_content_stream(
|
|
|
|
| 55 |
):
|
| 56 |
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
| 57 |
continue
|
| 58 |
+
candidate = chunk.candidates[0].content.parts[0]
|
| 59 |
+
# Check for inline image data
|
| 60 |
+
if candidate.inline_data:
|
| 61 |
+
save_binary_file(temp_path, candidate.inline_data.data)
|
| 62 |
+
print(f"File of mime type {candidate.inline_data.mime_type} saved to: {temp_path} and prompt input: {text}")
|
| 63 |
+
image_path = temp_path
|
| 64 |
+
# If an image is found, we assume that is the desired output.
|
| 65 |
+
break
|
| 66 |
else:
|
| 67 |
+
# Accumulate text response if no inline_data is present.
|
| 68 |
+
text_response += chunk.text + "\n"
|
| 69 |
+
|
| 70 |
del files
|
| 71 |
+
return image_path, text_response
|
|
|
|
| 72 |
|
| 73 |
def process_image_and_prompt(composite_pil, prompt, gemini_api_key):
|
| 74 |
try:
|
|
|
|
| 76 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 77 |
composite_path = tmp.name
|
| 78 |
composite_pil.save(composite_path)
|
| 79 |
+
|
| 80 |
file_name = composite_path
|
| 81 |
input_text = prompt
|
| 82 |
model = "gemini-2.0-flash-exp"
|
| 83 |
|
| 84 |
+
image_path, text_response = generate(text=input_text, file_name=file_name, api_key=gemini_api_key, model=model)
|
| 85 |
+
|
| 86 |
+
if image_path:
|
| 87 |
+
# Load and convert the image if needed.
|
| 88 |
+
result_img = Image.open(image_path)
|
| 89 |
+
if result_img.mode == "RGBA":
|
| 90 |
+
result_img = result_img.convert("RGB")
|
| 91 |
+
return [result_img], "" # Return image in gallery and empty text output.
|
| 92 |
+
else:
|
| 93 |
+
# Return no image and the text response.
|
| 94 |
+
return None, text_response
|
| 95 |
except Exception as e:
|
| 96 |
raise gr.Error(f"Error Getting {e}", duration=5)
|
| 97 |
|
| 98 |
+
# Build a Blocks-based interface with a custom HTML header.
|
| 99 |
with gr.Blocks() as demo:
|
|
|
|
| 100 |
gr.HTML(
|
| 101 |
"""
|
| 102 |
<div style='display: flex; align-items: center; justify-content: center; gap: 20px'>
|
| 103 |
+
<div style="background-color: var(--block-background-fill); border-radius: 8px">
|
| 104 |
+
<img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" style="width: 100px; height: 100px;">
|
| 105 |
+
</div>
|
| 106 |
+
<div>
|
| 107 |
+
<h1>Gen AI Image Editing</h1>
|
| 108 |
+
<p>Gemini for Image Editing</p>
|
| 109 |
+
<p>Powered by <a href="https://gradio.app/">Gradio</a> ⚡️</p>
|
| 110 |
+
<p>Duplicate Repo <a href="https://huggingface.co/spaces/ameerazam08/Gemini-Image-Edit?duplicate=true">Duplicate</a></p>
|
| 111 |
+
<p>Get an API Key <a href="https://aistudio.google.com/apikey">here</a></p>
|
| 112 |
+
<p>Follow me on Twitter: <a href="https://x.com/Ameerazam18">Ameerazam18</a></p>
|
| 113 |
+
</div>
|
| 114 |
</div>
|
| 115 |
"""
|
| 116 |
)
|
| 117 |
|
| 118 |
+
gr.Markdown("""
|
| 119 |
+
## ⚠️ API Configuration ⚠️
|
| 120 |
+
- **Issue:** ❗ Sometimes the model returns text instead of an image, causing failures when saving as an image.
|
| 121 |
+
### 🔧 Steps to Address:
|
| 122 |
+
1. **🛠️ Duplicate the Repository**
|
| 123 |
+
- Create a separate copy for modifications.
|
| 124 |
+
2. **🔑 Use Your Own Gemini API Key**
|
| 125 |
+
- You **must** configure your own Gemini key for generation!
|
| 126 |
+
---
|
| 127 |
+
### 📌 Usage
|
| 128 |
+
- Upload an image and enter a prompt to generate outputs.
|
| 129 |
+
- If text is returned instead of an image, it will appear in the text output.
|
| 130 |
+
- ❌ **Do not use NSFW images!**
|
| 131 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
with gr.Row():
|
| 134 |
with gr.Column():
|
|
|
|
| 140 |
gemini_api_key = gr.Textbox(
|
| 141 |
lines=1,
|
| 142 |
placeholder="Enter Gemini API Key (optional)",
|
| 143 |
+
label="Gemini API Key (optional)"
|
| 144 |
)
|
| 145 |
prompt_input = gr.Textbox(
|
| 146 |
lines=2,
|
|
|
|
| 150 |
submit_btn = gr.Button("Generate")
|
| 151 |
with gr.Column():
|
| 152 |
output_gallery = gr.Gallery(label="Generated Outputs")
|
| 153 |
+
output_text = gr.Textbox(label="Gemini Output", placeholder="Text response will appear here if no image is generated.")
|
| 154 |
|
| 155 |
+
# Set up the interaction with two outputs.
|
| 156 |
submit_btn.click(
|
| 157 |
fn=process_image_and_prompt,
|
| 158 |
inputs=[image_input, prompt_input, gemini_api_key],
|
| 159 |
+
outputs=[output_gallery, output_text],
|
|
|
|
| 160 |
)
|
| 161 |
+
|
| 162 |
+
examples = [
|
| 163 |
+
["data/1.webp", 'change text to "AMEER"', ""],
|
| 164 |
+
["data/2.webp", "remove the spoon from hand only", ""],
|
| 165 |
+
["data/3.webp", 'change text to "Make it "', ""],
|
| 166 |
+
["data/1.jpg", "add joker style only on face", ""],
|
| 167 |
+
["data/1777043.jpg", "add joker style only on face", ""],
|
| 168 |
+
["data/2807615.jpg", "add lipstick on lip only", ""],
|
| 169 |
+
["data/76860.jpg", "add lipstick on lip only", ""],
|
| 170 |
+
["data/2807615.jpg", "make it happy looking face only", ""],
|
| 171 |
+
]
|
| 172 |
+
|
| 173 |
gr.Examples(
|
| 174 |
examples=examples,
|
| 175 |
inputs=[image_input, prompt_input, gemini_api_key],
|
| 176 |
label="Try these examples"
|
| 177 |
)
|
| 178 |
|
| 179 |
+
demo.queue(max_size=500).launch()
|