Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,25 +6,37 @@ 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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
contents = [
|
23 |
types.Content(
|
24 |
role="user",
|
25 |
-
parts=
|
26 |
-
types.Part.from_text(text=prompt),
|
27 |
-
],
|
28 |
),
|
29 |
]
|
30 |
generate_content_config = types.GenerateContentConfig(
|
@@ -62,7 +74,7 @@ def generate_image(prompt, output_filename="generated_image"):
|
|
62 |
filename = f"{output_filename}{file_extension}"
|
63 |
save_binary_file(filename, inline_data.data)
|
64 |
|
65 |
-
# Convert binary data to PIL Image
|
66 |
img = Image.open(io.BytesIO(inline_data.data))
|
67 |
return img, f"Image saved as {filename}"
|
68 |
else:
|
@@ -71,64 +83,88 @@ def generate_image(prompt, output_filename="generated_image"):
|
|
71 |
return None, "No image generated"
|
72 |
|
73 |
# Function to handle chat interaction
|
74 |
-
def chat_handler(user_input,
|
75 |
# Add user message to chat history
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
# Generate image based on user input
|
79 |
-
img, status = generate_image(user_input)
|
80 |
|
81 |
# Add AI response to chat history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
chat_history.append({"role": "assistant", "content": status})
|
83 |
|
84 |
-
return chat_history,
|
85 |
|
86 |
# Create Gradio interface with chatbot layout
|
87 |
with gr.Blocks(title="Image Editing Chatbot") as demo:
|
88 |
gr.Markdown("# Image Editing Chatbot")
|
89 |
-
gr.Markdown("
|
90 |
|
91 |
-
# Chatbot display area for
|
92 |
chatbot = gr.Chatbot(
|
93 |
label="Chat",
|
94 |
-
height=
|
95 |
type="messages", # Explicitly set to 'messages' format
|
96 |
avatar_images=(None, None) # No avatars for simplicity
|
97 |
)
|
98 |
|
99 |
-
# Image display area
|
100 |
-
image_output = gr.Image(
|
101 |
-
label="Generated Image",
|
102 |
-
height=400
|
103 |
-
)
|
104 |
-
|
105 |
# Input area
|
106 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
prompt_input = gr.Textbox(
|
108 |
label="",
|
109 |
placeholder="Type something",
|
110 |
show_label=False,
|
111 |
container=False,
|
112 |
-
scale=
|
113 |
)
|
|
|
114 |
run_btn = gr.Button("Run", scale=1)
|
115 |
|
116 |
# State to maintain chat history
|
117 |
chat_state = gr.State([])
|
118 |
-
image_state = gr.State(None)
|
119 |
|
120 |
# Connect the button to the chat handler
|
121 |
run_btn.click(
|
122 |
fn=chat_handler,
|
123 |
-
inputs=[prompt_input,
|
124 |
-
outputs=[chatbot,
|
125 |
)
|
126 |
|
127 |
# Also allow Enter key to submit
|
128 |
prompt_input.submit(
|
129 |
fn=chat_handler,
|
130 |
-
inputs=[prompt_input,
|
131 |
-
outputs=[chatbot,
|
132 |
)
|
133 |
|
134 |
if __name__ == "__main__":
|
|
|
6 |
import gradio as gr
|
7 |
import io
|
8 |
from PIL import Image
|
9 |
+
import tempfile
|
10 |
|
11 |
def save_binary_file(file_name, data):
|
12 |
f = open(file_name, "wb")
|
13 |
f.write(data)
|
14 |
f.close()
|
15 |
|
16 |
+
def generate_image(prompt, image=None, output_filename="generated_image"):
|
17 |
# Initialize client with the API key
|
18 |
client = genai.Client(
|
19 |
api_key="AIzaSyAQcy3LfrkMy6DqS_8MqftAXu1Bx_ov_E8",
|
20 |
)
|
21 |
|
22 |
model = "gemini-2.0-flash-exp-image-generation"
|
23 |
+
parts = [types.Part.from_text(text=prompt)]
|
24 |
+
|
25 |
+
# If an image is provided, add it to the content
|
26 |
+
if image:
|
27 |
+
# Convert PIL Image to bytes
|
28 |
+
img_byte_arr = io.BytesIO()
|
29 |
+
image.save(img_byte_arr, format="PNG")
|
30 |
+
img_bytes = img_byte_arr.getvalue()
|
31 |
+
parts.append(types.Part.from_inline_data(
|
32 |
+
mime_type="image/png",
|
33 |
+
data=img_bytes
|
34 |
+
))
|
35 |
+
|
36 |
contents = [
|
37 |
types.Content(
|
38 |
role="user",
|
39 |
+
parts=parts,
|
|
|
|
|
40 |
),
|
41 |
]
|
42 |
generate_content_config = types.GenerateContentConfig(
|
|
|
74 |
filename = f"{output_filename}{file_extension}"
|
75 |
save_binary_file(filename, inline_data.data)
|
76 |
|
77 |
+
# Convert binary data to PIL Image
|
78 |
img = Image.open(io.BytesIO(inline_data.data))
|
79 |
return img, f"Image saved as {filename}"
|
80 |
else:
|
|
|
83 |
return None, "No image generated"
|
84 |
|
85 |
# Function to handle chat interaction
|
86 |
+
def chat_handler(user_input, user_image, chat_history):
|
87 |
# Add user message to chat history
|
88 |
+
if user_image:
|
89 |
+
# Save the uploaded image to a temporary file so Gradio can display it
|
90 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
91 |
+
user_image.save(tmp_file.name)
|
92 |
+
# Add the image to the chat history
|
93 |
+
chat_history.append({"role": "user", "content": tmp_file.name})
|
94 |
|
95 |
+
# Add the text prompt to the chat history
|
96 |
+
if user_input:
|
97 |
+
chat_history.append({"role": "user", "content": user_input})
|
98 |
+
|
99 |
+
# If no input (neither text nor image), return early
|
100 |
+
if not user_input and not user_image:
|
101 |
+
chat_history.append({"role": "assistant", "content": "Please provide a prompt or an image."})
|
102 |
+
return chat_history, None, ""
|
103 |
+
|
104 |
# Generate image based on user input
|
105 |
+
img, status = generate_image(user_input or "Generate an image", user_image)
|
106 |
|
107 |
# Add AI response to chat history
|
108 |
+
if img:
|
109 |
+
# Save the PIL Image to a temporary file so Gradio can display it
|
110 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
|
111 |
+
img.save(tmp_file.name)
|
112 |
+
# Add the image as a file path that Gradio can serve
|
113 |
+
chat_history.append({"role": "assistant", "content": tmp_file.name})
|
114 |
+
|
115 |
+
# Add the status message
|
116 |
chat_history.append({"role": "assistant", "content": status})
|
117 |
|
118 |
+
return chat_history, None, ""
|
119 |
|
120 |
# Create Gradio interface with chatbot layout
|
121 |
with gr.Blocks(title="Image Editing Chatbot") as demo:
|
122 |
gr.Markdown("# Image Editing Chatbot")
|
123 |
+
gr.Markdown("Upload an image and/or type a prompt to generate or edit an image using Google's Gemini model")
|
124 |
|
125 |
+
# Chatbot display area for the conversation thread
|
126 |
chatbot = gr.Chatbot(
|
127 |
label="Chat",
|
128 |
+
height=400,
|
129 |
type="messages", # Explicitly set to 'messages' format
|
130 |
avatar_images=(None, None) # No avatars for simplicity
|
131 |
)
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
# Input area
|
134 |
with gr.Row():
|
135 |
+
# Image upload button
|
136 |
+
image_input = gr.Image(
|
137 |
+
label="Upload Image",
|
138 |
+
type="pil",
|
139 |
+
scale=1,
|
140 |
+
height=100
|
141 |
+
)
|
142 |
+
# Text input
|
143 |
prompt_input = gr.Textbox(
|
144 |
label="",
|
145 |
placeholder="Type something",
|
146 |
show_label=False,
|
147 |
container=False,
|
148 |
+
scale=3
|
149 |
)
|
150 |
+
# Run button
|
151 |
run_btn = gr.Button("Run", scale=1)
|
152 |
|
153 |
# State to maintain chat history
|
154 |
chat_state = gr.State([])
|
|
|
155 |
|
156 |
# Connect the button to the chat handler
|
157 |
run_btn.click(
|
158 |
fn=chat_handler,
|
159 |
+
inputs=[prompt_input, image_input, chat_state],
|
160 |
+
outputs=[chatbot, image_input, prompt_input]
|
161 |
)
|
162 |
|
163 |
# Also allow Enter key to submit
|
164 |
prompt_input.submit(
|
165 |
fn=chat_handler,
|
166 |
+
inputs=[prompt_input, image_input, chat_state],
|
167 |
+
outputs=[chatbot, image_input, prompt_input]
|
168 |
)
|
169 |
|
170 |
if __name__ == "__main__":
|