Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,16 +4,16 @@ import mimetypes
|
|
4 |
import tempfile
|
5 |
import time
|
6 |
import google.generativeai as genai
|
7 |
-
from google.generativeai import types
|
8 |
import gradio as gr
|
|
|
9 |
|
10 |
-
def generate_image(api_key, prompt, file_name):
|
11 |
# Validate inputs
|
12 |
if not api_key:
|
13 |
return None, "Please enter your Gemini API key"
|
14 |
|
15 |
-
if not prompt:
|
16 |
-
return None, "Please enter a prompt
|
17 |
|
18 |
if not file_name:
|
19 |
file_name = f"gemini_image_{int(time.time())}"
|
@@ -40,9 +40,27 @@ def generate_image(api_key, prompt, file_name):
|
|
40 |
generated_image_path = None
|
41 |
generation_text = ""
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
# Generate the content
|
44 |
response = model.generate_content(
|
45 |
-
|
46 |
generation_config=generation_config,
|
47 |
stream=True
|
48 |
)
|
@@ -67,7 +85,7 @@ def generate_image(api_key, prompt, file_name):
|
|
67 |
if generated_image_path:
|
68 |
return generated_image_path, generation_text
|
69 |
else:
|
70 |
-
return None, "No image was generated. Try a different prompt."
|
71 |
|
72 |
except Exception as e:
|
73 |
return None, f"Error: {str(e)}"
|
@@ -89,6 +107,10 @@ def create_ui():
|
|
89 |
placeholder="Describe the image you want to generate",
|
90 |
lines=3
|
91 |
)
|
|
|
|
|
|
|
|
|
92 |
file_name = gr.Textbox(
|
93 |
label="Output File Name (optional)",
|
94 |
placeholder="Enter a file name (without extension)"
|
@@ -101,7 +123,7 @@ def create_ui():
|
|
101 |
|
102 |
generate_btn.click(
|
103 |
fn=generate_image,
|
104 |
-
inputs=[api_key, prompt, file_name],
|
105 |
outputs=[output_image, output_text]
|
106 |
)
|
107 |
|
@@ -109,11 +131,13 @@ def create_ui():
|
|
109 |
## How to use
|
110 |
1. Enter your Gemini API key (get one from https://ai.google.dev/)
|
111 |
2. Write a detailed prompt describing the image you want to generate
|
112 |
-
3. (Optional)
|
113 |
-
4.
|
|
|
114 |
|
115 |
## Notes
|
116 |
- The model used is `gemini-2.0-flash-exp-image-generation`
|
|
|
117 |
- All safety filters are set to "BLOCK_NONE" - use responsibly
|
118 |
- Image generation may take a few seconds to complete
|
119 |
""")
|
|
|
4 |
import tempfile
|
5 |
import time
|
6 |
import google.generativeai as genai
|
|
|
7 |
import gradio as gr
|
8 |
+
from PIL import Image as PILImage
|
9 |
|
10 |
+
def generate_image(api_key, prompt, input_image, file_name):
|
11 |
# Validate inputs
|
12 |
if not api_key:
|
13 |
return None, "Please enter your Gemini API key"
|
14 |
|
15 |
+
if not prompt and input_image is None:
|
16 |
+
return None, "Please enter a prompt and/or upload an input image"
|
17 |
|
18 |
if not file_name:
|
19 |
file_name = f"gemini_image_{int(time.time())}"
|
|
|
40 |
generated_image_path = None
|
41 |
generation_text = ""
|
42 |
|
43 |
+
# Prepare content based on inputs
|
44 |
+
content = []
|
45 |
+
|
46 |
+
# Add text prompt if provided
|
47 |
+
if prompt:
|
48 |
+
content.append(prompt)
|
49 |
+
|
50 |
+
# Add image if provided
|
51 |
+
if input_image is not None:
|
52 |
+
# If the input is a file path (string), open the image
|
53 |
+
if isinstance(input_image, str):
|
54 |
+
img = PILImage.open(input_image)
|
55 |
+
content.append(img)
|
56 |
+
# If the input is already a numpy array from Gradio
|
57 |
+
else:
|
58 |
+
img = PILImage.fromarray(input_image)
|
59 |
+
content.append(img)
|
60 |
+
|
61 |
# Generate the content
|
62 |
response = model.generate_content(
|
63 |
+
content,
|
64 |
generation_config=generation_config,
|
65 |
stream=True
|
66 |
)
|
|
|
85 |
if generated_image_path:
|
86 |
return generated_image_path, generation_text
|
87 |
else:
|
88 |
+
return None, "No image was generated. Try a different prompt or input image."
|
89 |
|
90 |
except Exception as e:
|
91 |
return None, f"Error: {str(e)}"
|
|
|
107 |
placeholder="Describe the image you want to generate",
|
108 |
lines=3
|
109 |
)
|
110 |
+
input_image = gr.Image(
|
111 |
+
label="Input Image (Optional)",
|
112 |
+
type="numpy"
|
113 |
+
)
|
114 |
file_name = gr.Textbox(
|
115 |
label="Output File Name (optional)",
|
116 |
placeholder="Enter a file name (without extension)"
|
|
|
123 |
|
124 |
generate_btn.click(
|
125 |
fn=generate_image,
|
126 |
+
inputs=[api_key, prompt, input_image, file_name],
|
127 |
outputs=[output_image, output_text]
|
128 |
)
|
129 |
|
|
|
131 |
## How to use
|
132 |
1. Enter your Gemini API key (get one from https://ai.google.dev/)
|
133 |
2. Write a detailed prompt describing the image you want to generate
|
134 |
+
3. (Optional) Upload an input image to influence the generation
|
135 |
+
4. (Optional) Provide a file name for your generated image
|
136 |
+
5. Click "Generate Image" and wait for the result
|
137 |
|
138 |
## Notes
|
139 |
- The model used is `gemini-2.0-flash-exp-image-generation`
|
140 |
+
- You can use text prompts, input images, or both together
|
141 |
- All safety filters are set to "BLOCK_NONE" - use responsibly
|
142 |
- Image generation may take a few seconds to complete
|
143 |
""")
|