Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
|
@@ -8,19 +8,13 @@ from diffusers import DiffusionPipeline
|
|
| 8 |
import time
|
| 9 |
|
| 10 |
# --- 1. الإعدادات والثوابت ---
|
| 11 |
-
|
| 12 |
-
# تحديد الجهاز ونوع البيانات
|
| 13 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
# هام: استبدل "YourUsername/Takween-v1" بالمعرف الخاص بنموذجك الذي رفعته على Hugging Face
|
| 18 |
-
MODEL_ID = "YourUsername/Takween-v1"
|
| 19 |
BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 20 |
-
|
| 21 |
MAX_SEED = np.iinfo(np.int32).max
|
| 22 |
|
| 23 |
-
# شعار المشروع (SVG مدمج)
|
| 24 |
LOGO_SVG = """
|
| 25 |
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
| 26 |
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z"></path>
|
|
@@ -30,13 +24,14 @@ LOGO_SVG = """
|
|
| 30 |
"""
|
| 31 |
|
| 32 |
# --- 2. تحميل النموذج ---
|
|
|
|
| 33 |
try:
|
| 34 |
pipe = DiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=DTYPE, safety_checker=None)
|
| 35 |
print(f"✅ Trained model '{MODEL_ID}' loaded successfully.")
|
| 36 |
except Exception:
|
| 37 |
print(f"❌ Could not load trained model '{MODEL_ID}'. Loading base model.")
|
| 38 |
-
|
| 39 |
-
(BASE_MODEL_ID, torch_dtype=DTYPE, safety_checker=None)
|
| 40 |
|
| 41 |
pipe = pipe.to(DEVICE)
|
| 42 |
|
|
@@ -50,7 +45,6 @@ theme = gr.themes.Base(
|
|
| 50 |
block_background_fill="white",
|
| 51 |
block_border_width="1px",
|
| 52 |
block_shadow="*shadow_drop_lg",
|
| 53 |
-
button_shadow="*shadow_push",
|
| 54 |
button_primary_background_fill="*primary_500",
|
| 55 |
button_primary_background_fill_hover="*primary_600",
|
| 56 |
)
|
|
@@ -62,13 +56,11 @@ def infer(prompt, negative_prompt, guidance_scale, num_inference_steps, seed, ra
|
|
| 62 |
|
| 63 |
generator = torch.Generator(device=DEVICE).manual_seed(seed)
|
| 64 |
|
| 65 |
-
# تحديث الواجهة لإظهار حالة التحميل
|
| 66 |
yield {
|
| 67 |
output_image: gr.update(value=None, interactive=False, visible=True),
|
| 68 |
run_button: gr.update(interactive=False, value="...جاري التكوين"),
|
| 69 |
}
|
| 70 |
|
| 71 |
-
# توليد الصورة
|
| 72 |
image = pipe(
|
| 73 |
prompt=prompt,
|
| 74 |
negative_prompt=negative_prompt,
|
|
@@ -77,54 +69,33 @@ def infer(prompt, negative_prompt, guidance_scale, num_inference_steps, seed, ra
|
|
| 77 |
generator=generator,
|
| 78 |
).images[0]
|
| 79 |
|
| 80 |
-
# تحديث الواجهة بالنتائج النهائية
|
| 81 |
yield {
|
| 82 |
output_image: gr.update(value=image, interactive=True),
|
| 83 |
output_seed: gr.update(value=seed),
|
| 84 |
run_button: gr.update(interactive=True, value="كوّن مرة أخرى"),
|
| 85 |
}
|
| 86 |
|
| 87 |
-
|
| 88 |
# --- 5. بناء الواجهة الاحترافية ---
|
| 89 |
with gr.Blocks(theme=theme, css="#footer {text-align: center;}") as demo:
|
| 90 |
-
# رأس الصفحة (Header)
|
| 91 |
with gr.Row():
|
| 92 |
gr.HTML(f"<div style='display: flex; align-items: center; gap: 12px;'>{LOGO_SVG}<h1>مشروع تكوين</h1></div>")
|
| 93 |
-
|
| 94 |
gr.Markdown("#### نموذج متخصص في تحويل الأوصاف النصية إلى صور هندسية دقيقة.")
|
| 95 |
gr.HTML("<hr>")
|
| 96 |
-
|
| 97 |
-
# الهيكل الرئيسي (عمودين)
|
| 98 |
with gr.Row():
|
| 99 |
-
# العمود الأيسر: الإعدادات
|
| 100 |
with gr.Column(scale=1):
|
| 101 |
prompt = gr.Textbox(label="الوصف (Prompt)", placeholder=" دائرة حمراء بحدود سوداء سميكة...", lines=3)
|
| 102 |
negative_prompt = gr.Textbox(label="الوصف السلبي (Negative Prompt)", placeholder=" جودة منخفضة، ضبابية، تشوه...")
|
| 103 |
-
|
| 104 |
with gr.Accordion("الإعدادات المتقدمة", open=False):
|
| 105 |
guidance_scale = gr.Slider(label="مقياس التوجيه", minimum=1.0, maximum=20.0, value=7.5, step=0.1)
|
| 106 |
num_inference_steps = gr.Slider(label="عدد الخطوات", minimum=10, maximum=100, value=30, step=1)
|
| 107 |
with gr.Row():
|
| 108 |
seed = gr.Number(label="البذرة (Seed)", value=0, precision=0)
|
| 109 |
randomize_seed = gr.Checkbox(label="عشوائي", value=True)
|
| 110 |
-
|
| 111 |
run_button = gr.Button("كوّن الصورة", variant="primary")
|
| 112 |
-
|
| 113 |
-
gr.Examples(
|
| 114 |
-
examples=[
|
| 115 |
-
"A filled red circle with a thick black border",
|
| 116 |
-
"An outline blue triangle positioned to the left of a yellow square",
|
| 117 |
-
"A green star overlapping a purple rectangle",
|
| 118 |
-
],
|
| 119 |
-
inputs=[prompt]
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
# العمود الأيمن: النتائج
|
| 123 |
with gr.Column(scale=2):
|
| 124 |
output_image = gr.Image(label="الصورة المولّدة", interactive=False, height=512)
|
| 125 |
output_seed = gr.Textbox(label="البذرة المستخدمة (Seed)", interactive=False)
|
| 126 |
-
|
| 127 |
-
# تذييل الصفحة (Footer)
|
| 128 |
gr.HTML("<hr>")
|
| 129 |
with gr.Accordion("فريق العمل والشكر والتقدير", open=False):
|
| 130 |
gr.Markdown("""
|
|
@@ -135,8 +106,7 @@ with gr.Blocks(theme=theme, css="#footer {text-align: center;}") as demo:
|
|
| 135 |
</div>
|
| 136 |
""")
|
| 137 |
gr.Markdown("<p id='footer'>© 2025 مشروع تكوين. جميع الحقوق محفوظة.</p>")
|
| 138 |
-
|
| 139 |
-
# ربط الأحداث بالدوال
|
| 140 |
run_button.click(
|
| 141 |
fn=infer,
|
| 142 |
inputs=[prompt, negative_prompt, guidance_scale, num_inference_steps, seed, randomize_seed],
|
|
|
|
| 1 |
+
# app.py (النسخة النهائية والمصححة)
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
|
|
|
| 8 |
import time
|
| 9 |
|
| 10 |
# --- 1. الإعدادات والثوابت ---
|
|
|
|
|
|
|
| 11 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 13 |
|
| 14 |
+
MODEL_ID = "YourUsername/Takween-v1" # هام: استبدل هذا باسم نموذجك على Hugging Face
|
|
|
|
|
|
|
| 15 |
BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
|
|
|
| 16 |
MAX_SEED = np.iinfo(np.int32).max
|
| 17 |
|
|
|
|
| 18 |
LOGO_SVG = """
|
| 19 |
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
| 20 |
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z"></path>
|
|
|
|
| 24 |
"""
|
| 25 |
|
| 26 |
# --- 2. تحميل النموذج ---
|
| 27 |
+
# تم التحقق من صحة هذا الجزء لتجنب أي أخطاء نحوية
|
| 28 |
try:
|
| 29 |
pipe = DiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=DTYPE, safety_checker=None)
|
| 30 |
print(f"✅ Trained model '{MODEL_ID}' loaded successfully.")
|
| 31 |
except Exception:
|
| 32 |
print(f"❌ Could not load trained model '{MODEL_ID}'. Loading base model.")
|
| 33 |
+
# هذا هو السطر الذي تم التحقق من صحته
|
| 34 |
+
pipe = DiffusionPipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=DTYPE, safety_checker=None)
|
| 35 |
|
| 36 |
pipe = pipe.to(DEVICE)
|
| 37 |
|
|
|
|
| 45 |
block_background_fill="white",
|
| 46 |
block_border_width="1px",
|
| 47 |
block_shadow="*shadow_drop_lg",
|
|
|
|
| 48 |
button_primary_background_fill="*primary_500",
|
| 49 |
button_primary_background_fill_hover="*primary_600",
|
| 50 |
)
|
|
|
|
| 56 |
|
| 57 |
generator = torch.Generator(device=DEVICE).manual_seed(seed)
|
| 58 |
|
|
|
|
| 59 |
yield {
|
| 60 |
output_image: gr.update(value=None, interactive=False, visible=True),
|
| 61 |
run_button: gr.update(interactive=False, value="...جاري التكوين"),
|
| 62 |
}
|
| 63 |
|
|
|
|
| 64 |
image = pipe(
|
| 65 |
prompt=prompt,
|
| 66 |
negative_prompt=negative_prompt,
|
|
|
|
| 69 |
generator=generator,
|
| 70 |
).images[0]
|
| 71 |
|
|
|
|
| 72 |
yield {
|
| 73 |
output_image: gr.update(value=image, interactive=True),
|
| 74 |
output_seed: gr.update(value=seed),
|
| 75 |
run_button: gr.update(interactive=True, value="كوّن مرة أخرى"),
|
| 76 |
}
|
| 77 |
|
|
|
|
| 78 |
# --- 5. بناء الواجهة الاحترافية ---
|
| 79 |
with gr.Blocks(theme=theme, css="#footer {text-align: center;}") as demo:
|
|
|
|
| 80 |
with gr.Row():
|
| 81 |
gr.HTML(f"<div style='display: flex; align-items: center; gap: 12px;'>{LOGO_SVG}<h1>مشروع تكوين</h1></div>")
|
|
|
|
| 82 |
gr.Markdown("#### نموذج متخصص في تحويل الأوصاف النصية إلى صور هندسية دقيقة.")
|
| 83 |
gr.HTML("<hr>")
|
|
|
|
|
|
|
| 84 |
with gr.Row():
|
|
|
|
| 85 |
with gr.Column(scale=1):
|
| 86 |
prompt = gr.Textbox(label="الوصف (Prompt)", placeholder=" دائرة حمراء بحدود سوداء سميكة...", lines=3)
|
| 87 |
negative_prompt = gr.Textbox(label="الوصف السلبي (Negative Prompt)", placeholder=" جودة منخفضة، ضبابية، تشوه...")
|
|
|
|
| 88 |
with gr.Accordion("الإعدادات المتقدمة", open=False):
|
| 89 |
guidance_scale = gr.Slider(label="مقياس التوجيه", minimum=1.0, maximum=20.0, value=7.5, step=0.1)
|
| 90 |
num_inference_steps = gr.Slider(label="عدد الخطوات", minimum=10, maximum=100, value=30, step=1)
|
| 91 |
with gr.Row():
|
| 92 |
seed = gr.Number(label="البذرة (Seed)", value=0, precision=0)
|
| 93 |
randomize_seed = gr.Checkbox(label="عشوائي", value=True)
|
|
|
|
| 94 |
run_button = gr.Button("كوّن الصورة", variant="primary")
|
| 95 |
+
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])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
with gr.Column(scale=2):
|
| 97 |
output_image = gr.Image(label="الصورة المولّدة", interactive=False, height=512)
|
| 98 |
output_seed = gr.Textbox(label="البذرة المستخدمة (Seed)", interactive=False)
|
|
|
|
|
|
|
| 99 |
gr.HTML("<hr>")
|
| 100 |
with gr.Accordion("فريق العمل والشكر والتقدير", open=False):
|
| 101 |
gr.Markdown("""
|
|
|
|
| 106 |
</div>
|
| 107 |
""")
|
| 108 |
gr.Markdown("<p id='footer'>© 2025 مشروع تكوين. جميع الحقوق محفوظة.</p>")
|
| 109 |
+
|
|
|
|
| 110 |
run_button.click(
|
| 111 |
fn=infer,
|
| 112 |
inputs=[prompt, negative_prompt, guidance_scale, num_inference_steps, seed, randomize_seed],
|