Spaces:
Running
Running
Update app.py from anycoder
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
def apply_modification(image, modification_type):
|
| 8 |
+
"""
|
| 9 |
+
Apply a simple modification to demonstrate the concept
|
| 10 |
+
This is a placeholder - you would replace with your actual processing
|
| 11 |
+
"""
|
| 12 |
+
img = np.array(image)
|
| 13 |
+
|
| 14 |
+
if modification_type == "small":
|
| 15 |
+
# Placeholder for small modification
|
| 16 |
+
img = cv2.circle(img, (img.shape[1]//2, img.shape[0]//2), 30, (255, 0, 0), -1)
|
| 17 |
+
elif modification_type == "large":
|
| 18 |
+
# Placeholder for large modification
|
| 19 |
+
img = cv2.circle(img, (img.shape[1]//2, img.shape[0]//2), 60, (0, 0, 255), -1)
|
| 20 |
+
|
| 21 |
+
return Image.fromarray(img)
|
| 22 |
+
|
| 23 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 24 |
+
gr.Markdown("""
|
| 25 |
+
# Personal Image Modifier
|
| 26 |
+
*Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)*
|
| 27 |
+
""")
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
with gr.Column():
|
| 31 |
+
input_image = gr.Image(label="Upload Your Image", type="pil")
|
| 32 |
+
modification = gr.Radio(
|
| 33 |
+
choices=["Small", "Large"],
|
| 34 |
+
label="Modification Type",
|
| 35 |
+
value="Small"
|
| 36 |
+
)
|
| 37 |
+
submit_btn = gr.Button("Apply Modification", variant="primary")
|
| 38 |
+
|
| 39 |
+
with gr.Column():
|
| 40 |
+
output_image = gr.Image(label="Modified Image")
|
| 41 |
+
download_btn = gr.DownloadButton("Download Result")
|
| 42 |
+
|
| 43 |
+
# Example images
|
| 44 |
+
gr.Examples(
|
| 45 |
+
examples=[["examples/male1.jpg", "Small"], ["examples/male2.jpg", "Large"]],
|
| 46 |
+
inputs=[input_image, modification],
|
| 47 |
+
outputs=output_image,
|
| 48 |
+
fn=apply_modification,
|
| 49 |
+
cache_examples=True
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
def process_and_download(image, mod_type):
|
| 53 |
+
modified = apply_modification(image, mod_type.lower())
|
| 54 |
+
temp_path = f"{tempfile.mkdtemp()}/modified.png"
|
| 55 |
+
modified.save(temp_path)
|
| 56 |
+
return modified, temp_path
|
| 57 |
+
|
| 58 |
+
submit_btn.click(
|
| 59 |
+
fn=process_and_download,
|
| 60 |
+
inputs=[input_image, modification],
|
| 61 |
+
outputs=[output_image, download_btn]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
demo.launch(
|
| 65 |
+
title="Personal Image Modifier",
|
| 66 |
+
footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}]
|
| 67 |
+
)
|