issue fixed
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
|
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
import requests
|
|
|
6 |
from typing import Optional, Tuple
|
7 |
|
8 |
def load_environment():
|
@@ -145,7 +146,7 @@ def query_hf_api(
|
|
145 |
|
146 |
raise RuntimeError("Unexpected error in image generation")
|
147 |
|
148 |
-
def generate_image(prompt: str, output_format: str = 'png') -> Tuple[Optional[Image.Image], str, Optional[
|
149 |
"""
|
150 |
Generate an image from a text prompt.
|
151 |
|
@@ -154,8 +155,8 @@ def generate_image(prompt: str, output_format: str = 'png') -> Tuple[Optional[Im
|
|
154 |
output_format (str): Desired output format
|
155 |
|
156 |
Returns:
|
157 |
-
Tuple[Optional[Image.Image], str, Optional[
|
158 |
-
Generated PIL Image, status message, and downloadable image
|
159 |
"""
|
160 |
try:
|
161 |
# Validate prompt
|
@@ -169,13 +170,17 @@ def generate_image(prompt: str, output_format: str = 'png') -> Tuple[Optional[Im
|
|
169 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
170 |
|
171 |
# Convert image to specified format
|
172 |
-
|
173 |
|
174 |
-
# Create a temporary file
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
177 |
|
178 |
-
return image, "Image generated successfully!",
|
179 |
|
180 |
except Exception as e:
|
181 |
print(f"Image generation error: {e}")
|
@@ -227,11 +232,11 @@ def create_gradio_interface():
|
|
227 |
# Status Output
|
228 |
status_output = gr.Textbox(label="Status")
|
229 |
|
230 |
-
# Download Button
|
231 |
download_button = gr.File(
|
232 |
label="Download Image",
|
233 |
file_count="single",
|
234 |
-
type="
|
235 |
)
|
236 |
|
237 |
# Event Handlers
|
@@ -252,7 +257,7 @@ def main():
|
|
252 |
demo.launch(
|
253 |
server_name="0.0.0.0", # Listen on all network interfaces
|
254 |
server_port=7860, # Default Gradio port
|
255 |
-
share=
|
256 |
)
|
257 |
except Exception as e:
|
258 |
print(f"Error launching Gradio app: {e}")
|
|
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
import requests
|
6 |
+
import tempfile
|
7 |
from typing import Optional, Tuple
|
8 |
|
9 |
def load_environment():
|
|
|
146 |
|
147 |
raise RuntimeError("Unexpected error in image generation")
|
148 |
|
149 |
+
def generate_image(prompt: str, output_format: str = 'png') -> Tuple[Optional[Image.Image], str, Optional[str]]:
|
150 |
"""
|
151 |
Generate an image from a text prompt.
|
152 |
|
|
|
155 |
output_format (str): Desired output format
|
156 |
|
157 |
Returns:
|
158 |
+
Tuple[Optional[Image.Image], str, Optional[str]]:
|
159 |
+
Generated PIL Image, status message, and path to downloadable image
|
160 |
"""
|
161 |
try:
|
162 |
# Validate prompt
|
|
|
170 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
171 |
|
172 |
# Convert image to specified format
|
173 |
+
image_data = convert_image(image, output_format)
|
174 |
|
175 |
+
# Create a temporary file
|
176 |
+
with tempfile.NamedTemporaryFile(
|
177 |
+
delete=False,
|
178 |
+
suffix=f'.{output_format.lower()}'
|
179 |
+
) as temp_file:
|
180 |
+
temp_file.write(image_data)
|
181 |
+
temp_file_path = temp_file.name
|
182 |
|
183 |
+
return image, "Image generated successfully!", temp_file_path
|
184 |
|
185 |
except Exception as e:
|
186 |
print(f"Image generation error: {e}")
|
|
|
232 |
# Status Output
|
233 |
status_output = gr.Textbox(label="Status")
|
234 |
|
235 |
+
# Download Button
|
236 |
download_button = gr.File(
|
237 |
label="Download Image",
|
238 |
file_count="single",
|
239 |
+
type="filepath" # Use filepath type
|
240 |
)
|
241 |
|
242 |
# Event Handlers
|
|
|
257 |
demo.launch(
|
258 |
server_name="0.0.0.0", # Listen on all network interfaces
|
259 |
server_port=7860, # Default Gradio port
|
260 |
+
share=True # Set to True for a public link
|
261 |
)
|
262 |
except Exception as e:
|
263 |
print(f"Error launching Gradio app: {e}")
|