Deadmon commited on
Commit
c1f3cac
·
verified ·
1 Parent(s): 31bf755

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -2
app.py CHANGED
@@ -6,6 +6,7 @@ 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")
@@ -62,7 +63,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 for Gradio display
66
  img = Image.open(io.BytesIO(inline_data.data))
67
  return img, f"Image saved as {filename}"
68
  else:
@@ -80,7 +81,13 @@ def chat_handler(user_input, chat_history):
80
 
81
  # Add AI response to chat history
82
  if img:
83
- chat_history.append({"role": "assistant", "content": img})
 
 
 
 
 
 
84
  chat_history.append({"role": "assistant", "content": status})
85
 
86
  return chat_history, ""
 
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")
 
63
  filename = f"{output_filename}{file_extension}"
64
  save_binary_file(filename, inline_data.data)
65
 
66
+ # Convert binary data to PIL Image
67
  img = Image.open(io.BytesIO(inline_data.data))
68
  return img, f"Image saved as {filename}"
69
  else:
 
81
 
82
  # Add AI response to chat history
83
  if img:
84
+ # Save the PIL Image to a temporary file so Gradio can display it
85
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
86
+ img.save(tmp_file.name)
87
+ # Add the image as a file path that Gradio can serve
88
+ chat_history.append({"role": "assistant", "content": tmp_file.name})
89
+
90
+ # Add the status message
91
  chat_history.append({"role": "assistant", "content": status})
92
 
93
  return chat_history, ""