Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,74 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
from google import genai
|
| 4 |
-
from
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
model=MODEL_NAME,
|
| 19 |
-
prompt=prompt,
|
| 20 |
-
images=[image1, image2]
|
| 21 |
)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
return f"Error: {str(e)}"
|
| 31 |
|
| 32 |
-
# Gradio
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from google import genai
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import base64
|
| 6 |
|
| 7 |
+
# Configure the Gemini API client with the hardcoded API key
|
| 8 |
+
GOOGLE_API_KEY = "AIzaSyDL5Rilo7ptJpUOZdY6wy8PJYUcVcnDADs"
|
| 9 |
+
client = genai.Client(api_key=GOOGLE_API_KEY)
|
| 10 |
|
| 11 |
+
def process_image(image, prompt):
|
| 12 |
+
try:
|
| 13 |
+
# Convert Gradio image (PIL Image) to base64
|
| 14 |
+
buffered = BytesIO()
|
| 15 |
+
image.save(buffered, format="PNG")
|
| 16 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 17 |
|
| 18 |
+
# Prepare the content for the Gemini API
|
| 19 |
+
contents = [
|
| 20 |
+
{
|
| 21 |
+
"parts": [
|
| 22 |
+
{"text": prompt},
|
| 23 |
+
{
|
| 24 |
+
"inline_data": {
|
| 25 |
+
"mime_type": "image/png",
|
| 26 |
+
"data": img_base64
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
]
|
| 30 |
+
}
|
| 31 |
+
]
|
| 32 |
|
| 33 |
+
# Call the Gemini API
|
| 34 |
+
response = client.models.generate_content(
|
| 35 |
+
model="gemini-2.5-flash-image-preview",
|
| 36 |
+
contents=contents
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Process the response
|
| 40 |
+
for part in response.candidates[0].content.parts:
|
| 41 |
+
if part.inline_data is not None:
|
| 42 |
+
# Decode the generated image
|
| 43 |
+
img_data = base64.b64decode(part.inline_data.data)
|
| 44 |
+
img = Image.open(BytesIO(img_data))
|
| 45 |
+
return img
|
| 46 |
+
elif part.text is not None:
|
| 47 |
+
return f"Text response: {part.text}"
|
| 48 |
+
|
| 49 |
+
return "No image or text returned by the model."
|
| 50 |
+
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error: {str(e)}"
|
| 53 |
|
| 54 |
+
# Create the Gradio interface
|
| 55 |
+
title = "Gemini 2.5 Flash Image Editor (Nano Banana)"
|
| 56 |
+
description = "Upload an image and provide a text prompt to generate or edit images using Google's Gemini 2.5 Flash Image Preview model."
|
| 57 |
+
interface = gr.Interface(
|
| 58 |
+
fn=process_image,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 61 |
+
gr.Textbox(label="Prompt", placeholder="e.g., 'Add a nano-banana to the image in a fancy restaurant setting'")
|
| 62 |
+
],
|
| 63 |
+
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 64 |
+
title=title,
|
| 65 |
+
description=description,
|
| 66 |
+
examples=[
|
| 67 |
+
[None, "Create a minimalist composition with a single red maple leaf in the bottom-right, soft lighting, square image"],
|
| 68 |
+
[None, "Generate an image of a cat eating a nano-banana in a fancy restaurant"]
|
| 69 |
+
]
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
# Launch the app
|
| 73 |
if __name__ == "__main__":
|
| 74 |
+
interface.launch()
|