Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import os
|
3 |
+
import mimetypes
|
4 |
+
import tempfile
|
5 |
+
import time
|
6 |
+
from google import genai
|
7 |
+
from google.genai 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 for image generation"
|
17 |
+
|
18 |
+
if not file_name:
|
19 |
+
file_name = f"gemini_image_{int(time.time())}"
|
20 |
+
|
21 |
+
try:
|
22 |
+
# Set up the client
|
23 |
+
client = genai.Client(api_key=api_key)
|
24 |
+
model = "gemini-2.0-flash-exp-image-generation"
|
25 |
+
|
26 |
+
# Create content
|
27 |
+
contents = [
|
28 |
+
types.Content(
|
29 |
+
role="user",
|
30 |
+
parts=[types.Part.from_text(text=prompt)],
|
31 |
+
),
|
32 |
+
]
|
33 |
+
|
34 |
+
# Configure generation settings
|
35 |
+
generate_content_config = types.GenerateContentConfig(
|
36 |
+
response_modalities=["image", "text"],
|
37 |
+
safety_settings=[
|
38 |
+
types.SafetySetting(category="HARM_CATEGORY_HARASSMENT", threshold="BLOCK_NONE"),
|
39 |
+
types.SafetySetting(category="HARM_CATEGORY_HATE_SPEECH", threshold="BLOCK_NONE"),
|
40 |
+
types.SafetySetting(category="HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold="BLOCK_NONE"),
|
41 |
+
types.SafetySetting(category="HARM_CATEGORY_DANGEROUS_CONTENT", threshold="BLOCK_NONE"),
|
42 |
+
types.SafetySetting(category="HARM_CATEGORY_CIVIC_INTEGRITY", threshold="BLOCK_NONE"),
|
43 |
+
],
|
44 |
+
response_mime_type="text/plain",
|
45 |
+
)
|
46 |
+
|
47 |
+
# Create a temporary directory to store the image
|
48 |
+
temp_dir = tempfile.mkdtemp()
|
49 |
+
generated_image_path = None
|
50 |
+
generation_text = ""
|
51 |
+
|
52 |
+
# Stream the content
|
53 |
+
for chunk in client.models.generate_content_stream(
|
54 |
+
model=model,
|
55 |
+
contents=contents,
|
56 |
+
config=generate_content_config,
|
57 |
+
):
|
58 |
+
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
59 |
+
continue
|
60 |
+
|
61 |
+
if chunk.candidates[0].content.parts[0].inline_data:
|
62 |
+
inline_data = chunk.candidates[0].content.parts[0].inline_data
|
63 |
+
file_extension = mimetypes.guess_extension(inline_data.mime_type) or '.jpg'
|
64 |
+
generated_image_path = os.path.join(temp_dir, f"{file_name}{file_extension}")
|
65 |
+
|
66 |
+
with open(generated_image_path, "wb") as f:
|
67 |
+
f.write(inline_data.data)
|
68 |
+
|
69 |
+
generation_text += f"Image of type {inline_data.mime_type} generated successfully."
|
70 |
+
else:
|
71 |
+
generation_text += chunk.text
|
72 |
+
|
73 |
+
if generated_image_path:
|
74 |
+
return generated_image_path, generation_text
|
75 |
+
else:
|
76 |
+
return None, "No image was generated. Try a different prompt."
|
77 |
+
|
78 |
+
except Exception as e:
|
79 |
+
return None, f"Error: {str(e)}"
|
80 |
+
|
81 |
+
def create_ui():
|
82 |
+
with gr.Blocks(title="Gemini Image Generator") as demo:
|
83 |
+
gr.Markdown("# Gemini Image Generator")
|
84 |
+
gr.Markdown("Generate images using Google's Gemini 2.0 Flash Image Generation model")
|
85 |
+
|
86 |
+
with gr.Row():
|
87 |
+
with gr.Column():
|
88 |
+
api_key = gr.Textbox(
|
89 |
+
label="Gemini API Key",
|
90 |
+
placeholder="Enter your Gemini API key here",
|
91 |
+
type="password"
|
92 |
+
)
|
93 |
+
prompt = gr.Textbox(
|
94 |
+
label="Prompt",
|
95 |
+
placeholder="Describe the image you want to generate",
|
96 |
+
lines=3
|
97 |
+
)
|
98 |
+
file_name = gr.Textbox(
|
99 |
+
label="Output File Name (optional)",
|
100 |
+
placeholder="Enter a file name (without extension)"
|
101 |
+
)
|
102 |
+
generate_btn = gr.Button("Generate Image", variant="primary")
|
103 |
+
|
104 |
+
with gr.Column():
|
105 |
+
output_image = gr.Image(label="Generated Image", type="filepath")
|
106 |
+
output_text = gr.Textbox(label="Generation Info", lines=2)
|
107 |
+
|
108 |
+
generate_btn.click(
|
109 |
+
fn=generate_image,
|
110 |
+
inputs=[api_key, prompt, file_name],
|
111 |
+
outputs=[output_image, output_text]
|
112 |
+
)
|
113 |
+
|
114 |
+
gr.Markdown("""
|
115 |
+
## How to use
|
116 |
+
1. Enter your Gemini API key (get one from https://ai.google.dev/)
|
117 |
+
2. Write a detailed prompt describing the image you want to generate
|
118 |
+
3. (Optional) Provide a file name for your generated image
|
119 |
+
4. Click "Generate Image" and wait for the result
|
120 |
+
|
121 |
+
## Notes
|
122 |
+
- The model used is `gemini-2.0-flash-exp-image-generation`
|
123 |
+
- All safety filters are set to "BLOCK_NONE" - use responsibly
|
124 |
+
- Image generation may take a few seconds to complete
|
125 |
+
""")
|
126 |
+
|
127 |
+
return demo
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
demo = create_ui()
|
131 |
+
demo.launch()
|