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