Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,13 +6,20 @@ from google.genai import types
|
|
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(
|
@@ -89,11 +96,10 @@ def generate_image(prompt, image=None, output_filename="generated_image"):
|
|
89 |
def chat_handler(user_input, user_image, chat_history):
|
90 |
# Add user message to chat history
|
91 |
if user_image:
|
92 |
-
#
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
chat_history.append({"role": "user", "content": tmp_file.name})
|
97 |
|
98 |
# Add the text prompt to the chat history
|
99 |
if user_input:
|
@@ -109,11 +115,10 @@ def chat_handler(user_input, user_image, chat_history):
|
|
109 |
|
110 |
# Add AI response to chat history
|
111 |
if img:
|
112 |
-
#
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
chat_history.append({"role": "assistant", "content": tmp_file.name})
|
117 |
|
118 |
# Add the status message
|
119 |
chat_history.append({"role": "assistant", "content": status})
|
@@ -128,7 +133,7 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
|
|
128 |
# Chatbot display area for the conversation thread
|
129 |
chatbot = gr.Chatbot(
|
130 |
label="Chat",
|
131 |
-
height=300, #
|
132 |
type="messages", # Explicitly set to 'messages' format
|
133 |
avatar_images=(None, None) # No avatars for simplicity
|
134 |
)
|
|
|
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 |
+
# Function to convert a PIL Image to a base64 string
|
16 |
+
def image_to_base64(img):
|
17 |
+
img_byte_arr = io.BytesIO()
|
18 |
+
img.save(img_byte_arr, format="PNG")
|
19 |
+
img_bytes = img_byte_arr.getvalue()
|
20 |
+
base64_string = base64.b64encode(img_bytes).decode("utf-8")
|
21 |
+
return f"data:image/png;base64,{base64_string}"
|
22 |
+
|
23 |
def generate_image(prompt, image=None, output_filename="generated_image"):
|
24 |
# Initialize client with the API key
|
25 |
client = genai.Client(
|
|
|
96 |
def chat_handler(user_input, user_image, chat_history):
|
97 |
# Add user message to chat history
|
98 |
if user_image:
|
99 |
+
# Convert the uploaded image to base64
|
100 |
+
img_base64 = image_to_base64(user_image)
|
101 |
+
# Add the image to the chat history
|
102 |
+
chat_history.append({"role": "user", "content": img_base64})
|
|
|
103 |
|
104 |
# Add the text prompt to the chat history
|
105 |
if user_input:
|
|
|
115 |
|
116 |
# Add AI response to chat history
|
117 |
if img:
|
118 |
+
# Convert the generated image to base64
|
119 |
+
img_base64 = image_to_base64(img)
|
120 |
+
# Add the image to the chat history
|
121 |
+
chat_history.append({"role": "assistant", "content": img_base64})
|
|
|
122 |
|
123 |
# Add the status message
|
124 |
chat_history.append({"role": "assistant", "content": status})
|
|
|
133 |
# Chatbot display area for the conversation thread
|
134 |
chatbot = gr.Chatbot(
|
135 |
label="Chat",
|
136 |
+
height=300, # Already reduced to 300
|
137 |
type="messages", # Explicitly set to 'messages' format
|
138 |
avatar_images=(None, None) # No avatars for simplicity
|
139 |
)
|