AkashKumarave commited on
Commit
d12f2bb
·
verified ·
1 Parent(s): c012947

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -76
app.py CHANGED
@@ -1,85 +1,48 @@
 
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
- import torch
5
- from diffusers import DiffusionPipeline
6
 
7
- # Ensure the model runs on CPU
8
- device = "cpu"
9
- dtype = torch.float32 # Use float32 for CPU compatibility
10
 
11
- # Load model from Hugging Face (it will cache locally in Hugging Face Spaces)
12
- pipe = DiffusionPipeline.from_pretrained(
13
- "black-forest-labs/FLUX.1-schnell",
14
- torch_dtype=dtype,
15
- low_cpu_mem_usage=True
16
- ).to(device)
17
 
18
- MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = 1024
20
 
21
- def infer(prompt, seed=42, randomize_seed=False, width=512, height=512, num_inference_steps=4):
22
- if randomize_seed:
23
- seed = random.randint(0, MAX_SEED)
24
- generator = torch.Generator(device=device).manual_seed(seed)
25
- image = pipe(
26
- prompt=prompt,
27
- width=width,
28
- height=height,
29
- num_inference_steps=num_inference_steps,
30
- generator=generator,
31
- guidance_scale=0.0
32
- ).images[0]
33
- return image, seed
34
 
35
- examples = [
36
- "a tiny astronaut hatching from an egg on the moon",
37
- "a cat holding a sign that says hello world",
38
- "an anime illustration of a wiener schnitzel",
39
- ]
 
 
 
40
 
41
- css="""
42
- #col-container {
43
- margin: 0 auto;
44
- max-width: 520px;
45
- }
46
- """
47
 
48
- with gr.Blocks(css=css) as demo:
49
- with gr.Column(elem_id="col-container"):
50
- gr.Markdown("""# FLUX.1 [schnell]
51
- 12B param rectified flow transformer distilled from FLUX.1 [pro]
52
- """)
53
-
54
- with gr.Row():
55
- prompt = gr.Text(label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False)
56
- run_button = gr.Button("Run", scale=0)
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
- demo.launch()
 
 
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()