Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,168 +1,29 @@
|
|
1 |
-
|
2 |
-
import os
|
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 |
-
import uuid
|
10 |
-
|
11 |
-
# Create a static directory to store images
|
12 |
-
STATIC_DIR = "static"
|
13 |
-
if not os.path.exists(STATIC_DIR):
|
14 |
-
os.makedirs(STATIC_DIR)
|
15 |
-
|
16 |
-
def save_binary_file(file_name, data):
|
17 |
-
f = open(file_name, "wb")
|
18 |
-
f.write(data)
|
19 |
-
f.close()
|
20 |
-
|
21 |
-
def save_image_to_static(img, prefix="image"):
|
22 |
-
# Generate a unique filename
|
23 |
-
filename = f"{prefix}_{uuid.uuid4().hex}.png"
|
24 |
-
filepath = os.path.join(STATIC_DIR, filename)
|
25 |
-
img.save(filepath, format="PNG")
|
26 |
-
# Return the relative path
|
27 |
-
return filepath
|
28 |
-
|
29 |
-
def get_file_url(filepath):
|
30 |
-
# Convert the relative file path to a URL that Gradio can serve
|
31 |
-
# Gradio serves files using the /file/ prefix
|
32 |
-
return f"/file/{filepath}"
|
33 |
-
|
34 |
-
def generate_image(prompt, image=None, output_filename="generated_image"):
|
35 |
-
# Initialize client with the API key
|
36 |
-
client = genai.Client(
|
37 |
-
api_key="AIzaSyAQcy3LfrkMy6DqS_8MqftAXu1Bx_ov_E8",
|
38 |
-
)
|
39 |
-
|
40 |
-
model = "gemini-2.0-flash-exp-image-generation"
|
41 |
-
parts = [types.Part.from_text(text=prompt)]
|
42 |
-
|
43 |
-
# If an image is provided, add it to the content
|
44 |
-
if image:
|
45 |
-
# Convert PIL Image to bytes
|
46 |
-
img_byte_arr = io.BytesIO()
|
47 |
-
image.save(img_byte_arr, format="PNG")
|
48 |
-
img_bytes = img_byte_arr.getvalue()
|
49 |
-
# Add the image as a Part with inline_data
|
50 |
-
parts.append({
|
51 |
-
"inline_data": {
|
52 |
-
"mime_type": "image/png",
|
53 |
-
"data": img_bytes
|
54 |
-
}
|
55 |
-
})
|
56 |
-
|
57 |
-
contents = [
|
58 |
-
types.Content(
|
59 |
-
role="user",
|
60 |
-
parts=parts,
|
61 |
-
),
|
62 |
-
]
|
63 |
-
generate_content_config = types.GenerateContentConfig(
|
64 |
-
temperature=1,
|
65 |
-
top_p=0.95,
|
66 |
-
top_k=40,
|
67 |
-
max_output_tokens=8192,
|
68 |
-
response_modalities=[
|
69 |
-
"image",
|
70 |
-
"text",
|
71 |
-
],
|
72 |
-
safety_settings=[
|
73 |
-
types.SafetySetting(
|
74 |
-
category="HARM_CATEGORY_CIVIC_INTEGRITY",
|
75 |
-
threshold="OFF",
|
76 |
-
),
|
77 |
-
],
|
78 |
-
response_mime_type="text/plain",
|
79 |
-
)
|
80 |
-
|
81 |
-
# Generate the content
|
82 |
-
response = client.models.generate_content_stream(
|
83 |
-
model=model,
|
84 |
-
contents=contents,
|
85 |
-
config=generate_content_config,
|
86 |
-
)
|
87 |
-
|
88 |
-
# Process the response
|
89 |
-
for chunk in response:
|
90 |
-
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts:
|
91 |
-
continue
|
92 |
-
if chunk.candidates[0].content.parts[0].inline_data:
|
93 |
-
inline_data = chunk.candidates[0].content.parts[0].inline_data
|
94 |
-
file_extension = mimetypes.guess_extension(inline_data.mime_type)
|
95 |
-
filename = f"{output_filename}{file_extension}"
|
96 |
-
save_binary_file(filename, inline_data.data)
|
97 |
-
|
98 |
-
# Convert binary data to PIL Image
|
99 |
-
img = Image.open(io.BytesIO(inline_data.data))
|
100 |
-
return img, f"Image saved as {filename}"
|
101 |
-
else:
|
102 |
-
return None, chunk.text
|
103 |
-
|
104 |
-
return None, "No image generated"
|
105 |
-
|
106 |
-
# Function to handle chat interaction
|
107 |
-
def chat_handler(user_input, user_image, chat_history):
|
108 |
-
# Add user message to chat history
|
109 |
-
if user_image:
|
110 |
-
# Save the uploaded image to the static directory
|
111 |
-
img_path = save_image_to_static(user_image, prefix="uploaded")
|
112 |
-
# Convert the file path to a URL that Gradio can serve
|
113 |
-
img_url = get_file_url(img_path)
|
114 |
-
# Add the image URL to the chat history
|
115 |
-
chat_history.append({"role": "user", "content": img_url})
|
116 |
-
|
117 |
-
# Add the text prompt to the chat history
|
118 |
-
if user_input:
|
119 |
-
chat_history.append({"role": "user", "content": user_input})
|
120 |
-
|
121 |
-
# If no input (neither text nor image), return early
|
122 |
-
if not user_input and not user_image:
|
123 |
-
chat_history.append({"role": "assistant", "content": "Please provide a prompt or an image."})
|
124 |
-
return chat_history, None, ""
|
125 |
-
|
126 |
-
# Generate image based on user input
|
127 |
-
img, status = generate_image(user_input or "Generate an image", user_image)
|
128 |
-
|
129 |
-
# Add AI response to chat history
|
130 |
-
if img:
|
131 |
-
# Save the generated image to the static directory
|
132 |
-
img_path = save_image_to_static(img, prefix="generated")
|
133 |
-
# Convert the file path to a URL that Gradio can serve
|
134 |
-
img_url = get_file_url(img_path)
|
135 |
-
# Add the image URL to the chat history
|
136 |
-
chat_history.append({"role": "assistant", "content": img_url})
|
137 |
-
|
138 |
-
# Add the status message
|
139 |
-
chat_history.append({"role": "assistant", "content": status})
|
140 |
-
|
141 |
-
return chat_history, None, ""
|
142 |
-
|
143 |
-
# Create Gradio interface with chatbot layout
|
144 |
with gr.Blocks(title="Image Editing Chatbot") as demo:
|
145 |
gr.Markdown("# Image Editing Chatbot")
|
146 |
gr.Markdown("Upload an image and/or type a prompt to generate or edit an image using Google's Gemini model")
|
147 |
|
148 |
-
# Chatbot display area for
|
149 |
chatbot = gr.Chatbot(
|
150 |
label="Chat",
|
151 |
height=300,
|
152 |
-
type="messages",
|
153 |
-
avatar_images=(None, None)
|
154 |
)
|
155 |
|
|
|
|
|
|
|
|
|
|
|
156 |
# Input area
|
157 |
with gr.Row():
|
158 |
-
# Image upload button
|
159 |
image_input = gr.Image(
|
160 |
label="Upload Image",
|
161 |
type="pil",
|
162 |
scale=1,
|
163 |
height=100
|
164 |
)
|
165 |
-
# Text input
|
166 |
prompt_input = gr.Textbox(
|
167 |
label="",
|
168 |
placeholder="Type something",
|
@@ -170,24 +31,41 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
|
|
170 |
container=False,
|
171 |
scale=3
|
172 |
)
|
173 |
-
# Run button
|
174 |
run_btn = gr.Button("Run", scale=1)
|
175 |
|
176 |
# State to maintain chat history
|
177 |
chat_state = gr.State([])
|
178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
# Connect the button to the chat handler
|
180 |
run_btn.click(
|
181 |
fn=chat_handler,
|
182 |
inputs=[prompt_input, image_input, chat_state],
|
183 |
-
outputs=[chatbot, image_input, prompt_input]
|
184 |
)
|
185 |
|
186 |
# Also allow Enter key to submit
|
187 |
prompt_input.submit(
|
188 |
fn=chat_handler,
|
189 |
inputs=[prompt_input, image_input, chat_state],
|
190 |
-
outputs=[chatbot, image_input, prompt_input]
|
191 |
)
|
192 |
|
193 |
if __name__ == "__main__":
|
|
|
1 |
+
# Alternative layout using a separate gr.Image component
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
with gr.Blocks(title="Image Editing Chatbot") as demo:
|
3 |
gr.Markdown("# Image Editing Chatbot")
|
4 |
gr.Markdown("Upload an image and/or type a prompt to generate or edit an image using Google's Gemini model")
|
5 |
|
6 |
+
# Chatbot display area for text messages
|
7 |
chatbot = gr.Chatbot(
|
8 |
label="Chat",
|
9 |
height=300,
|
10 |
+
type="messages",
|
11 |
+
avatar_images=(None, None)
|
12 |
)
|
13 |
|
14 |
+
# Separate image outputs
|
15 |
+
with gr.Row():
|
16 |
+
uploaded_image_output = gr.Image(label="Uploaded Image")
|
17 |
+
generated_image_output = gr.Image(label="Generated Image")
|
18 |
+
|
19 |
# Input area
|
20 |
with gr.Row():
|
|
|
21 |
image_input = gr.Image(
|
22 |
label="Upload Image",
|
23 |
type="pil",
|
24 |
scale=1,
|
25 |
height=100
|
26 |
)
|
|
|
27 |
prompt_input = gr.Textbox(
|
28 |
label="",
|
29 |
placeholder="Type something",
|
|
|
31 |
container=False,
|
32 |
scale=3
|
33 |
)
|
|
|
34 |
run_btn = gr.Button("Run", scale=1)
|
35 |
|
36 |
# State to maintain chat history
|
37 |
chat_state = gr.State([])
|
38 |
|
39 |
+
def chat_handler(user_input, user_image, chat_history):
|
40 |
+
# Add user message to chat history
|
41 |
+
if user_input:
|
42 |
+
chat_history.append({"role": "user", "content": user_input})
|
43 |
+
|
44 |
+
# If no input, return early
|
45 |
+
if not user_input and not user_image:
|
46 |
+
chat_history.append({"role": "assistant", "content": "Please provide a prompt or an image."})
|
47 |
+
return chat_history, None, user_image, None, ""
|
48 |
+
|
49 |
+
# Generate image based on user input
|
50 |
+
img, status = generate_image(user_input or "Generate an image", user_image)
|
51 |
+
|
52 |
+
# Add AI response to chat history
|
53 |
+
chat_history.append({"role": "assistant", "content": status})
|
54 |
+
|
55 |
+
return chat_history, None, user_image, img, ""
|
56 |
+
|
57 |
# Connect the button to the chat handler
|
58 |
run_btn.click(
|
59 |
fn=chat_handler,
|
60 |
inputs=[prompt_input, image_input, chat_state],
|
61 |
+
outputs=[chatbot, image_input, uploaded_image_output, generated_image_output, prompt_input]
|
62 |
)
|
63 |
|
64 |
# Also allow Enter key to submit
|
65 |
prompt_input.submit(
|
66 |
fn=chat_handler,
|
67 |
inputs=[prompt_input, image_input, chat_state],
|
68 |
+
outputs=[chatbot, image_input, uploaded_image_output, generated_image_output, prompt_input]
|
69 |
)
|
70 |
|
71 |
if __name__ == "__main__":
|