import gradio as gr import torch from diffusers import StableDiffusionImg2ImgPipeline from PIL import Image import numpy as np # تهيئة النموذج device = "cuda" if torch.cuda.is_available() else "cpu" model_id = "runwayml/stable-diffusion-v1-5" pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32) pipe = pipe.to(device) # قائمة الأنماط الفنية المتاحة ARTISTIC_STYLES = { "فان غوخ": "in the style of Vincent van Gogh, oil painting, impressionist, bold brushstrokes, vibrant colors", "بيكاسو": "in the style of Pablo Picasso, cubism, geometric shapes, abstract art", "دافنشي": "in the style of Leonardo da Vinci, Renaissance art, detailed, realistic, sfumato technique", "موناليزا": "in the style of Mona Lisa, Renaissance portrait, subtle colors, mysterious smile", "رسوم متحركة": "cartoon style, vibrant colors, simple shapes, cute characters", "أنمي": "anime style, manga art, detailed eyes, vibrant colors", "لوحة زيتية": "oil painting style, textured canvas, rich colors, detailed brushstrokes", "رسم بالألوان المائية": "watercolor painting, soft colors, flowing texture, artistic", "فن البوب": "pop art style, bold colors, comic book style, Andy Warhol inspired", "رسم رقمي": "digital art, clean lines, modern style, professional illustration" } def process_image(image, style, strength): """ تحويل الصورة إلى النمط الفني المختار """ # تحويل الصورة إلى الحجم المناسب width, height = image.size max_size = 768 if width > max_size or height > max_size: ratio = max_size / max(width, height) new_width = int(width * ratio) new_height = int(height * ratio) image = image.resize((new_width, new_height), Image.Resampling.LANCZOS) # إنشاء وصف النمط prompt = f"transform this image {ARTISTIC_STYLES[style]}, masterpiece, highly detailed" # معالجة الصورة output = pipe( prompt=prompt, image=image, strength=strength, guidance_scale=7.5, num_inference_steps=50 ).images[0] return output def style_transfer(input_image, style_name, effect_strength=0.75): """ واجهة تحويل النمط الفني """ if input_image is None: return None # تحويل الصورة إلى كائن PIL if isinstance(input_image, np.ndarray): input_image = Image.fromarray(input_image) try: output_image = process_image(input_image, style_name, effect_strength) return output_image except Exception as e: return f"حدث خطأ: {str(e)}" # إنشاء واجهة المستخدم with gr.Blocks(title="محول الأنماط الفني 🎨", theme=gr.themes.Soft()) as iface: gr.Markdown(""" # 🎨 محول الأنماط الفني قم بتحويل صورك إلى أعمال فنية مذهلة باستخدام تقنيات الذكاء الاصطناعي! ### 📝 التعليمات: 1. قم بتحميل صورة 2. اختر النمط الفني المطلوب 3. اضبط قوة التأثير 4. انقر على "تحويل" وانتظر النتيجة """) with gr.Row(): with gr.Column(): input_image = gr.Image( label="الصورة الأصلية", type="pil", height=400 ) style_dropdown = gr.Dropdown( choices=list(ARTISTIC_STYLES.keys()), value=list(ARTISTIC_STYLES.keys())[0], label="اختر النمط الفني" ) strength_slider = gr.Slider( minimum=0.1, maximum=0.9, value=0.75, step=0.05, label="قوة التأثير" ) convert_btn = gr.Button("تحويل 🎨", variant="primary") with gr.Column(): output_image = gr.Image( label="النتيجة", height=400 ) with gr.Row(): gr.Markdown(""" ### 💡 نصائح: - استخدم صوراً واضحة وذات جودة عالية - جرب أنماطاً مختلفة لنفس الصورة - اضبط قوة التأثير للحصول على النتيجة المطلوبة ### ℹ️ معلومات: - يستخدم هذا التطبيق تقنية Stable Diffusion - جميع المعالجة تتم محلياً على جهازك - الصور المدخلة لا يتم تخزينها أو مشاركتها """) # ربط الأحداث convert_btn.click( style_transfer, inputs=[input_image, style_dropdown, strength_slider], outputs=output_image ) # تشغيل التطبيق iface.launch()