Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ timeout = 100
|
|
| 17 |
|
| 18 |
def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7):
|
| 19 |
if prompt == "" or prompt == None:
|
| 20 |
-
return None
|
| 21 |
|
| 22 |
key = random.randint(0, 999)
|
| 23 |
|
|
@@ -51,10 +51,14 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
| 51 |
image_bytes = response.content
|
| 52 |
image = Image.open(io.BytesIO(image_bytes))
|
| 53 |
print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Error when trying to open the image: {e}")
|
| 57 |
-
return None
|
| 58 |
|
| 59 |
css = """
|
| 60 |
#app-container {
|
|
@@ -84,7 +88,24 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
| 84 |
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
| 85 |
with gr.Row():
|
| 86 |
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
app.launch(show_api=False, share=True)
|
|
|
|
| 17 |
|
| 18 |
def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7):
|
| 19 |
if prompt == "" or prompt == None:
|
| 20 |
+
return None, None # Return None for both image and file
|
| 21 |
|
| 22 |
key = random.randint(0, 999)
|
| 23 |
|
|
|
|
| 51 |
image_bytes = response.content
|
| 52 |
image = Image.open(io.BytesIO(image_bytes))
|
| 53 |
print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
|
| 54 |
+
|
| 55 |
+
# Save the image to a temporary file for download
|
| 56 |
+
temp_file = f"temp_generated_image_{key}.png"
|
| 57 |
+
image.save(temp_file, format="PNG")
|
| 58 |
+
return image, temp_file # Return both the image and the file path
|
| 59 |
except Exception as e:
|
| 60 |
print(f"Error when trying to open the image: {e}")
|
| 61 |
+
return None, None
|
| 62 |
|
| 63 |
css = """
|
| 64 |
#app-container {
|
|
|
|
| 88 |
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
| 89 |
with gr.Row():
|
| 90 |
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
| 91 |
+
with gr.Row():
|
| 92 |
+
download_button = gr.File(label="Download Image", visible=False, elem_id="download-button")
|
| 93 |
|
| 94 |
+
# Update the download button visibility and file when the image is generated
|
| 95 |
+
def update_download_button(image, file):
|
| 96 |
+
if image is not None:
|
| 97 |
+
return gr.File.update(value=file, visible=True)
|
| 98 |
+
return gr.File.update(visible=False)
|
| 99 |
+
|
| 100 |
+
text_button.click(
|
| 101 |
+
query,
|
| 102 |
+
inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength],
|
| 103 |
+
outputs=[image_output, download_button]
|
| 104 |
+
)
|
| 105 |
+
image_output.change(
|
| 106 |
+
update_download_button,
|
| 107 |
+
inputs=[image_output, download_button],
|
| 108 |
+
outputs=[download_button]
|
| 109 |
+
)
|
| 110 |
|
| 111 |
app.launch(show_api=False, share=True)
|