Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,11 +60,17 @@ def process_image_and_prompt(pil_image, prompt, api_key):
|
|
| 60 |
result_img = Image.open(image_path)
|
| 61 |
if result_img.mode == "RGBA":
|
| 62 |
result_img = result_img.convert("RGB")
|
| 63 |
-
return [result_img], ""
|
| 64 |
else:
|
| 65 |
-
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
css_style = """
|
| 70 |
.app-container {
|
|
@@ -75,13 +81,14 @@ css_style = """
|
|
| 75 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 76 |
}
|
| 77 |
.header-text {
|
| 78 |
-
font-size:
|
| 79 |
font-weight: 900;
|
| 80 |
background: linear-gradient(90deg, #FF6A00, #EE0979);
|
| 81 |
-webkit-background-clip: text;
|
| 82 |
-webkit-text-fill-color: transparent;
|
| 83 |
-
margin-bottom: 1.
|
| 84 |
text-align: center;
|
|
|
|
| 85 |
}
|
| 86 |
.gradient-button {
|
| 87 |
background: linear-gradient(90deg, #FF6A00, #EE0979);
|
|
@@ -96,38 +103,74 @@ css_style = """
|
|
| 96 |
.gradient-button:hover {
|
| 97 |
background: linear-gradient(90deg, #EE0979, #FF6A00);
|
| 98 |
}
|
| 99 |
-
main-content {
|
| 100 |
display: flex;
|
| 101 |
gap: 2rem;
|
| 102 |
margin-top: 1rem;
|
| 103 |
}
|
| 104 |
.column {
|
| 105 |
flex: 1;
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
.output-gallery {
|
| 108 |
-
|
| 109 |
-
max-height:
|
| 110 |
overflow: visible !important;
|
| 111 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
"""
|
| 113 |
|
| 114 |
with gr.Blocks(css=css_style) as demo:
|
| 115 |
with gr.Column(elem_classes="app-container"):
|
| 116 |
gr.Markdown("<div class='header-text'>Image Editor (Powered by Gemini)</div>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
with gr.Row(elem_classes="main-content"):
|
| 118 |
with gr.Column(elem_classes="column"):
|
| 119 |
image_input = gr.Image(type="pil", label="Upload PNG Image", image_mode="RGBA")
|
| 120 |
prompt_input = gr.Textbox(label="Edit Prompt", placeholder="Describe how to edit the image", lines=2)
|
| 121 |
-
api_key_input = gr.Textbox(label="Gemini API Key (
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
with gr.Column(elem_classes="column"):
|
| 124 |
-
gallery_output = gr.Gallery(label="Generated Image", elem_classes="output-gallery")
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
submit_btn.click(
|
| 128 |
-
fn=
|
| 129 |
inputs=[image_input, prompt_input, api_key_input],
|
| 130 |
-
outputs=[gallery_output,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
)
|
| 132 |
|
| 133 |
demo.queue(max_size=50).launch()
|
|
|
|
| 60 |
result_img = Image.open(image_path)
|
| 61 |
if result_img.mode == "RGBA":
|
| 62 |
result_img = result_img.convert("RGB")
|
| 63 |
+
return [result_img], "", None
|
| 64 |
else:
|
| 65 |
+
# Show text output in info box
|
| 66 |
+
return None, text_output, None
|
| 67 |
except Exception as e:
|
| 68 |
+
# Show error message in popup
|
| 69 |
+
return None, None, gr.Error(f"Generation failed: {str(e)}")
|
| 70 |
+
|
| 71 |
+
def reset_inputs(api_key_value):
|
| 72 |
+
# Reset all except API key
|
| 73 |
+
return None, "", None, api_key_value
|
| 74 |
|
| 75 |
css_style = """
|
| 76 |
.app-container {
|
|
|
|
| 81 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 82 |
}
|
| 83 |
.header-text {
|
| 84 |
+
font-size: 3rem;
|
| 85 |
font-weight: 900;
|
| 86 |
background: linear-gradient(90deg, #FF6A00, #EE0979);
|
| 87 |
-webkit-background-clip: text;
|
| 88 |
-webkit-text-fill-color: transparent;
|
| 89 |
+
margin-bottom: 1.5rem;
|
| 90 |
text-align: center;
|
| 91 |
+
text-shadow: 1px 1px 5px rgba(255,106,0,0.6);
|
| 92 |
}
|
| 93 |
.gradient-button {
|
| 94 |
background: linear-gradient(90deg, #FF6A00, #EE0979);
|
|
|
|
| 103 |
.gradient-button:hover {
|
| 104 |
background: linear-gradient(90deg, #EE0979, #FF6A00);
|
| 105 |
}
|
| 106 |
+
.main-content {
|
| 107 |
display: flex;
|
| 108 |
gap: 2rem;
|
| 109 |
margin-top: 1rem;
|
| 110 |
}
|
| 111 |
.column {
|
| 112 |
flex: 1;
|
| 113 |
+
display: flex;
|
| 114 |
+
flex-direction: column;
|
| 115 |
}
|
| 116 |
.output-gallery {
|
| 117 |
+
width: 100% !important;
|
| 118 |
+
max-height: none !important;
|
| 119 |
overflow: visible !important;
|
| 120 |
}
|
| 121 |
+
#text-info-box textarea {
|
| 122 |
+
height: 6.5rem !important;
|
| 123 |
+
resize: none !important;
|
| 124 |
+
}
|
| 125 |
"""
|
| 126 |
|
| 127 |
with gr.Blocks(css=css_style) as demo:
|
| 128 |
with gr.Column(elem_classes="app-container"):
|
| 129 |
gr.Markdown("<div class='header-text'>Image Editor (Powered by Gemini)</div>")
|
| 130 |
+
with gr.Accordion("App Usage Instructions", open=True):
|
| 131 |
+
gr.Markdown(
|
| 132 |
+
"""
|
| 133 |
+
- Upload a PNG image you wish to edit.
|
| 134 |
+
- Enter a detailed prompt describing the edits.
|
| 135 |
+
- Provide your Gemini API Key (required).
|
| 136 |
+
- Click Generate Edit to process the image.
|
| 137 |
+
"""
|
| 138 |
+
)
|
| 139 |
+
with gr.Accordion("API Key Instructions", open=False):
|
| 140 |
+
gr.Markdown(
|
| 141 |
+
"""
|
| 142 |
+
- The Gemini API Key is **required** to use this app.
|
| 143 |
+
- Obtain your key from your Google Cloud Console.
|
| 144 |
+
- Keep the key private and do not share it publicly.
|
| 145 |
+
"""
|
| 146 |
+
)
|
| 147 |
with gr.Row(elem_classes="main-content"):
|
| 148 |
with gr.Column(elem_classes="column"):
|
| 149 |
image_input = gr.Image(type="pil", label="Upload PNG Image", image_mode="RGBA")
|
| 150 |
prompt_input = gr.Textbox(label="Edit Prompt", placeholder="Describe how to edit the image", lines=2)
|
| 151 |
+
api_key_input = gr.Textbox(label="Gemini API Key (required)", placeholder="Enter your Gemini API key here", type="password")
|
| 152 |
+
with gr.Row():
|
| 153 |
+
submit_btn = gr.Button("Generate Edit", elem_classes="gradient-button")
|
| 154 |
+
reset_btn = gr.Button("Reset Inputs")
|
| 155 |
with gr.Column(elem_classes="column"):
|
| 156 |
+
gallery_output = gr.Gallery(label="Generated Image", elem_classes="output-gallery", show_label=True)
|
| 157 |
+
text_info = gr.Textbox(label="Information / Logs", interactive=False, lines=6, elem_id="text-info-box")
|
| 158 |
+
|
| 159 |
+
def on_submit(pil_img, prompt, key):
|
| 160 |
+
if not key or key.strip() == "":
|
| 161 |
+
raise gr.Error("Gemini API Key is required!")
|
| 162 |
+
return process_image_and_prompt(pil_img, prompt, key)
|
| 163 |
|
| 164 |
submit_btn.click(
|
| 165 |
+
fn=on_submit,
|
| 166 |
inputs=[image_input, prompt_input, api_key_input],
|
| 167 |
+
outputs=[gallery_output, text_info, None]
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
reset_btn.click(
|
| 171 |
+
fn=reset_inputs,
|
| 172 |
+
inputs=[api_key_input],
|
| 173 |
+
outputs=[image_input, prompt_input, text_info, api_key_input]
|
| 174 |
)
|
| 175 |
|
| 176 |
demo.queue(max_size=50).launch()
|