Spaces:
Runtime error
Runtime error
Create ImageGen
Browse files
ImageGen
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
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 = "CompVis/stable-diffusion-v1-4"
|
| 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 |
+
|
| 18 |
+
# وظيفة تدريب النموذج
|
| 19 |
+
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]
|
| 37 |
+
|
| 38 |
+
return result
|
| 39 |
+
|
| 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)
|
| 71 |
+
|
| 72 |
+
gr.Markdown("### 🔑 تسجيل الدخول / التسجيل")
|
| 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)
|
| 89 |
+
|
| 90 |
+
# تشغيل التطبيق
|
| 91 |
+
demo.launch()
|