AkashKumarave commited on
Commit
15652b4
·
verified ·
1 Parent(s): 9e0552c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -35
app.py CHANGED
@@ -1,48 +1,74 @@
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()
 
 
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()