Deadmon commited on
Commit
e93189f
·
verified ·
1 Parent(s): 252260c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -6,19 +6,25 @@ 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
- # 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
@@ -96,10 +102,10 @@ def generate_image(prompt, image=None, output_filename="generated_image"):
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,10 +121,10 @@ def chat_handler(user_input, user_image, chat_history):
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,7 +139,7 @@ with gr.Blocks(title="Image Editing Chatbot") as demo:
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
  )
 
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 generate_image(prompt, image=None, output_filename="generated_image"):
30
  # Initialize client with the API key
 
102
  def chat_handler(user_input, user_image, chat_history):
103
  # Add user message to chat history
104
  if user_image:
105
+ # Save the uploaded image to the static directory
106
+ img_path = save_image_to_static(user_image, prefix="uploaded")
107
  # Add the image to the chat history
108
+ chat_history.append({"role": "user", "content": img_path})
109
 
110
  # Add the text prompt to the chat history
111
  if user_input:
 
121
 
122
  # Add AI response to chat history
123
  if img:
124
+ # Save the generated image to the static directory
125
+ img_path = save_image_to_static(img, prefix="generated")
126
  # Add the image to the chat history
127
+ chat_history.append({"role": "assistant", "content": img_path})
128
 
129
  # Add the status message
130
  chat_history.append({"role": "assistant", "content": status})
 
139
  # Chatbot display area for the conversation thread
140
  chatbot = gr.Chatbot(
141
  label="Chat",
142
+ height=300,
143
  type="messages", # Explicitly set to 'messages' format
144
  avatar_images=(None, None) # No avatars for simplicity
145
  )