Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,17 +3,19 @@ import os
|
|
3 |
import mimetypes
|
4 |
from google import genai
|
5 |
from google.genai import types
|
6 |
-
|
|
|
|
|
7 |
|
8 |
def save_binary_file(file_name, data):
|
9 |
f = open(file_name, "wb")
|
10 |
f.write(data)
|
11 |
f.close()
|
12 |
|
13 |
-
|
14 |
-
|
15 |
client = genai.Client(
|
16 |
-
api_key=
|
17 |
)
|
18 |
|
19 |
model = "gemini-2.0-flash-exp-image-generation"
|
@@ -21,7 +23,7 @@ def generate():
|
|
21 |
types.Content(
|
22 |
role="user",
|
23 |
parts=[
|
24 |
-
types.Part.from_text(text=
|
25 |
],
|
26 |
),
|
27 |
]
|
@@ -37,33 +39,66 @@ def generate():
|
|
37 |
safety_settings=[
|
38 |
types.SafetySetting(
|
39 |
category="HARM_CATEGORY_CIVIC_INTEGRITY",
|
40 |
-
threshold="OFF",
|
41 |
),
|
42 |
],
|
43 |
response_mime_type="text/plain",
|
44 |
)
|
45 |
|
46 |
-
|
|
|
47 |
model=model,
|
48 |
contents=contents,
|
49 |
config=generate_content_config,
|
50 |
-
)
|
|
|
|
|
|
|
51 |
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
52 |
continue
|
53 |
if chunk.candidates[0].content.parts[0].inline_data:
|
54 |
-
file_name = "ENTER_FILE_NAME"
|
55 |
inline_data = chunk.candidates[0].content.parts[0].inline_data
|
56 |
file_extension = mimetypes.guess_extension(inline_data.mime_type)
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
)
|
60 |
-
|
61 |
-
"
|
62 |
-
|
63 |
-
|
64 |
)
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
-
|
|
|
3 |
import mimetypes
|
4 |
from google import genai
|
5 |
from google.genai import types
|
6 |
+
import gradio as gr
|
7 |
+
import io
|
8 |
+
from PIL import Image
|
9 |
|
10 |
def save_binary_file(file_name, data):
|
11 |
f = open(file_name, "wb")
|
12 |
f.write(data)
|
13 |
f.close()
|
14 |
|
15 |
+
def generate_image(prompt, output_filename="generated_image"):
|
16 |
+
# Initialize client with the API key
|
17 |
client = genai.Client(
|
18 |
+
api_key="AIzaSyAQcy3LfrkMy6DqS_8MqftAXu1Bx_ov_E8",
|
19 |
)
|
20 |
|
21 |
model = "gemini-2.0-flash-exp-image-generation"
|
|
|
23 |
types.Content(
|
24 |
role="user",
|
25 |
parts=[
|
26 |
+
types.Part.from_text(text=prompt),
|
27 |
],
|
28 |
),
|
29 |
]
|
|
|
39 |
safety_settings=[
|
40 |
types.SafetySetting(
|
41 |
category="HARM_CATEGORY_CIVIC_INTEGRITY",
|
42 |
+
threshold="OFF",
|
43 |
),
|
44 |
],
|
45 |
response_mime_type="text/plain",
|
46 |
)
|
47 |
|
48 |
+
# Generate the content
|
49 |
+
response = client.models.generate_content_stream(
|
50 |
model=model,
|
51 |
contents=contents,
|
52 |
config=generate_content_config,
|
53 |
+
)
|
54 |
+
|
55 |
+
# Process the response
|
56 |
+
for chunk in response:
|
57 |
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
58 |
continue
|
59 |
if chunk.candidates[0].content.parts[0].inline_data:
|
|
|
60 |
inline_data = chunk.candidates[0].content.parts[0].inline_data
|
61 |
file_extension = mimetypes.guess_extension(inline_data.mime_type)
|
62 |
+
filename = f"{output_filename}{file_extension}"
|
63 |
+
save_binary_file(filename, inline_data.data)
|
64 |
+
|
65 |
+
# Convert binary data to PIL Image for Gradio display
|
66 |
+
img = Image.open(io.BytesIO(inline_data.data))
|
67 |
+
return img, f"Image saved as {filename}"
|
68 |
+
else:
|
69 |
+
return None, chunk.text
|
70 |
+
|
71 |
+
return None, "No image generated"
|
72 |
+
|
73 |
+
# Create Gradio interface
|
74 |
+
with gr.Blocks(title="Image Generation App") as demo:
|
75 |
+
gr.Markdown("# Image Generation with Gemini")
|
76 |
+
gr.Markdown("Enter a prompt to generate an image using Google's Gemini model")
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
with gr.Column():
|
80 |
+
prompt_input = gr.Textbox(
|
81 |
+
label="Prompt",
|
82 |
+
placeholder="Enter your image description here...",
|
83 |
+
lines=3
|
84 |
)
|
85 |
+
filename_input = gr.Textbox(
|
86 |
+
label="Output Filename",
|
87 |
+
value="generated_image",
|
88 |
+
placeholder="Enter desired filename (without extension)"
|
89 |
)
|
90 |
+
generate_btn = gr.Button("Generate Image")
|
91 |
+
|
92 |
+
with gr.Column():
|
93 |
+
image_output = gr.Image(label="Generated Image")
|
94 |
+
text_output = gr.Textbox(label="Status")
|
95 |
+
|
96 |
+
# Connect the button to the generation function
|
97 |
+
generate_btn.click(
|
98 |
+
fn=generate_image,
|
99 |
+
inputs=[prompt_input, filename_input],
|
100 |
+
outputs=[image_output, text_output]
|
101 |
+
)
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
+
demo.launch()
|