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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -26,6 +26,11 @@ def save_image_to_static(img, prefix="image"):
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
31
  client = genai.Client(
@@ -104,8 +109,10 @@ def chat_handler(user_input, user_image, 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:
@@ -123,8 +130,10 @@ def chat_handler(user_input, user_image, 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})
 
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(
 
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:
 
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})