Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import torch | |
from diffusers import StableDiffusionPipeline | |
from werkzeug.security import generate_password_hash, check_password_hash | |
# تحميل نموذج Stable Diffusion مسبقًا | |
MODEL_NAME = "CompVis/stable-diffusion-v1-4" | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
pipe = StableDiffusionPipeline.from_pretrained(MODEL_NAME) | |
pipe.to(device) | |
# قاعدة بيانات بسيطة لتخزين المستخدمين والنماذج المحفوظة | |
users = {"[email protected]": generate_password_hash("admin123")} | |
saved_models = {} | |
# وظيفة تدريب النموذج | |
def train_model(user_email, images): | |
if not user_email: | |
return "يجب تسجيل الدخول لحفظ النموذج." | |
# محاكاة تدريب نموذج على الصور (DreamBooth أو Fine-Tuning) | |
user_model_id = f"user_model_{user_email}" | |
saved_models[user_email] = user_model_id | |
return f"تم حفظ النموذج بنجاح: {user_model_id}" | |
# وظيفة إنشاء الصور باستخدام البرومبتات | |
def generate_image(prompt, user_email=None): | |
if user_email and user_email in saved_models: | |
model_id = saved_models[user_email] | |
# هنا يمكن استخدام نموذج مخصص لكل مستخدم إذا كان محفوظًا | |
result = pipe(prompt).images[0] | |
else: | |
result = pipe(prompt).images[0] | |
return result | |
# وظيفة تسجيل المستخدمين | |
def register(email, password): | |
if email in users: | |
return "البريد الإلكتروني مسجل بالفعل!" | |
users[email] = generate_password_hash(password) | |
return "تم التسجيل بنجاح!" | |
# وظيفة تسجيل الدخول | |
def login(email, password): | |
if email not in users or not check_password_hash(users[email], password): | |
return "البريد الإلكتروني أو كلمة المرور غير صحيحة!" | |
return f"تم تسجيل الدخول: {email}" | |
# واجهة Gradio | |
with gr.Blocks() as demo: | |
gr.Markdown("# 🖼️ تدريب وإنشاء صور مخصصة باستخدام الذكاء الاصطناعي") | |
with gr.Row(): | |
with gr.Column(): | |
image_input = gr.File(label="رفع صورك للتدريب", type="file", multiple=True) | |
user_email_input = gr.Textbox(label="أدخل بريدك الإلكتروني (اختياري للحفظ)") | |
train_button = gr.Button("تدريب النموذج") | |
train_output = gr.Textbox(label="نتيجة التدريب") | |
with gr.Column(): | |
prompt_input = gr.Textbox(label="أدخل البرومبت لإنشاء صورة") | |
generate_button = gr.Button("إنشاء صورة") | |
output_image = gr.Image(label="الصورة الناتجة") | |
train_button.click(train_model, inputs=[user_email_input, image_input], outputs=train_output) | |
generate_button.click(generate_image, inputs=[prompt_input, user_email_input], outputs=output_image) | |
gr.Markdown("### 🔑 تسجيل الدخول / التسجيل") | |
with gr.Row(): | |
with gr.Column(): | |
login_email = gr.Textbox(label="البريد الإلكتروني") | |
login_password = gr.Textbox(label="كلمة المرور", type="password") | |
login_button = gr.Button("تسجيل الدخول") | |
login_output = gr.Textbox(label="حالة تسجيل الدخول") | |
with gr.Column(): | |
register_email = gr.Textbox(label="البريد الإلكتروني للتسجيل") | |
register_password = gr.Textbox(label="كلمة المرور للتسجيل", type="password") | |
register_button = gr.Button("تسجيل حساب جديد") | |
register_output = gr.Textbox(label="حالة التسجيل") | |
login_button.click(login, inputs=[login_email, login_password], outputs=login_output) | |
register_button.click(register, inputs=[register_email, register_password], outputs=register_output) | |
# تشغيل التطبيق | |
demo.launch() |