Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,14 @@ import torch
|
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 6 |
|
| 7 |
-
# تحميل نموذج Stable Diffusion
|
| 8 |
-
MODEL_NAME = "
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
pipe = StableDiffusionPipeline.from_pretrained(MODEL_NAME)
|
| 12 |
pipe.to(device)
|
| 13 |
|
| 14 |
-
# قاعدة بيانات بسيطة
|
| 15 |
users = {"[email protected]": generate_password_hash("admin123")}
|
| 16 |
saved_models = {}
|
| 17 |
|
|
@@ -20,17 +20,15 @@ def train_model(user_email, images):
|
|
| 20 |
if not user_email:
|
| 21 |
return "يجب تسجيل الدخول لحفظ النموذج."
|
| 22 |
|
| 23 |
-
# محاكاة تدريب نموذج على الصور (DreamBooth أو Fine-Tuning)
|
| 24 |
user_model_id = f"user_model_{user_email}"
|
| 25 |
saved_models[user_email] = user_model_id
|
| 26 |
|
| 27 |
-
return f"تم حفظ النموذج بنجاح: {user_model_id}"
|
| 28 |
|
| 29 |
# وظيفة إنشاء الصور باستخدام البرومبتات
|
| 30 |
def generate_image(prompt, user_email=None):
|
| 31 |
if user_email and user_email in saved_models:
|
| 32 |
model_id = saved_models[user_email]
|
| 33 |
-
# هنا يمكن استخدام نموذج مخصص لكل مستخدم إذا كان محفوظًا
|
| 34 |
result = pipe(prompt).images[0]
|
| 35 |
else:
|
| 36 |
result = pipe(prompt).images[0]
|
|
@@ -40,31 +38,31 @@ def generate_image(prompt, user_email=None):
|
|
| 40 |
# وظيفة تسجيل المستخدمين
|
| 41 |
def register(email, password):
|
| 42 |
if email in users:
|
| 43 |
-
return "البريد الإلكتروني مسجل بالفعل!"
|
| 44 |
users[email] = generate_password_hash(password)
|
| 45 |
-
return "تم التسجيل بنجاح!"
|
| 46 |
|
| 47 |
# وظيفة تسجيل الدخول
|
| 48 |
def login(email, password):
|
| 49 |
if email not in users or not check_password_hash(users[email], password):
|
| 50 |
-
return "البريد الإلكتروني أو كلمة المرور غير صحيحة!"
|
| 51 |
-
return f"تم تسجيل الدخول: {email}"
|
| 52 |
|
| 53 |
# واجهة Gradio
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
-
gr.Markdown("# 🖼️
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column():
|
| 59 |
-
image_input = gr.File(label="رفع صورك للتدريب", type="file", multiple=True)
|
| 60 |
-
user_email_input = gr.Textbox(label="
|
| 61 |
-
train_button = gr.Button("تدريب النموذج")
|
| 62 |
-
train_output = gr.Textbox(label="نتيجة التدريب")
|
| 63 |
|
| 64 |
with gr.Column():
|
| 65 |
-
prompt_input = gr.Textbox(label="أدخل البرومبت لإنشاء صورة")
|
| 66 |
-
generate_button = gr.Button("إنشاء صورة")
|
| 67 |
-
output_image = gr.Image(label="الصورة الناتجة")
|
| 68 |
|
| 69 |
train_button.click(train_model, inputs=[user_email_input, image_input], outputs=train_output)
|
| 70 |
generate_button.click(generate_image, inputs=[prompt_input, user_email_input], outputs=output_image)
|
|
@@ -73,16 +71,16 @@ with gr.Blocks() as demo:
|
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column():
|
| 76 |
-
login_email = gr.Textbox(label="البريد الإلكتروني")
|
| 77 |
-
login_password = gr.Textbox(label="كلمة المرور", type="password")
|
| 78 |
-
login_button = gr.Button("تسجيل الدخول")
|
| 79 |
-
login_output = gr.Textbox(label="حالة تسجيل الدخول")
|
| 80 |
|
| 81 |
with gr.Column():
|
| 82 |
-
register_email = gr.Textbox(label="البريد الإلكتروني للتسجيل")
|
| 83 |
-
register_password = gr.Textbox(label="كلمة المرور للتسجيل", type="password")
|
| 84 |
-
register_button = gr.Button("تسجيل حساب جديد")
|
| 85 |
-
register_output = gr.Textbox(label="حالة التسجيل")
|
| 86 |
|
| 87 |
login_button.click(login, inputs=[login_email, login_password], outputs=login_output)
|
| 88 |
register_button.click(register, inputs=[register_email, register_password], outputs=register_output)
|
|
|
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
from werkzeug.security import generate_password_hash, check_password_hash
|
| 6 |
|
| 7 |
+
# تحميل نموذج Stable Diffusion الأخف
|
| 8 |
+
MODEL_NAME = "runwayml/stable-diffusion-v1-5"
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
pipe = StableDiffusionPipeline.from_pretrained(MODEL_NAME)
|
| 12 |
pipe.to(device)
|
| 13 |
|
| 14 |
+
# قاعدة بيانات بسيطة للمستخدمين والنماذج المحفوظة
|
| 15 |
users = {"[email protected]": generate_password_hash("admin123")}
|
| 16 |
saved_models = {}
|
| 17 |
|
|
|
|
| 20 |
if not user_email:
|
| 21 |
return "يجب تسجيل الدخول لحفظ النموذج."
|
| 22 |
|
|
|
|
| 23 |
user_model_id = f"user_model_{user_email}"
|
| 24 |
saved_models[user_email] = user_model_id
|
| 25 |
|
| 26 |
+
return f"✅ تم حفظ النموذج بنجاح: {user_model_id}"
|
| 27 |
|
| 28 |
# وظيفة إنشاء الصور باستخدام البرومبتات
|
| 29 |
def generate_image(prompt, user_email=None):
|
| 30 |
if user_email and user_email in saved_models:
|
| 31 |
model_id = saved_models[user_email]
|
|
|
|
| 32 |
result = pipe(prompt).images[0]
|
| 33 |
else:
|
| 34 |
result = pipe(prompt).images[0]
|
|
|
|
| 38 |
# وظيفة تسجيل المستخدمين
|
| 39 |
def register(email, password):
|
| 40 |
if email in users:
|
| 41 |
+
return "⚠️ البريد الإلكتروني مسجل بالفعل!"
|
| 42 |
users[email] = generate_password_hash(password)
|
| 43 |
+
return "✅ تم التسجيل بنجاح!"
|
| 44 |
|
| 45 |
# وظيفة تسجيل الدخول
|
| 46 |
def login(email, password):
|
| 47 |
if email not in users or not check_password_hash(users[email], password):
|
| 48 |
+
return "❌ البريد الإلكتروني أو كلمة المرور غير صحيحة!"
|
| 49 |
+
return f"✅ تم تسجيل الدخول: {email}"
|
| 50 |
|
| 51 |
# واجهة Gradio
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("# 🖼️ إنشاء صور مخصصة باستخدام الذكاء الاصطناعي")
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column():
|
| 57 |
+
image_input = gr.File(label="📤 رفع صورك للتدريب", type="file", multiple=True)
|
| 58 |
+
user_email_input = gr.Textbox(label="📧 بريدك الإلكتروني (اختياري لحفظ النموذج)")
|
| 59 |
+
train_button = gr.Button("🔧 تدريب النموذج")
|
| 60 |
+
train_output = gr.Textbox(label="🔔 نتيجة التدريب")
|
| 61 |
|
| 62 |
with gr.Column():
|
| 63 |
+
prompt_input = gr.Textbox(label="✏️ أدخل البرومبت لإنشاء صورة")
|
| 64 |
+
generate_button = gr.Button("🎨 إنشاء صورة")
|
| 65 |
+
output_image = gr.Image(label="📷 الصورة الناتجة")
|
| 66 |
|
| 67 |
train_button.click(train_model, inputs=[user_email_input, image_input], outputs=train_output)
|
| 68 |
generate_button.click(generate_image, inputs=[prompt_input, user_email_input], outputs=output_image)
|
|
|
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
with gr.Column():
|
| 74 |
+
login_email = gr.Textbox(label="📧 البريد الإلكتروني")
|
| 75 |
+
login_password = gr.Textbox(label="🔑 كلمة المرور", type="password")
|
| 76 |
+
login_button = gr.Button("🚀 تسجيل الدخول")
|
| 77 |
+
login_output = gr.Textbox(label="🔔 حالة تسجيل الدخول")
|
| 78 |
|
| 79 |
with gr.Column():
|
| 80 |
+
register_email = gr.Textbox(label="📧 البريد الإلكتروني للتسجيل")
|
| 81 |
+
register_password = gr.Textbox(label="🔑 كلمة المرور للتسجيل", type="password")
|
| 82 |
+
register_button = gr.Button("📝 تسجيل حساب جديد")
|
| 83 |
+
register_output = gr.Textbox(label="🔔 حالة التسجيل")
|
| 84 |
|
| 85 |
login_button.click(login, inputs=[login_email, login_password], outputs=login_output)
|
| 86 |
register_button.click(register, inputs=[register_email, register_password], outputs=register_output)
|