Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ 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 = "runwayml/stable-diffusion-v1-5"
|
@@ -11,18 +13,31 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
11 |
pipe = StableDiffusionPipeline.from_pretrained(MODEL_NAME)
|
12 |
pipe.to(device)
|
13 |
|
14 |
-
# قاعدة بيانات
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
saved_models = {}
|
17 |
|
18 |
-
# وظيفة تدريب النموذج
|
19 |
-
def train_model(user_email, images):
|
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 |
# وظيفة إنشاء الصور باستخدام البرومبتات
|
@@ -39,15 +54,21 @@ def generate_image(prompt, user_email=None):
|
|
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("# 🖼️ إنشاء صور مخصصة باستخدام الذكاء الاصطناعي")
|
@@ -58,15 +79,16 @@ with gr.Blocks() as demo:
|
|
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)
|
69 |
-
|
70 |
gr.Markdown("### 🔑 تسجيل الدخول / التسجيل")
|
71 |
|
72 |
with gr.Row():
|
@@ -75,15 +97,15 @@ with gr.Blocks() as demo:
|
|
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)
|
87 |
-
|
88 |
# تشغيل التطبيق
|
89 |
demo.launch()
|
|
|
3 |
import torch
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
from werkzeug.security import generate_password_hash, check_password_hash
|
6 |
+
import json
|
7 |
+
import time
|
8 |
|
9 |
# تحميل نموذج Stable Diffusion الأخف
|
10 |
MODEL_NAME = "runwayml/stable-diffusion-v1-5"
|
|
|
13 |
pipe = StableDiffusionPipeline.from_pretrained(MODEL_NAME)
|
14 |
pipe.to(device)
|
15 |
|
16 |
+
# قاعدة بيانات للمستخدمين والنماذج المحفوظة (سيتم حفظها في ملف JSON)
|
17 |
+
user_data_file = "user_data.json"
|
18 |
+
|
19 |
+
# تحميل البيانات من ملف JSON إذا كان موجودًا
|
20 |
+
if os.path.exists(user_data_file):
|
21 |
+
with open(user_data_file, "r") as f:
|
22 |
+
users = json.load(f)
|
23 |
+
else:
|
24 |
+
users = {}
|
25 |
+
|
26 |
saved_models = {}
|
27 |
|
28 |
+
# وظيفة تدريب النموذج (التدريب يتم مرة واحدة فقط)
|
29 |
+
def train_model(user_email, images, progress=gr.Progress()):
|
30 |
if not user_email:
|
31 |
return "يجب تسجيل الدخول لحفظ النموذج."
|
32 |
|
33 |
user_model_id = f"user_model_{user_email}"
|
34 |
saved_models[user_email] = user_model_id
|
35 |
|
36 |
+
# محاكاة عملية التدريب (يمكنك تعديل هذا الجزء ليتناسب مع عملية التدريب الفعلية)
|
37 |
+
for i in range(1, 101): # محاكاة من 0% إلى 100%
|
38 |
+
time.sleep(0.1) # تأخير زمني لمحاكاة الوقت
|
39 |
+
progress(i) # تحديث شريط التقدم
|
40 |
+
|
41 |
return f"✅ تم حفظ النموذج بنجاح: {user_model_id}"
|
42 |
|
43 |
# وظيفة إنشاء الصور باستخدام البرومبتات
|
|
|
54 |
def register(email, password):
|
55 |
if email in users:
|
56 |
return "⚠️ البريد الإلكتروني مسجل بالفعل!"
|
57 |
+
users[email] = {"password": generate_password_hash(password)}
|
58 |
+
save_users_data()
|
59 |
return "✅ تم التسجيل بنجاح!"
|
60 |
|
61 |
# وظيفة تسجيل الدخول
|
62 |
def login(email, password):
|
63 |
+
if email not in users or not check_password_hash(users[email]["password"], password):
|
64 |
return "❌ البريد الإلكتروني أو كلمة المرور غير صحيحة!"
|
65 |
return f"✅ تم تسجيل الدخول: {email}"
|
66 |
|
67 |
+
# حفظ بيانات المستخدمين إلى ملف JSON
|
68 |
+
def save_users_data():
|
69 |
+
with open(user_data_file, "w") as f:
|
70 |
+
json.dump(users, f)
|
71 |
+
|
72 |
# واجهة Gradio
|
73 |
with gr.Blocks() as demo:
|
74 |
gr.Markdown("# 🖼️ إنشاء صور مخصصة باستخدام الذكاء الاصطناعي")
|
|
|
79 |
user_email_input = gr.Textbox(label="📧 بريدك الإلكتروني (اختياري لحفظ النموذج)")
|
80 |
train_button = gr.Button("🔧 تدريب النموذج")
|
81 |
train_output = gr.Textbox(label="🔔 نتيجة التدريب")
|
82 |
+
progress_bar = gr.Progress(label="🕒 تقدم التدريب")
|
83 |
+
|
84 |
with gr.Column():
|
85 |
prompt_input = gr.Textbox(label="✏️ أدخل البرومبت لإنشاء صورة")
|
86 |
generate_button = gr.Button("🎨 إنشاء صورة")
|
87 |
output_image = gr.Image(label="📷 الصورة الناتجة")
|
88 |
+
|
89 |
+
train_button.click(train_model, inputs=[user_email_input, image_input, progress_bar], outputs=train_output)
|
90 |
generate_button.click(generate_image, inputs=[prompt_input, user_email_input], outputs=output_image)
|
91 |
+
|
92 |
gr.Markdown("### 🔑 تسجيل الدخول / التسجيل")
|
93 |
|
94 |
with gr.Row():
|
|
|
97 |
login_password = gr.Textbox(label="🔑 كلمة المرور", type="password")
|
98 |
login_button = gr.Button("🚀 تسجيل الدخول")
|
99 |
login_output = gr.Textbox(label="🔔 حالة تسجيل الدخول")
|
100 |
+
|
101 |
with gr.Column():
|
102 |
register_email = gr.Textbox(label="📧 البريد الإلكتروني للتسجيل")
|
103 |
register_password = gr.Textbox(label="🔑 كلمة المرور للتسجيل", type="password")
|
104 |
register_button = gr.Button("📝 تسجيل حساب جديد")
|
105 |
register_output = gr.Textbox(label="🔔 حالة التسجيل")
|
106 |
+
|
107 |
login_button.click(login, inputs=[login_email, login_password], outputs=login_output)
|
108 |
register_button.click(register, inputs=[register_email, register_password], outputs=register_output)
|
109 |
+
|
110 |
# تشغيل التطبيق
|
111 |
demo.launch()
|