# app.py (النسخة النهائية - مع تصحيح SyntaxError الثاني) import gradio as gr import numpy as np import random import torch from diffusers import DiffusionPipeline import time # --- 1. Settings and Constants --- DEVICE = "cuda" if torch.cuda.is_available() else "cpu" DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32 MODEL_ID = "YourUsername/Takween-v1" BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5" MAX_SEED = np.iinfo(np.int32).max LOGO_SVG = """ """ # --- 2. Model Loading --- try: pipe = DiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=DTYPE, safety_checker=None) print(f"✅ Trained model '{MODEL_ID}' loaded successfully.") except Exception: print(f"❌ Could not load trained model '{MODEL_ID}'. Loading base model.") pipe = DiffusionPipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=DTYPE, safety_checker=None) pipe = pipe.to(DEVICE) # --- 3. Professional Theme (Golden Version) --- theme = gr.themes.Base( primary_hue=gr.themes.colors.amber, secondary_hue=gr.themes.colors.neutral, font=[gr.themes.GoogleFont("IBM Plex Sans"), "system-ui", "sans-serif"], ).set( body_background_fill="*neutral_50", block_background_fill="white", block_border_width="1px", block_shadow="*shadow_drop_lg", button_primary_background_fill="*primary_500", button_primary_background_fill_hover="*primary_600", ) # --- 4. Inference Function with UI Updates (Corrected) --- def infer(prompt, negative_prompt, guidance_scale, num_inference_steps, seed, randomize_seed): if randomize_seed: seed = random.randint(0, MAX_SEED) # ======================================================= # <<< تم تعديل هذا الجزء لحل مشكلة SyntaxError >>> # الخطوة 1: إنشاء المولد على الجهاز الصحيح generator = torch.Generator(device=DEVICE) # الخطوة 2: تحديد البذرة للمولد generator.manual_seed(seed) # ======================================================= yield { output_image: gr.update(value=None, interactive=False, visible=True), run_button: gr.update(interactive=False, value="Generating..."), } image = pipe( prompt=prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, num_inference_steps=int(num_inference_steps), generator=generator, ).images[0] yield { output_image: gr.update(value=image, interactive=True), output_seed: gr.update(value=seed), run_button: gr.update(interactive=True, value="Generate Again"), } # --- 5. Professional UI Layout --- with gr.Blocks(theme=theme, css="#footer {text-align: center;}") as demo: with gr.Row(): gr.HTML(f"
We extend our sincere gratitude for the guidance and support of: