Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import tempfile
|
|
| 6 |
import uuid
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
-
# Initialize the Gemini client globally
|
| 10 |
client = None
|
| 11 |
if os.environ.get("GOOGLE_API_KEY"):
|
| 12 |
client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))
|
|
@@ -19,27 +18,21 @@ def save_binary_file(file_name, data):
|
|
| 19 |
|
| 20 |
|
| 21 |
def process_image_with_gemini(image, instruction) -> tuple[str, str, str]:
|
| 22 |
-
# Create output directory if it doesn't exist
|
| 23 |
output_dir = Path("output_gemini")
|
| 24 |
output_dir.mkdir(exist_ok=True)
|
| 25 |
|
| 26 |
-
# Generate a unique ID for this request
|
| 27 |
request_id = f"request_{uuid.uuid4().hex[:8]}"
|
| 28 |
request_folder = output_dir / request_id
|
| 29 |
request_folder.mkdir(exist_ok=True)
|
| 30 |
|
| 31 |
-
# Save the input image to the request folder
|
| 32 |
input_image_path = request_folder / "input.jpg"
|
| 33 |
image.save(input_image_path)
|
| 34 |
|
| 35 |
try:
|
| 36 |
-
# Create a temporary directory that will be automatically cleaned up
|
| 37 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 38 |
-
# Save the image to a temporary file
|
| 39 |
temp_image_path = Path(temp_dir) / "temp_input_image.jpg"
|
| 40 |
image.save(temp_image_path)
|
| 41 |
|
| 42 |
-
# Upload the temporary file to Gemini API using global client
|
| 43 |
files = [
|
| 44 |
client.files.upload(file=str(temp_image_path)),
|
| 45 |
]
|
|
@@ -69,7 +62,7 @@ def process_image_with_gemini(image, instruction) -> tuple[str, str, str]:
|
|
| 69 |
safety_settings=[
|
| 70 |
types.SafetySetting(
|
| 71 |
category="HARM_CATEGORY_CIVIC_INTEGRITY",
|
| 72 |
-
threshold="OFF",
|
| 73 |
),
|
| 74 |
],
|
| 75 |
response_mime_type="text/plain",
|
|
@@ -90,19 +83,15 @@ def process_image_with_gemini(image, instruction) -> tuple[str, str, str]:
|
|
| 90 |
):
|
| 91 |
continue
|
| 92 |
|
| 93 |
-
# Handle image response
|
| 94 |
if hasattr(chunk.candidates[0].content.parts[0], "inline_data"):
|
| 95 |
-
# Save the generated image
|
| 96 |
edited_image_path = request_folder / "edited.jpg"
|
| 97 |
save_binary_file(
|
| 98 |
str(edited_image_path),
|
| 99 |
chunk.candidates[0].content.parts[0].inline_data.data,
|
| 100 |
)
|
| 101 |
-
# Handle text response
|
| 102 |
elif hasattr(chunk.candidates[0].content.parts[0], "text"):
|
| 103 |
response_text += chunk.candidates[0].content.parts[0].text
|
| 104 |
|
| 105 |
-
# Simplify the return statement and ensure consistent types
|
| 106 |
if edited_image_path and edited_image_path.exists():
|
| 107 |
return str(edited_image_path), response_text or "", "Success"
|
| 108 |
return None, response_text or "", "No image generated"
|
|
@@ -119,11 +108,9 @@ def process_image_with_gemini(image, instruction) -> tuple[str, str, str]:
|
|
| 119 |
|
| 120 |
def process_image(image, instruction):
|
| 121 |
"""Process an image with Gemini based on given instructions.
|
| 122 |
-
|
| 123 |
Args:
|
| 124 |
image: Input PIL image
|
| 125 |
instruction: Text instructions for editing
|
| 126 |
-
|
| 127 |
Returns:
|
| 128 |
Tuple containing (output_image_path, response_text, status_message)
|
| 129 |
"""
|
|
@@ -146,9 +133,9 @@ def process_image(image, instruction):
|
|
| 146 |
return None, "", f"Unexpected error: {str(e)}"
|
| 147 |
|
| 148 |
|
| 149 |
-
with gr.Blocks(title="
|
| 150 |
with gr.Column():
|
| 151 |
-
gr.Markdown("# 🖼️
|
| 152 |
gr.Markdown(
|
| 153 |
"Upload an image and provide instructions for Gemini to edit it. The AI will generate a new image based on your instructions."
|
| 154 |
)
|
|
@@ -176,19 +163,6 @@ with gr.Blocks(title="Gemini Image Editor") as app:
|
|
| 176 |
outputs=[output_image, response_text, status],
|
| 177 |
)
|
| 178 |
|
| 179 |
-
# Add sample instructions (without example images)
|
| 180 |
-
gr.Markdown("### Sample Instructions (upload your own image and try these)")
|
| 181 |
-
sample_instructions = gr.Examples(
|
| 182 |
-
examples=[
|
| 183 |
-
"Make the sky more blue and add birds flying",
|
| 184 |
-
"Convert this to a watercolor painting style",
|
| 185 |
-
"Add a sunset effect to this image",
|
| 186 |
-
"Turn this into a night scene with stars",
|
| 187 |
-
"Make this look like it was taken in winter with snow",
|
| 188 |
-
],
|
| 189 |
-
inputs=instruction,
|
| 190 |
-
)
|
| 191 |
-
|
| 192 |
gr.Markdown(
|
| 193 |
"""
|
| 194 |
### Notes
|
|
@@ -199,7 +173,6 @@ with gr.Blocks(title="Gemini Image Editor") as app:
|
|
| 199 |
)
|
| 200 |
|
| 201 |
|
| 202 |
-
# Launch the app
|
| 203 |
if __name__ == "__main__":
|
| 204 |
print("Starting Gemini Image Editor...")
|
| 205 |
-
app.launch(ssr_mode=True)
|
|
|
|
| 6 |
import uuid
|
| 7 |
from pathlib import Path
|
| 8 |
|
|
|
|
| 9 |
client = None
|
| 10 |
if os.environ.get("GOOGLE_API_KEY"):
|
| 11 |
client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
def process_image_with_gemini(image, instruction) -> tuple[str, str, str]:
|
|
|
|
| 21 |
output_dir = Path("output_gemini")
|
| 22 |
output_dir.mkdir(exist_ok=True)
|
| 23 |
|
|
|
|
| 24 |
request_id = f"request_{uuid.uuid4().hex[:8]}"
|
| 25 |
request_folder = output_dir / request_id
|
| 26 |
request_folder.mkdir(exist_ok=True)
|
| 27 |
|
|
|
|
| 28 |
input_image_path = request_folder / "input.jpg"
|
| 29 |
image.save(input_image_path)
|
| 30 |
|
| 31 |
try:
|
|
|
|
| 32 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
| 33 |
temp_image_path = Path(temp_dir) / "temp_input_image.jpg"
|
| 34 |
image.save(temp_image_path)
|
| 35 |
|
|
|
|
| 36 |
files = [
|
| 37 |
client.files.upload(file=str(temp_image_path)),
|
| 38 |
]
|
|
|
|
| 62 |
safety_settings=[
|
| 63 |
types.SafetySetting(
|
| 64 |
category="HARM_CATEGORY_CIVIC_INTEGRITY",
|
| 65 |
+
threshold="OFF",
|
| 66 |
),
|
| 67 |
],
|
| 68 |
response_mime_type="text/plain",
|
|
|
|
| 83 |
):
|
| 84 |
continue
|
| 85 |
|
|
|
|
| 86 |
if hasattr(chunk.candidates[0].content.parts[0], "inline_data"):
|
|
|
|
| 87 |
edited_image_path = request_folder / "edited.jpg"
|
| 88 |
save_binary_file(
|
| 89 |
str(edited_image_path),
|
| 90 |
chunk.candidates[0].content.parts[0].inline_data.data,
|
| 91 |
)
|
|
|
|
| 92 |
elif hasattr(chunk.candidates[0].content.parts[0], "text"):
|
| 93 |
response_text += chunk.candidates[0].content.parts[0].text
|
| 94 |
|
|
|
|
| 95 |
if edited_image_path and edited_image_path.exists():
|
| 96 |
return str(edited_image_path), response_text or "", "Success"
|
| 97 |
return None, response_text or "", "No image generated"
|
|
|
|
| 108 |
|
| 109 |
def process_image(image, instruction):
|
| 110 |
"""Process an image with Gemini based on given instructions.
|
|
|
|
| 111 |
Args:
|
| 112 |
image: Input PIL image
|
| 113 |
instruction: Text instructions for editing
|
|
|
|
| 114 |
Returns:
|
| 115 |
Tuple containing (output_image_path, response_text, status_message)
|
| 116 |
"""
|
|
|
|
| 133 |
return None, "", f"Unexpected error: {str(e)}"
|
| 134 |
|
| 135 |
|
| 136 |
+
with gr.Blocks(title="Image Editor", theme='Jonny001/GreenEarth_Theme') as app:
|
| 137 |
with gr.Column():
|
| 138 |
+
gr.Markdown("# 🖼️ Image Editor")
|
| 139 |
gr.Markdown(
|
| 140 |
"Upload an image and provide instructions for Gemini to edit it. The AI will generate a new image based on your instructions."
|
| 141 |
)
|
|
|
|
| 163 |
outputs=[output_image, response_text, status],
|
| 164 |
)
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
gr.Markdown(
|
| 167 |
"""
|
| 168 |
### Notes
|
|
|
|
| 173 |
)
|
| 174 |
|
| 175 |
|
|
|
|
| 176 |
if __name__ == "__main__":
|
| 177 |
print("Starting Gemini Image Editor...")
|
| 178 |
+
app.launch(ssr_mode=True)
|