# 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"
{LOGO_SVG}

Takween Project

") gr.Markdown("#### A specialized model for generating precise geometric images from text descriptions.") gr.HTML("
") with gr.Row(): with gr.Column(scale=1): prompt = gr.Textbox(label="Prompt", placeholder="A red circle with thick black borders...", lines=3) negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Low quality, blurry, distorted...") with gr.Accordion("Advanced Settings", open=False): guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, value=7.5, step=0.1) num_inference_steps = gr.Slider(label="Number of Steps", minimum=10, maximum=100, value=30, step=1) with gr.Row(): seed = gr.Number(label="Seed", value=0, precision=0) randomize_seed = gr.Checkbox(label="Randomize", value=True) run_button = gr.Button("Generate Image", variant="primary") gr.Examples(examples=["A filled red circle with a thick black border", "An outline blue triangle positioned to the left of a yellow square", "A green star overlapping a purple rectangle"], inputs=[prompt]) with gr.Column(scale=2): output_image = gr.Image(label="Generated Image", interactive=False, height=512) output_seed = gr.Textbox(label="Seed Used", interactive=False) gr.HTML("
") with gr.Accordion("Team and Acknowledgments", open=False): gr.Markdown("""

Development Team:


Special Thanks:

We extend our sincere gratitude for the guidance and support of:

""") gr.Markdown("") run_button.click( fn=infer, inputs=[prompt, negative_prompt, guidance_scale, num_inference_steps, seed, randomize_seed], outputs=[output_image, output_seed, run_button], ) # --- 6. Launch the App --- if __name__ == "__main__": demo.launch()