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)