AkashKumarave commited on
Commit
1b3c35b
Β·
verified Β·
1 Parent(s): efbbf0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -71
app.py CHANGED
@@ -1,89 +1,164 @@
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
- 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
- {
25
- "inline_data": {
26
- "mime_type": "image/png",
27
- "data": img_base64
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
-
 
 
 
 
 
 
 
 
 
 
54
  except Exception as e:
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()
 
1
+ import os
2
+ import tempfile
3
  from PIL import Image
4
+ import gradio as gr
5
+ import google.generativeai as genai
6
+
7
+ # Hardcode the Gemini API key
8
+ GEMINI_API_KEY = "AIzaSyDL5Rilo7ptJpUOZdY6wy8PJYUcVcnDADs"
9
 
10
+ def configure_api_key():
11
+ """Configure the Gemini API key."""
12
+ if not GEMINI_API_KEY:
13
+ raise gr.Error("Gemini API key is not set.")
14
+ genai.configure(api_key=GEMINI_API_KEY)
15
 
16
+ def generate(text, images, model="gemini-2.5-flash"):
17
+ """Generate content using the Gemini model."""
18
+ configure_api_key()
19
+
20
+ # Convert images to Gemini-compatible format
21
+ contents = []
22
+ for img in images:
23
+ if img.mode == "RGBA":
24
+ img = img.convert("RGB")
25
+ contents.append(img)
26
+ contents.append(text)
27
+
28
  try:
29
+ response = genai.GenerativeModel(model).generate_content(contents)
30
+
31
+ text_response = ""
32
+ image_path = None
33
+
34
+ for part in response.candidates[0].content.parts:
35
+ if hasattr(part, 'text') and part.text:
36
+ text_response += part.text + "\n"
37
+ elif hasattr(part, 'inline_data') and part.inline_data:
38
+ # Save generated image to a temporary file
39
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
40
+ image_path = tmp.name
41
+ generated_image = Image.open(BytesIO(part.inline_data.data))
42
+ generated_image.save(image_path)
43
+ print(f"Generated image saved to: {image_path} with prompt: {text}")
44
+
45
+ return image_path, text_response
46
+ except Exception as e:
47
+ raise gr.Error(f"Error generating content: {str(e)}")
 
 
48
 
49
+ def load_uploaded_images(uploaded_files):
50
+ """Load and display uploaded images immediately."""
51
+ uploaded_images = []
52
+ if uploaded_files:
53
+ for file in uploaded_files:
54
+ if file.name.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
55
+ img = Image.open(file.name)
56
+ uploaded_images.append(img)
57
+ return uploaded_images
58
 
59
+ def process_image_and_prompt(uploaded_files, prompt):
60
+ """Process uploaded images and prompt to generate output."""
61
+ try:
62
+ if not uploaded_files:
63
+ raise gr.Error("Please upload at least one image.")
64
+ if not prompt:
65
+ raise gr.Error("Please provide a prompt.")
 
 
66
 
67
+ # Load images
68
+ images = load_uploaded_images(uploaded_files)
69
+
70
+ # Generate content
71
+ image_path, text_response = generate(
72
+ text=prompt,
73
+ images=images
74
+ )
75
+
76
+ # Prepare outputs
77
+ output_images = [Image.open(image_path)] if image_path else None
78
+ return images, output_images, text_response
79
  except Exception as e:
80
+ raise gr.Error(f"Error: {str(e)}")
81
 
82
+ # Gradio interface
83
+ with gr.Blocks(css="style.css") as demo:
84
+ gr.HTML("""
85
+ <div class="header-container">
86
+ <div>
87
+ <img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" alt="Gemini logo">
88
+ </div>
89
+ <div>
90
+ <h1>Gemini for Image Editing</h1>
91
+ <p>Powered by <a href="https://gradio.app/">Gradio</a>⚑️ |
92
+ <a href="https://huggingface.co/spaces">Duplicate this Space</a> |
93
+ <a href="https://aistudio.google.com/apikey">Learn about Gemini API</a></p>
94
+ </div>
95
+ </div>
96
+ """)
97
+
98
+ with gr.Accordion("⚠️ API Configuration ⚠️", open=False):
99
+ gr.Markdown("""
100
+ - **Note:** The Gemini API is pre-configured for this Space.
101
+ - **Issue:** ❗ Sometimes the model returns text instead of an image.
102
+ ### πŸ”§ Steps to Address:
103
+ 1. **πŸ› οΈ Duplicate the Space**
104
+ - Create a copy on Hugging Face Spaces for modifications.
105
+ 2. **πŸ”‘ API Key Info**
106
+ - The API key is already set up for this demo. For custom deployments, get your own key from Google AI Studio.
107
+ """)
108
 
109
+ with gr.Accordion("πŸ“Œ Usage Instructions", open=False):
110
+ gr.Markdown("""
111
+ ### πŸ“Œ Usage
112
+ - Upload an image and enter a prompt to generate outputs.
113
+ - If text is returned instead of an image, it will appear in the text output.
114
+ - Supported formats: PNG, JPG, JPEG, WEBP
115
+ - ❌ **Do not use NSFW images!**
116
+ """)
117
 
118
  with gr.Row():
119
+ with gr.Column():
120
+ image_input = gr.File(
121
+ file_types=["image"],
122
+ file_count="multiple",
123
+ label="Upload Images"
124
+ )
125
  prompt_input = gr.Textbox(
126
+ lines=2,
127
+ placeholder="Enter prompt here...",
128
+ label="Prompt"
129
+ )
130
+ submit_btn = gr.Button("Generate")
131
+
132
+ with gr.Column():
133
+ uploaded_gallery = gr.Gallery(label="Uploaded Images")
134
+ output_gallery = gr.Gallery(label="Generated Outputs")
135
+ output_text = gr.Textbox(
136
+ label="Gemini Output",
137
+ placeholder="Text response will appear here if no image is generated."
138
  )
 
 
 
 
139
 
140
+ submit_btn.click(
141
+ fn=process_image_and_prompt,
 
142
  inputs=[image_input, prompt_input],
143
+ outputs=[uploaded_gallery, output_gallery, output_text]
144
+ )
145
+
146
+ image_input.upload(
147
+ fn=load_uploaded_images,
148
+ inputs=[image_input],
149
+ outputs=[uploaded_gallery]
150
+ )
151
+
152
+ gr.Markdown("## Try these examples")
153
+ examples = [
154
+ ["data/1.webp", "change text to 'HUGGINGFACE'"],
155
+ ["data/2.webp", "remove the spoon from hand only"],
156
+ ["data/3.webp", "change text to 'AI POWERED'"],
157
+ ["data/1.jpg", "add futuristic style to background"],
158
+ ]
159
+ gr.Examples(
160
+ examples=examples,
161
+ inputs=[image_input, prompt_input]
162
  )
163
 
164
+ demo.launch()