Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ def save_binary_file(file_name, data):
|
|
12 |
f.write(data)
|
13 |
f.close()
|
14 |
|
15 |
-
def generate_image(prompt, image=None
|
16 |
# Initialize client with the API key
|
17 |
client = genai.Client(
|
18 |
api_key="AIzaSyAQcy3LfrkMy6DqS_8MqftAXu1Bx_ov_E8",
|
@@ -74,7 +74,7 @@ def generate_image(prompt, image=None, output_filename="generated_image"):
|
|
74 |
if chunk.candidates[0].content.parts[0].inline_data:
|
75 |
inline_data = chunk.candidates[0].content.parts[0].inline_data
|
76 |
file_extension = mimetypes.guess_extension(inline_data.mime_type)
|
77 |
-
filename = f"{
|
78 |
save_binary_file(filename, inline_data.data)
|
79 |
|
80 |
# Convert binary data to PIL Image
|
@@ -89,7 +89,7 @@ def generate_image(prompt, image=None, output_filename="generated_image"):
|
|
89 |
|
90 |
|
91 |
# Function to handle chat interaction
|
92 |
-
def chat_handler(prompt, user_image, chat_history
|
93 |
# Add the user prompt to the chat history - ONLY TEXT PROMPT for user message
|
94 |
if prompt:
|
95 |
chat_history.append({"role": "user", "content": prompt})
|
@@ -111,7 +111,7 @@ def chat_handler(prompt, user_image, chat_history, output_filename="generated_im
|
|
111 |
return chat_history, user_image, None, ""
|
112 |
|
113 |
# Generate image based on user input
|
114 |
-
img, status = generate_image(prompt or "Generate an image", user_image
|
115 |
|
116 |
thumbnail_data_uri = None # Initialize to None
|
117 |
if img:
|
@@ -169,11 +169,12 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
|
|
169 |
placeholder="Enter your image description here...",
|
170 |
lines=3
|
171 |
)
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
)
|
|
|
177 |
generate_btn = gr.Button("Generate Image")
|
178 |
|
179 |
# State to maintain chat history
|
@@ -182,14 +183,14 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
|
|
182 |
# Connect the button to the chat handler
|
183 |
generate_btn.click(
|
184 |
fn=chat_handler,
|
185 |
-
inputs=[prompt_input, image_input, chat_state, filename_input
|
186 |
outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
|
187 |
)
|
188 |
|
189 |
# Also allow Enter key to submit
|
190 |
prompt_input.submit(
|
191 |
fn=chat_handler,
|
192 |
-
inputs=[prompt_input, image_input, chat_state, filename_input
|
193 |
outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
|
194 |
)
|
195 |
|
|
|
12 |
f.write(data)
|
13 |
f.close()
|
14 |
|
15 |
+
def generate_image(prompt, image=None): # Removed output_filename parameter
|
16 |
# Initialize client with the API key
|
17 |
client = genai.Client(
|
18 |
api_key="AIzaSyAQcy3LfrkMy6DqS_8MqftAXu1Bx_ov_E8",
|
|
|
74 |
if chunk.candidates[0].content.parts[0].inline_data:
|
75 |
inline_data = chunk.candidates[0].content.parts[0].inline_data
|
76 |
file_extension = mimetypes.guess_extension(inline_data.mime_type)
|
77 |
+
filename = f"generated_image{file_extension}" # Hardcoded filename
|
78 |
save_binary_file(filename, inline_data.data)
|
79 |
|
80 |
# Convert binary data to PIL Image
|
|
|
89 |
|
90 |
|
91 |
# Function to handle chat interaction
|
92 |
+
def chat_handler(prompt, user_image, chat_history): # Removed output_filename parameter
|
93 |
# Add the user prompt to the chat history - ONLY TEXT PROMPT for user message
|
94 |
if prompt:
|
95 |
chat_history.append({"role": "user", "content": prompt})
|
|
|
111 |
return chat_history, user_image, None, ""
|
112 |
|
113 |
# Generate image based on user input
|
114 |
+
img, status = generate_image(prompt or "Generate an image", user_image) # Removed output_filename argument
|
115 |
|
116 |
thumbnail_data_uri = None # Initialize to None
|
117 |
if img:
|
|
|
169 |
placeholder="Enter your image description here...",
|
170 |
lines=3
|
171 |
)
|
172 |
+
# Removed filename_input
|
173 |
+
# filename_input = gr.Textbox(
|
174 |
+
# label="Output Filename",
|
175 |
+
# value="generated_image",
|
176 |
+
# placeholder="Enter desired filename (without extension)"
|
177 |
+
# )
|
178 |
generate_btn = gr.Button("Generate Image")
|
179 |
|
180 |
# State to maintain chat history
|
|
|
183 |
# Connect the button to the chat handler
|
184 |
generate_btn.click(
|
185 |
fn=chat_handler,
|
186 |
+
inputs=[prompt_input, image_input, chat_state], # Removed filename_input
|
187 |
outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
|
188 |
)
|
189 |
|
190 |
# Also allow Enter key to submit
|
191 |
prompt_input.submit(
|
192 |
fn=chat_handler,
|
193 |
+
inputs=[prompt_input, image_input, chat_state], # Removed filename_input
|
194 |
outputs=[chatbot, uploaded_image_output, generated_image_output, prompt_input]
|
195 |
)
|
196 |
|