Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,139 +1,160 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 18 |
pipe = pipe.to(device)
|
| 19 |
|
|
|
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
-
MAX_IMAGE_SIZE = 1024
|
| 22 |
|
|
|
|
| 23 |
|
| 24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
def infer(
|
| 26 |
prompt,
|
| 27 |
negative_prompt,
|
| 28 |
seed,
|
| 29 |
randomize_seed,
|
| 30 |
-
width,
|
| 31 |
-
height,
|
| 32 |
guidance_scale,
|
| 33 |
num_inference_steps,
|
| 34 |
progress=gr.Progress(track_tqdm=True),
|
| 35 |
):
|
|
|
|
|
|
|
|
|
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
|
| 39 |
-
generator = torch.Generator().manual_seed(seed)
|
| 40 |
|
| 41 |
image = pipe(
|
| 42 |
prompt=prompt,
|
| 43 |
negative_prompt=negative_prompt,
|
| 44 |
guidance_scale=guidance_scale,
|
| 45 |
-
num_inference_steps=num_inference_steps,
|
| 46 |
-
width=width,
|
| 47 |
-
height=height,
|
| 48 |
generator=generator,
|
| 49 |
).images[0]
|
| 50 |
|
|
|
|
| 51 |
return image, seed
|
| 52 |
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
examples = [
|
| 55 |
-
"
|
| 56 |
-
"An
|
| 57 |
-
"A
|
| 58 |
]
|
| 59 |
|
|
|
|
| 60 |
css = """
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
-
max-width:
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
prompt = gr.Text(
|
| 73 |
-
label="Prompt",
|
| 74 |
show_label=False,
|
| 75 |
max_lines=1,
|
| 76 |
-
placeholder="
|
| 77 |
container=False,
|
| 78 |
)
|
|
|
|
| 79 |
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 85 |
negative_prompt = gr.Text(
|
| 86 |
-
label="Negative
|
| 87 |
max_lines=1,
|
| 88 |
-
placeholder="
|
| 89 |
-
visible=False,
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
seed = gr.Slider(
|
| 93 |
-
label="Seed",
|
| 94 |
-
minimum=0,
|
| 95 |
-
maximum=MAX_SEED,
|
| 96 |
-
step=1,
|
| 97 |
-
value=0,
|
| 98 |
)
|
| 99 |
-
|
| 100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 101 |
-
|
| 102 |
with gr.Row():
|
| 103 |
-
|
| 104 |
-
label="
|
| 105 |
-
minimum=
|
| 106 |
-
maximum=
|
| 107 |
-
step=
|
| 108 |
-
value=
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
height = gr.Slider(
|
| 112 |
-
label="Height",
|
| 113 |
-
minimum=256,
|
| 114 |
-
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=32,
|
| 116 |
-
value=1024, # Replace with defaults that work for your model
|
| 117 |
)
|
|
|
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
guidance_scale = gr.Slider(
|
| 121 |
-
label="Guidance
|
| 122 |
minimum=0.0,
|
| 123 |
-
maximum=
|
| 124 |
step=0.1,
|
| 125 |
-
value=
|
| 126 |
)
|
| 127 |
-
|
| 128 |
num_inference_steps = gr.Slider(
|
| 129 |
-
label="
|
| 130 |
minimum=1,
|
| 131 |
-
maximum=
|
| 132 |
step=1,
|
| 133 |
-
value=
|
| 134 |
)
|
| 135 |
-
|
|
|
|
| 136 |
gr.Examples(examples=examples, inputs=[prompt])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
gr.on(
|
| 138 |
triggers=[run_button.click, prompt.submit],
|
| 139 |
fn=infer,
|
|
@@ -142,13 +163,12 @@ with gr.Blocks(css=css) as demo:
|
|
| 142 |
negative_prompt,
|
| 143 |
seed,
|
| 144 |
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
guidance_scale,
|
| 148 |
num_inference_steps,
|
| 149 |
],
|
| 150 |
-
outputs=[
|
| 151 |
)
|
| 152 |
|
|
|
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import random
|
|
|
|
|
|
|
|
|
|
| 6 |
import torch
|
| 7 |
+
from diffusers import DiffusionPipeline
|
| 8 |
+
|
| 9 |
+
# --- 1. إعداد النموذج والجهاز ---
|
| 10 |
|
| 11 |
+
# التحقق من وجود GPU وتحديد نوع البيانات
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 14 |
|
| 15 |
+
# هام: استبدل "YourUsername/Takween-v1" بالمعرف الخاص بنموذجك الذي رفعته على Hugging Face
|
| 16 |
+
MODEL_ID = "YourUsername/Takween-v1"
|
| 17 |
+
BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 18 |
+
|
| 19 |
+
# تحميل النموذج مع خطة بديلة
|
| 20 |
+
try:
|
| 21 |
+
pipe = DiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=torch_dtype)
|
| 22 |
+
print(f"✅ Trained model '{MODEL_ID}' loaded successfully from the Hub.")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"❌ Could not load trained model '{MODEL_ID}'. Loading base model. Error: {e}")
|
| 25 |
+
pipe = DiffusionPipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=torch_dtype)
|
| 26 |
|
|
|
|
| 27 |
pipe = pipe.to(device)
|
| 28 |
|
| 29 |
+
# تحديد أقصى قيمة للبذرة العشوائية
|
| 30 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 31 |
|
| 32 |
+
# --- 2. دالة توليد الصور ---
|
| 33 |
|
|
|
|
| 34 |
def infer(
|
| 35 |
prompt,
|
| 36 |
negative_prompt,
|
| 37 |
seed,
|
| 38 |
randomize_seed,
|
|
|
|
|
|
|
| 39 |
guidance_scale,
|
| 40 |
num_inference_steps,
|
| 41 |
progress=gr.Progress(track_tqdm=True),
|
| 42 |
):
|
| 43 |
+
"""
|
| 44 |
+
تستقبل هذه الدالة المدخلات من الواجهة وتقوم بتوليد الصورة.
|
| 45 |
+
"""
|
| 46 |
if randomize_seed:
|
| 47 |
seed = random.randint(0, MAX_SEED)
|
| 48 |
|
| 49 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 50 |
|
| 51 |
image = pipe(
|
| 52 |
prompt=prompt,
|
| 53 |
negative_prompt=negative_prompt,
|
| 54 |
guidance_scale=guidance_scale,
|
| 55 |
+
num_inference_steps=int(num_inference_steps),
|
|
|
|
|
|
|
| 56 |
generator=generator,
|
| 57 |
).images[0]
|
| 58 |
|
| 59 |
+
# إرجاع الصورة والبذرة المستخدمة لعرضها في الواجهة
|
| 60 |
return image, seed
|
| 61 |
|
| 62 |
|
| 63 |
+
# --- 3. تصميم واجهة Gradio ---
|
| 64 |
+
|
| 65 |
+
# أمثلة خاصة بمشروع تكوين
|
| 66 |
examples = [
|
| 67 |
+
"A filled red circle with a black border",
|
| 68 |
+
"An outline blue triangle positioned to the left of a yellow square",
|
| 69 |
+
"A green star overlapping a purple rectangle",
|
| 70 |
]
|
| 71 |
|
| 72 |
+
# تنسيق CSS بسيط لتوسيط الواجهة
|
| 73 |
css = """
|
| 74 |
#col-container {
|
| 75 |
margin: 0 auto;
|
| 76 |
+
max-width: 700px;
|
| 77 |
}
|
| 78 |
"""
|
| 79 |
|
| 80 |
+
# بناء الواجهة باستخدام gr.Blocks
|
| 81 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 82 |
with gr.Column(elem_id="col-container"):
|
| 83 |
+
# العنوان الرئيسي
|
| 84 |
+
gr.Markdown("# مشروع تكوين (Takween Project)")
|
| 85 |
+
gr.Markdown("#### نموذج متخصص في تحويل الأوصاف النصية إلى صور هندسية دقيقة.")
|
| 86 |
|
| 87 |
+
# صف الإدخال الرئيسي وزر التشغيل
|
| 88 |
with gr.Row():
|
| 89 |
prompt = gr.Text(
|
| 90 |
+
label="أدخل الوصف النصي هنا (Prompt)",
|
| 91 |
show_label=False,
|
| 92 |
max_lines=1,
|
| 93 |
+
placeholder="دائرة حمراء ممتلئة على خلفية زرقاء...",
|
| 94 |
container=False,
|
| 95 |
)
|
| 96 |
+
run_button = gr.Button("تكوين", scale=0, variant="primary")
|
| 97 |
|
| 98 |
+
# عرض الصورة الناتجة
|
| 99 |
+
result_image = gr.Image(label="الصورة المولدة", show_label=False)
|
| 100 |
|
| 101 |
+
# قسم الإعدادات المتقدمة
|
| 102 |
+
with gr.Accordion("الإعدادات المتقدمة (Advanced Settings)", open=False):
|
|
|
|
| 103 |
negative_prompt = gr.Text(
|
| 104 |
+
label="الوصف السلبي (Negative Prompt)",
|
| 105 |
max_lines=1,
|
| 106 |
+
placeholder="أدخل ما لا ترغب في رؤيته في الصورة",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
+
|
|
|
|
|
|
|
| 109 |
with gr.Row():
|
| 110 |
+
seed = gr.Slider(
|
| 111 |
+
label="البذرة (Seed)",
|
| 112 |
+
minimum=0,
|
| 113 |
+
maximum=MAX_SEED,
|
| 114 |
+
step=1,
|
| 115 |
+
value=0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
)
|
| 117 |
+
randomize_seed = gr.Checkbox(label="بذرة عشوائية", value=True)
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
guidance_scale = gr.Slider(
|
| 121 |
+
label="مقياس التوجيه (Guidance Scale)",
|
| 122 |
minimum=0.0,
|
| 123 |
+
maximum=20.0,
|
| 124 |
step=0.1,
|
| 125 |
+
value=7.5, # قيمة مثالية لنموذج SD v1.5
|
| 126 |
)
|
|
|
|
| 127 |
num_inference_steps = gr.Slider(
|
| 128 |
+
label="عدد خطوات التوليد",
|
| 129 |
minimum=1,
|
| 130 |
+
maximum=100,
|
| 131 |
step=1,
|
| 132 |
+
value=30, # قيمة جيدة ومتوازنة
|
| 133 |
)
|
| 134 |
+
|
| 135 |
+
# عرض أمثلة جاهزة
|
| 136 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 137 |
+
|
| 138 |
+
# قسم فريق العمل والشكر
|
| 139 |
+
with gr.Accordion("فريق العمل والشكر والتقدير", open=True):
|
| 140 |
+
gr.Markdown("""
|
| 141 |
+
<div style='text-align: right; direction: rtl;'>
|
| 142 |
+
<h4><b>فريق العمل:</b></h4>
|
| 143 |
+
<ul>
|
| 144 |
+
<li>أسامة سعيد</li>
|
| 145 |
+
<li>طارق العمري</li>
|
| 146 |
+
</ul>
|
| 147 |
+
<hr>
|
| 148 |
+
<h4><b>شكر وتقدير خاص:</b></h4>
|
| 149 |
+
<p>نتقدم بجزيل الشكر والتقدير لكل من ساهم في إنجاح هذا العمل بإرشاده ودعمه:</p>
|
| 150 |
+
<ul>
|
| 151 |
+
<li><b>الدكتور القدير/ أكرم الصباري</b> (أستاذ الذكاء الاصطناعي وتعلم الآلة)</li>
|
| 152 |
+
<li><b>المهندسة/ فاتن الحيافي</b> (أستاذة الجانب العملي)</li>
|
| 153 |
+
</ul>
|
| 154 |
+
</div>
|
| 155 |
+
""")
|
| 156 |
+
|
| 157 |
+
# ربط المدخلات والمخرجات بالدالة
|
| 158 |
gr.on(
|
| 159 |
triggers=[run_button.click, prompt.submit],
|
| 160 |
fn=infer,
|
|
|
|
| 163 |
negative_prompt,
|
| 164 |
seed,
|
| 165 |
randomize_seed,
|
|
|
|
|
|
|
| 166 |
guidance_scale,
|
| 167 |
num_inference_steps,
|
| 168 |
],
|
| 169 |
+
outputs=[result_image, seed],
|
| 170 |
)
|
| 171 |
|
| 172 |
+
# لتشغيل الواجهة محلياً
|
| 173 |
if __name__ == "__main__":
|
| 174 |
+
demo.launch()
|