Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from google import genai
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
-
# Load API key from
|
| 8 |
api_key = os.getenv("Gemini_api_key")
|
| 9 |
|
| 10 |
# Initialize Gemini client
|
| 11 |
client = genai.Client(api_key=api_key)
|
| 12 |
|
| 13 |
-
# Function to generate
|
| 14 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
response = client.models.generate_content(
|
| 16 |
model="gemini-2.5-flash-image-preview",
|
| 17 |
-
contents=
|
| 18 |
)
|
|
|
|
|
|
|
| 19 |
for part in response.candidates[0].content.parts:
|
| 20 |
if part.inline_data is not None:
|
| 21 |
image = Image.open(BytesIO(part.inline_data.data))
|
|
@@ -24,12 +80,20 @@ def generate_image(prompt):
|
|
| 24 |
|
| 25 |
# Gradio interface
|
| 26 |
demo = gr.Interface(
|
| 27 |
-
fn=
|
| 28 |
-
inputs=
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
| 35 |
demo.launch()
|
|
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import gradio as gr
|
| 3 |
+
# from google import genai
|
| 4 |
+
# from PIL import Image
|
| 5 |
+
# from io import BytesIO
|
| 6 |
+
|
| 7 |
+
# # Load API key from Hugging Face secrets
|
| 8 |
+
# api_key = os.getenv("Gemini_api_key")
|
| 9 |
+
|
| 10 |
+
# # Initialize Gemini client
|
| 11 |
+
# client = genai.Client(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
# # Function to generate image from prompt
|
| 14 |
+
# def generate_image(prompt):
|
| 15 |
+
# response = client.models.generate_content(
|
| 16 |
+
# model="gemini-2.5-flash-image-preview",
|
| 17 |
+
# contents=[prompt],
|
| 18 |
+
# )
|
| 19 |
+
# for part in response.candidates[0].content.parts:
|
| 20 |
+
# if part.inline_data is not None:
|
| 21 |
+
# image = Image.open(BytesIO(part.inline_data.data))
|
| 22 |
+
# return image
|
| 23 |
+
# return None
|
| 24 |
+
|
| 25 |
+
# # Gradio interface
|
| 26 |
+
# demo = gr.Interface(
|
| 27 |
+
# fn=generate_image,
|
| 28 |
+
# inputs=gr.Textbox(label="Enter your prompt"),
|
| 29 |
+
# outputs=gr.Image(label="Generated Image"),
|
| 30 |
+
# title="Image Generator",
|
| 31 |
+
# description="Enter a text prompt and generate images"
|
| 32 |
+
# )
|
| 33 |
+
|
| 34 |
+
# if __name__ == "__main__":
|
| 35 |
+
# demo.launch()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
import os
|
| 41 |
import gradio as gr
|
| 42 |
from google import genai
|
| 43 |
from PIL import Image
|
| 44 |
from io import BytesIO
|
| 45 |
|
| 46 |
+
# Load API key from environment
|
| 47 |
api_key = os.getenv("Gemini_api_key")
|
| 48 |
|
| 49 |
# Initialize Gemini client
|
| 50 |
client = genai.Client(api_key=api_key)
|
| 51 |
|
| 52 |
+
# Function to generate or edit image
|
| 53 |
+
def generate_or_edit_image(prompt, uploaded_img):
|
| 54 |
+
contents = [prompt]
|
| 55 |
+
|
| 56 |
+
# If user uploads an image, include it in the request
|
| 57 |
+
if uploaded_img is not None:
|
| 58 |
+
# Convert uploaded image to bytes (keep original format if possible)
|
| 59 |
+
buffered = BytesIO()
|
| 60 |
+
uploaded_img.save(buffered, format="PNG") # convert to PNG for consistency
|
| 61 |
+
image_bytes = buffered.getvalue()
|
| 62 |
+
|
| 63 |
+
contents.append({
|
| 64 |
+
"mime_type": "image/png",
|
| 65 |
+
"data": image_bytes
|
| 66 |
+
})
|
| 67 |
+
|
| 68 |
+
# Call Gemini image model
|
| 69 |
response = client.models.generate_content(
|
| 70 |
model="gemini-2.5-flash-image-preview",
|
| 71 |
+
contents=contents,
|
| 72 |
)
|
| 73 |
+
|
| 74 |
+
# Extract image from response
|
| 75 |
for part in response.candidates[0].content.parts:
|
| 76 |
if part.inline_data is not None:
|
| 77 |
image = Image.open(BytesIO(part.inline_data.data))
|
|
|
|
| 80 |
|
| 81 |
# Gradio interface
|
| 82 |
demo = gr.Interface(
|
| 83 |
+
fn=generate_or_edit_image,
|
| 84 |
+
inputs=[
|
| 85 |
+
gr.Textbox(label="Enter your prompt"),
|
| 86 |
+
gr.Image(
|
| 87 |
+
label="Upload an image (optional)",
|
| 88 |
+
type="pil",
|
| 89 |
+
file_types=[".jpg", ".jpeg", ".png"] # allow JPG, JPEG, PNG
|
| 90 |
+
)
|
| 91 |
+
],
|
| 92 |
+
outputs=gr.Image(label="Generated/Edited Image"),
|
| 93 |
+
title="Image Generator & Editor",
|
| 94 |
+
description="Enter a text prompt to generate a new image, or upload an image (JPG, JPEG, PNG) to edit it."
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
demo.launch()
|
| 99 |
+
|