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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import requests
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
def convert_to_anime(image, style_intensity, brightness, contrast):
|
| 9 |
+
"""
|
| 10 |
+
Convert a regular photo to anime style using Qwen-Image-Edit-2509
|
| 11 |
+
This is a placeholder function that simulates the anime conversion process.
|
| 12 |
+
In a real implementation, this would connect to the actual Qwen model.
|
| 13 |
+
"""
|
| 14 |
+
if image is None:
|
| 15 |
+
return None
|
| 16 |
+
|
| 17 |
+
# Convert to PIL Image if needed
|
| 18 |
+
if isinstance(image, str):
|
| 19 |
+
img = Image.open(image)
|
| 20 |
+
elif isinstance(image, np.ndarray):
|
| 21 |
+
img = Image.fromarray(image)
|
| 22 |
+
else:
|
| 23 |
+
img = image
|
| 24 |
+
|
| 25 |
+
# Apply simulated anime-style effects
|
| 26 |
+
img_array = np.array(img)
|
| 27 |
+
|
| 28 |
+
# Increase saturation and vibrance (simulating anime colors)
|
| 29 |
+
hsv = img_array.astype(np.float32)
|
| 30 |
+
hsv[..., 1] *= style_intensity
|
| 31 |
+
hsv[..., 2] = np.clip(hsv[..., 2] * brightness, 0, 255)
|
| 32 |
+
|
| 33 |
+
# Apply edge enhancement (simulating anime line art)
|
| 34 |
+
from scipy import ndimage
|
| 35 |
+
|
| 36 |
+
# Create a simple anime-like effect
|
| 37 |
+
edges = ndimage.sobel(img_array.mean(axis=2))
|
| 38 |
+
anime_effect = np.clip(edges * 50, 0, 255)
|
| 39 |
+
|
| 40 |
+
# Apply color quantization (simulating flat anime colors)
|
| 41 |
+
quantized = (img_array // 32) * 32
|
| 42 |
+
|
| 43 |
+
# Blend with original
|
| 44 |
+
alpha = style_intensity
|
| 45 |
+
result = (img_array * (1 - alpha) + (quantized * alpha)
|
| 46 |
+
|
| 47 |
+
# Apply contrast
|
| 48 |
+
result = ((result - 127.5) * contrast) + 127.5
|
| 49 |
+
result = np.clip(result, 0, 255).astype(np.uint8)
|
| 50 |
+
|
| 51 |
+
return Image.fromarray(result)
|
| 52 |
+
|
| 53 |
+
def process_image_with_api(image):
|
| 54 |
+
"""
|
| 55 |
+
Placeholder function for actual API integration
|
| 56 |
+
"""
|
| 57 |
+
# Simulate API call to Qwen model
|
| 58 |
+
# In real implementation, this would call the actual model
|
| 59 |
+
return result
|
| 60 |
+
|
| 61 |
+
def validate_image(image):
|
| 62 |
+
"""
|
| 63 |
+
Validate the uploaded image
|
| 64 |
+
"""
|
| 65 |
+
if image is None:
|
| 66 |
+
return False, "Please upload an image first"
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
if isinstance(image, str):
|
| 70 |
+
img = Image.open(image)
|
| 71 |
+
elif isinstance(image, np.ndarray):
|
| 72 |
+
img = Image.fromarray(image)
|
| 73 |
+
else:
|
| 74 |
+
img = image
|
| 75 |
+
|
| 76 |
+
# Check image size
|
| 77 |
+
if img.size[0] < 100 or img.size[1] < 100:
|
| 78 |
+
return False, "Image is too small. Please upload a larger image."
|
| 79 |
+
|
| 80 |
+
return True, "Image validated successfully"
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return False, f"Error loading image: {str(e)}"
|
| 83 |
+
|
| 84 |
+
return True, "Image validated successfully"
|
| 85 |
+
|
| 86 |
+
def download_example_images():
|
| 87 |
+
"""
|
| 88 |
+
Provide example images for users to try
|
| 89 |
+
"""
|
| 90 |
+
examples = [
|
| 91 |
+
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/cheetah.jpg",
|
| 92 |
+
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/dog.jpg",
|
| 93 |
+
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/cat.jpg"
|
| 94 |
+
]
|
| 95 |
+
return examples
|
| 96 |
+
|
| 97 |
+
# Create the Gradio interface
|
| 98 |
+
with gr.Blocks(
|
| 99 |
+
title="Qwen-Image-Edit-2509 Photo to Anime Converter",
|
| 100 |
+
theme=gr.themes.Soft(),
|
| 101 |
+
footer_links=[
|
| 102 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 103 |
+
) as demo:
|
| 104 |
+
|
| 105 |
+
gr.Markdown("# 🎨 Qwen-Image-Edit-2509 Photo to Anime Converter")
|
| 106 |
+
gr.Markdown("Upload your photo and transform it into beautiful anime art! ✨")
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
with gr.Column(scale=1):
|
| 110 |
+
gr.Markdown("## 📤 Upload Your Photo")
|
| 111 |
+
|
| 112 |
+
with gr.Group():
|
| 113 |
+
input_image = gr.Image(
|
| 114 |
+
label="Upload Photo",
|
| 115 |
+
sources=["upload", "webcam"],
|
| 116 |
+
type="pil",
|
| 117 |
+
height=300
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
gr.Markdown("### 🎛 Adjustment Controls")
|
| 121 |
+
|
| 122 |
+
style_intensity = gr.Slider(
|
| 123 |
+
minimum=1.0,
|
| 124 |
+
maximum=3.0,
|
| 125 |
+
value=2.0,
|
| 126 |
+
step=0.1,
|
| 127 |
+
label="Anime Style Intensity"
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
brightness = gr.Slider(
|
| 131 |
+
minimum=0.5,
|
| 132 |
+
maximum=2.0,
|
| 133 |
+
value=1.0,
|
| 134 |
+
label="Brightness"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
contrast = gr.Slider(
|
| 138 |
+
minimum=0.5,
|
| 139 |
+
maximum=2.0,
|
| 140 |
+
value=1.0,
|
| 141 |
+
step=0.1,
|
| 142 |
+
interactive=True
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
with gr.Row():
|
| 146 |
+
process_btn = gr.Button("✨ Transform to Anime", variant="primary")
|
| 147 |
+
clear_btn = gr.ClearButton(components=[input_image])
|
| 148 |
+
|
| 149 |
+
with gr.Column(scale=1):
|
| 150 |
+
gr.Markdown("## 🖼 Anime Result")
|
| 151 |
+
|
| 152 |
+
output_image = gr.Image(
|
| 153 |
+
label="Anime Style Result",
|
| 154 |
+
height=300,
|
| 155 |
+
interactive=False
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Process button click
|
| 159 |
+
process_btn.click(
|
| 160 |
+
fn=convert_to_anime,
|
| 161 |
+
inputs=[input_image, style_intensity, brightness, contrast],
|
| 162 |
+
outputs=[output_image],
|
| 163 |
+
api_visibility="public"
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# Examples section
|
| 167 |
+
gr.Markdown("## 🎪 Try with Examples")
|
| 168 |
+
|
| 169 |
+
example_images = download_example_images()
|
| 170 |
+
|
| 171 |
+
gr.Examples(
|
| 172 |
+
examples=example_images,
|
| 173 |
+
inputs=[input_image],
|
| 174 |
+
outputs=[output_image],
|
| 175 |
+
fn=process_image_with_api,
|
| 176 |
+
cache_examples=True
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# Instructions
|
| 180 |
+
with gr.Accordion("ℹ️ Instructions", open=False):
|
| 181 |
+
gr.Markdown("""
|
| 182 |
+
1. **Upload a photo** using the upload button or webcam
|
| 183 |
+
2. **Adjust the style parameters** to your preference
|
| 184 |
+
3. **Click 'Transform to Anime'** to generate your anime-style image
|
| 185 |
+
4. **Download your result** when you're happy with it!
|
| 186 |
+
|
| 187 |
+
### 🎯 Tips for Best Results:
|
| 188 |
+
- Use well-lit photos with clear subjects
|
| 189 |
+
- Adjust style intensity for more dramatic effects
|
| 190 |
+
- Fine-tune brightness and contrast for optimal results
|
| 191 |
+
""")
|
| 192 |
+
|
| 193 |
+
# Error handling demonstration
|
| 194 |
+
def handle_error(image):
|
| 195 |
+
try:
|
| 196 |
+
is_valid, message = validate_image(image)
|
| 197 |
+
if not is_valid:
|
| 198 |
+
raise gr.Error(message)
|
| 199 |
+
return image
|
| 200 |
+
except gr.Error as e:
|
| 201 |
+
raise e
|
| 202 |
+
except Exception as e:
|
| 203 |
+
raise gr.Error(f"Unexpected error: {str(e)}")
|
| 204 |
+
|
| 205 |
+
input_image.upload(
|
| 206 |
+
fn=handle_error,
|
| 207 |
+
inputs=[input_image],
|
| 208 |
+
outputs=[input_image],
|
| 209 |
+
api_visibility="private"
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
if __name__ == "__main__":
|
| 213 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
requests
|
| 3 |
+
gradio
|
| 4 |
+
scipy
|
| 5 |
+
Pillow
|
| 6 |
+
matplotlib
|
| 7 |
+
pandas
|
| 8 |
+
scikit-learn
|
| 9 |
+
opencv-python
|
| 10 |
+
openpyxl
|
| 11 |
+
tqdm
|