Spaces:
Running
Running
Update app.py
Browse filesadd toggle for tags
app.py
CHANGED
|
@@ -174,7 +174,7 @@ def generate(text, file_name, model="gemini-2.0-flash-exp"):
|
|
| 174 |
print(f"Error deleting uploaded file {file.name}: {cleanup_e}")
|
| 175 |
|
| 176 |
# Main processing function for Gradio
|
| 177 |
-
def process_image_and_prompt(composite_pil, prompt):
|
| 178 |
composite_path = None # Initialize input temp file path for finally block
|
| 179 |
output_image_path = None # Initialize output temp file path for finally block
|
| 180 |
try:
|
|
@@ -191,15 +191,20 @@ def process_image_and_prompt(composite_pil, prompt):
|
|
| 191 |
model = "gemini-2.0-flash-exp" # Specify the model here
|
| 192 |
|
| 193 |
# 2. Call get_image_tags to get tags from the original image
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
| 196 |
|
| 197 |
# 3. Call generate for the main image processing based on the user prompt
|
| 198 |
output_image_path, main_text_response = generate(text=prompt, file_name=file_name, model=model)
|
| 199 |
|
| 200 |
# 4. Combine the tag JSON string and the main text response
|
| 201 |
# Format the output clearly
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
# 5. Prepare the image output for the Gradio gallery
|
| 205 |
result_img = None
|
|
@@ -298,6 +303,7 @@ with gr.Blocks( # css_paths="style.css", # Тимчасово закоменту
|
|
| 298 |
label="Prompt for Gemini",
|
| 299 |
elem_classes="prompt-input"
|
| 300 |
)
|
|
|
|
| 301 |
submit_btn = gr.Button("Generate", elem_classes="generate-btn")
|
| 302 |
|
| 303 |
with gr.Column(elem_classes="output-column"):
|
|
@@ -312,7 +318,7 @@ with gr.Blocks( # css_paths="style.css", # Тимчасово закоменту
|
|
| 312 |
# Connect the button click to the updated processing function
|
| 313 |
submit_btn.click(
|
| 314 |
fn=process_image_and_prompt,
|
| 315 |
-
inputs=[image_input, prompt_input],
|
| 316 |
outputs=[output_gallery, output_text],
|
| 317 |
)
|
| 318 |
|
|
@@ -335,4 +341,4 @@ with gr.Blocks( # css_paths="style.css", # Тимчасово закоменту
|
|
| 335 |
elem_id="examples-grid"
|
| 336 |
)
|
| 337 |
|
| 338 |
-
demo.queue(max_size=50).launch()
|
|
|
|
| 174 |
print(f"Error deleting uploaded file {file.name}: {cleanup_e}")
|
| 175 |
|
| 176 |
# Main processing function for Gradio
|
| 177 |
+
def process_image_and_prompt(composite_pil, prompt, enable_tagging=True):
|
| 178 |
composite_path = None # Initialize input temp file path for finally block
|
| 179 |
output_image_path = None # Initialize output temp file path for finally block
|
| 180 |
try:
|
|
|
|
| 191 |
model = "gemini-2.0-flash-exp" # Specify the model here
|
| 192 |
|
| 193 |
# 2. Call get_image_tags to get tags from the original image
|
| 194 |
+
tag_json_string = ""
|
| 195 |
+
if enable_tagging:
|
| 196 |
+
tagging_prompt = "Analyze this image. Provide a JSON object containing a single key, 'tags', whose value is a JSON array of strings, representing relevant keywords or tags for the image content. Example: {\"tags\": [\"apple\", \"fruit\", \"red\"]}. Provide ONLY the JSON object and nothing else."
|
| 197 |
+
tag_json_string = get_image_tags(file_name, tagging_prompt, model=model)
|
| 198 |
|
| 199 |
# 3. Call generate for the main image processing based on the user prompt
|
| 200 |
output_image_path, main_text_response = generate(text=prompt, file_name=file_name, model=model)
|
| 201 |
|
| 202 |
# 4. Combine the tag JSON string and the main text response
|
| 203 |
# Format the output clearly
|
| 204 |
+
if tag_json_string:
|
| 205 |
+
final_text_output = f"{tag_json_string},{main_text_response}"
|
| 206 |
+
else:
|
| 207 |
+
final_text_output = main_text_response
|
| 208 |
|
| 209 |
# 5. Prepare the image output for the Gradio gallery
|
| 210 |
result_img = None
|
|
|
|
| 303 |
label="Prompt for Gemini",
|
| 304 |
elem_classes="prompt-input"
|
| 305 |
)
|
| 306 |
+
with_tags = gr.Checkbox(label="Enable Tagging", value=True)
|
| 307 |
submit_btn = gr.Button("Generate", elem_classes="generate-btn")
|
| 308 |
|
| 309 |
with gr.Column(elem_classes="output-column"):
|
|
|
|
| 318 |
# Connect the button click to the updated processing function
|
| 319 |
submit_btn.click(
|
| 320 |
fn=process_image_and_prompt,
|
| 321 |
+
inputs=[image_input, prompt_input, with_tags],
|
| 322 |
outputs=[output_gallery, output_text],
|
| 323 |
)
|
| 324 |
|
|
|
|
| 341 |
elem_id="examples-grid"
|
| 342 |
)
|
| 343 |
|
| 344 |
+
demo.queue(max_size=50).launch()
|