AkashKumarave commited on
Commit
efbbf0f
·
verified ·
1 Parent(s): 26f140a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -36
app.py CHANGED
@@ -7,17 +7,18 @@ import base64
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
  {
@@ -27,24 +28,26 @@ def process_image(image, prompt):
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
 
@@ -52,23 +55,35 @@ def process_image(image, prompt):
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()
 
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
+ GEMINI_MODEL_NAME = 'gemini-2.5-flash-image-preview'
11
 
12
  def process_image(image, prompt):
13
  try:
 
 
 
 
 
14
  # Prepare the content for the Gemini API
15
+ contents = []
16
+ if image:
17
+ # Convert Gradio image (PIL Image) to base64
18
+ buffered = BytesIO()
19
+ image.save(buffered, format="PNG")
20
+ img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
21
+ contents.append({
22
  "parts": [
23
  {"text": prompt},
24
  {
 
28
  }
29
  }
30
  ]
31
+ })
32
+ else:
33
+ # Text-to-image generation
34
+ contents.append({"parts": [{"text": prompt}]})
35
 
36
  # Call the Gemini API
37
  response = client.models.generate_content(
38
+ model=GEMINI_MODEL_NAME,
39
  contents=contents
40
  )
41
 
42
  # Process the response
43
+ for candidate in response.candidates:
44
+ for part in candidate.content.parts:
45
+ if hasattr(part, 'inline_data') and part.inline_data:
46
+ # Decode the generated image
47
+ img_data = base64.b64decode(part.inline_data.data)
48
+ return Image.open(BytesIO(img_data))
49
+ elif part.text:
50
+ return f"Text response: {part.text}"
51
 
52
  return "No image or text returned by the model."
53
 
 
55
  return f"Error: {str(e)}"
56
 
57
  # Create the Gradio interface
58
+ css = '''
59
+ .grid-container img {object-fit: contain}
60
+ .grid-container {display: grid; grid-template-columns: 1fr}
61
+ '''
62
+
63
+ with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
64
+ gr.HTML('''
65
+ <img src='https://huggingface.co/spaces/multimodalart/nano-banana/resolve/main/nano_banana_pros_light.png' style='margin: 0 auto; max-width: 500px' />
66
+ <h3 style='text-align:center'>Nano Banana: Gemini 2.5 Flash Image Preview</h3>
67
+ ''')
68
+
69
+ with gr.Row():
70
+ with gr.Column(scale=1):
71
+ image_input = gr.Image(type="pil", label="Upload Image (Optional)", file_types=["image"])
72
+ prompt_input = gr.Textbox(
73
+ label="Prompt",
74
+ placeholder="e.g., 'Add a nano-banana to the image in a fancy restaurant setting' or 'Generate a cat eating a nano-banana'"
75
+ )
76
+ generate_button = gr.Button("Generate", variant="primary")
77
+
78
+ with gr.Column(scale=1):
79
+ output_image = gr.Image(label="Generated Image", type="pil")
80
+
81
+ # Event handler
82
+ generate_button.click(
83
+ fn=process_image,
84
+ inputs=[image_input, prompt_input],
85
+ outputs=[output_image]
86
+ )
87
 
 
88
  if __name__ == "__main__":
89
+ demo.launch()