Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,48 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import torch
|
| 5 |
-
from diffusers import DiffusionPipeline
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
dtype = torch.float32 # Use float32 for CPU compatibility
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
"black-forest-labs/FLUX.1-schnell",
|
| 14 |
-
torch_dtype=dtype,
|
| 15 |
-
low_cpu_mem_usage=True
|
| 16 |
-
).to(device)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
MAX_IMAGE_SIZE = 1024
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
num_inference_steps=num_inference_steps,
|
| 30 |
-
generator=generator,
|
| 31 |
-
guidance_scale=0.0
|
| 32 |
-
).images[0]
|
| 33 |
-
return image, seed
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
max-width: 520px;
|
| 45 |
-
}
|
| 46 |
-
"""
|
| 47 |
|
| 48 |
-
with gr.
|
| 49 |
-
|
| 50 |
-
gr.
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
result = gr.Image(label="Result", show_label=False)
|
| 59 |
-
|
| 60 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 61 |
-
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 62 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 63 |
-
|
| 64 |
-
with gr.Row():
|
| 65 |
-
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
|
| 66 |
-
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512)
|
| 67 |
-
|
| 68 |
-
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=4)
|
| 69 |
-
|
| 70 |
-
gr.Examples(
|
| 71 |
-
examples=examples,
|
| 72 |
-
fn=infer,
|
| 73 |
-
inputs=[prompt],
|
| 74 |
-
outputs=[result, seed],
|
| 75 |
-
cache_examples="lazy"
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
gr.on(
|
| 79 |
-
triggers=[run_button.click, prompt.submit],
|
| 80 |
-
fn=infer,
|
| 81 |
-
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 82 |
-
outputs=[result, seed]
|
| 83 |
-
)
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from google import genai
|
| 4 |
+
from google.genai import types
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Set your Google API key
|
| 7 |
+
os.environ["GOOGLE_API_KEY"] = "AIzaSyDL5Rilo7ptJpUOZdY6wy8PJYUcVcnDADs"
|
|
|
|
| 8 |
|
| 9 |
+
# Initialize the Google Generative AI client
|
| 10 |
+
client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
MODEL_NAME = "gemini-2.5-flash-image-preview"
|
|
|
|
| 13 |
|
| 14 |
+
def remix_images(image1, image2, prompt):
|
| 15 |
+
try:
|
| 16 |
+
# Run request to Google Generative AI
|
| 17 |
+
response = client.models.generate_images(
|
| 18 |
+
model=MODEL_NAME,
|
| 19 |
+
prompt=prompt,
|
| 20 |
+
images=[image1, image2]
|
| 21 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Extract the first generated image
|
| 24 |
+
if response.generated_images and len(response.generated_images) > 0:
|
| 25 |
+
img = response.generated_images[0]
|
| 26 |
+
return img.image # returns PIL.Image
|
| 27 |
+
else:
|
| 28 |
+
return None
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error: {str(e)}"
|
| 31 |
|
| 32 |
+
# Gradio UI
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("## 🖼️ Google Gemini 2.5 Flash Image Preview (via Gradio)")
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
with gr.Row():
|
| 37 |
+
img1 = gr.Image(type="filepath", label="Upload Image 1")
|
| 38 |
+
img2 = gr.Image(type="filepath", label="Upload Image 2")
|
| 39 |
+
|
| 40 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Describe how to remix the images...")
|
| 41 |
+
|
| 42 |
+
btn = gr.Button("Generate")
|
| 43 |
+
output = gr.Image(label="Generated Image")
|
| 44 |
+
|
| 45 |
+
btn.click(fn=remix_images, inputs=[img1, img2, prompt], outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch()
|