Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,15 +34,14 @@
|
|
| 34 |
# if __name__ == "__main__":
|
| 35 |
# demo.launch()
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
import os
|
| 40 |
import gradio as gr
|
| 41 |
-
|
| 42 |
from PIL import Image
|
| 43 |
from io import BytesIO
|
|
|
|
| 44 |
|
| 45 |
-
# Load API key
|
| 46 |
api_key = os.getenv("Gemini_api_key")
|
| 47 |
|
| 48 |
# Initialize Gemini client
|
|
@@ -52,48 +51,52 @@ client = genai.Client(api_key=api_key)
|
|
| 52 |
def generate_or_edit_image(prompt, uploaded_img):
|
| 53 |
contents = [prompt]
|
| 54 |
|
| 55 |
-
# If user uploads an image, include it
|
| 56 |
if uploaded_img is not None:
|
| 57 |
-
# Validate image format
|
| 58 |
-
if uploaded_img.format not in ["JPEG", "JPG", "PNG"]:
|
| 59 |
-
return "❌ Please upload a JPG, JPEG, or PNG image."
|
| 60 |
-
|
| 61 |
-
# Convert uploaded image to PNG bytes (safe for Gemini)
|
| 62 |
buffered = BytesIO()
|
| 63 |
uploaded_img.save(buffered, format="PNG")
|
| 64 |
image_bytes = buffered.getvalue()
|
| 65 |
-
|
| 66 |
contents.append({
|
| 67 |
"mime_type": "image/png",
|
| 68 |
"data": image_bytes
|
| 69 |
})
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
for part in response.candidates[0].content.parts:
|
| 79 |
-
if part.inline_data is not None:
|
| 80 |
-
image = Image.open(BytesIO(part.inline_data.data))
|
| 81 |
-
return image
|
| 82 |
-
return None
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
demo = gr.Interface(
|
| 86 |
fn=generate_or_edit_image,
|
| 87 |
inputs=[
|
| 88 |
gr.Textbox(label="Enter your prompt"),
|
| 89 |
-
gr.Image(
|
| 90 |
-
label="Upload an image (optional)",
|
| 91 |
-
type="pil" # just keep type=pil, no file_types in your version
|
| 92 |
-
)
|
| 93 |
],
|
| 94 |
outputs=gr.Image(label="Generated/Edited Image"),
|
| 95 |
title="Image Generator & Editor",
|
| 96 |
-
description="Enter a
|
| 97 |
)
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
|
|
|
| 34 |
# if __name__ == "__main__":
|
| 35 |
# demo.launch()
|
| 36 |
|
|
|
|
|
|
|
| 37 |
import os
|
| 38 |
import gradio as gr
|
| 39 |
+
import google.genai as genai # ✅ correct import
|
| 40 |
from PIL import Image
|
| 41 |
from io import BytesIO
|
| 42 |
+
import base64
|
| 43 |
|
| 44 |
+
# Load API key
|
| 45 |
api_key = os.getenv("Gemini_api_key")
|
| 46 |
|
| 47 |
# Initialize Gemini client
|
|
|
|
| 51 |
def generate_or_edit_image(prompt, uploaded_img):
|
| 52 |
contents = [prompt]
|
| 53 |
|
| 54 |
+
# If user uploads an image, include it
|
| 55 |
if uploaded_img is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
buffered = BytesIO()
|
| 57 |
uploaded_img.save(buffered, format="PNG")
|
| 58 |
image_bytes = buffered.getvalue()
|
|
|
|
| 59 |
contents.append({
|
| 60 |
"mime_type": "image/png",
|
| 61 |
"data": image_bytes
|
| 62 |
})
|
| 63 |
|
| 64 |
+
try:
|
| 65 |
+
response = client.models.generate_content(
|
| 66 |
+
model="gemini-2.5-flash-image-preview",
|
| 67 |
+
contents=contents,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# 🔎 Debug: print structure
|
| 71 |
+
print("Gemini Response:", response)
|
| 72 |
+
|
| 73 |
+
for part in response.candidates[0].content.parts:
|
| 74 |
+
if part.inline_data is not None:
|
| 75 |
+
# Sometimes Gemini returns base64 string
|
| 76 |
+
data = part.inline_data.data
|
| 77 |
+
if isinstance(data, str):
|
| 78 |
+
image_bytes = base64.b64decode(data)
|
| 79 |
+
else:
|
| 80 |
+
image_bytes = data
|
| 81 |
+
|
| 82 |
+
image = Image.open(BytesIO(image_bytes))
|
| 83 |
+
return image
|
| 84 |
|
| 85 |
+
return "⚠️ No image generated. Try a different prompt."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return f"❌ Error: {e}"
|
| 89 |
+
|
| 90 |
+
# Gradio UI
|
| 91 |
demo = gr.Interface(
|
| 92 |
fn=generate_or_edit_image,
|
| 93 |
inputs=[
|
| 94 |
gr.Textbox(label="Enter your prompt"),
|
| 95 |
+
gr.Image(label="Upload an image (optional)", type="pil")
|
|
|
|
|
|
|
|
|
|
| 96 |
],
|
| 97 |
outputs=gr.Image(label="Generated/Edited Image"),
|
| 98 |
title="Image Generator & Editor",
|
| 99 |
+
description="Enter a prompt to generate a new image, or upload a JPG/PNG to edit."
|
| 100 |
)
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|