Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,73 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
import time
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
return "تم تسجيل الدخول بنجاح!"
|
8 |
-
else:
|
9 |
-
return "البريد الإلكتروني أو كلمة المرور غير صحيحة."
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
# محاكاة وظيفة التدريب
|
16 |
-
def train_model(images, progress):
|
17 |
# محاكاة عملية التدريب (يمكنك تعديل هذا الجزء ليتناسب مع عملية التدريب الفعلية)
|
18 |
for i in range(1, 101): # محاكاة من 0% إلى 100%
|
19 |
time.sleep(0.1) # تأخير زمني لمحاكاة الوقت
|
20 |
-
progress(
|
21 |
|
22 |
-
return "✅ تم حفظ النموذج
|
23 |
|
24 |
-
#
|
25 |
-
def generate_image(prompt):
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# واجهة Gradio
|
29 |
with gr.Blocks() as demo:
|
@@ -31,18 +75,19 @@ with gr.Blocks() as demo:
|
|
31 |
|
32 |
with gr.Row():
|
33 |
with gr.Column():
|
34 |
-
image_input = gr.Files(label="📤 رفع صورك للتدريب")
|
|
|
35 |
train_button = gr.Button("🔧 تدريب النموذج")
|
36 |
train_output = gr.Textbox(label="🔔 نتيجة التدريب")
|
37 |
-
progress_bar = gr.
|
38 |
|
39 |
with gr.Column():
|
40 |
prompt_input = gr.Textbox(label="✏️ أدخل البرومبت لإنشاء صورة")
|
41 |
generate_button = gr.Button("🎨 إنشاء صورة")
|
42 |
output_image = gr.Image(label="📷 الصورة الناتجة")
|
43 |
|
44 |
-
train_button.click(train_model, inputs=[image_input, progress_bar], outputs=train_output)
|
45 |
-
generate_button.click(generate_image, inputs=[prompt_input], outputs=output_image)
|
46 |
|
47 |
gr.Markdown("### 🔑 تسجيل الد��ول / التسجيل")
|
48 |
|
@@ -61,6 +106,6 @@ with gr.Blocks() as demo:
|
|
61 |
|
62 |
login_button.click(login, inputs=[login_email, login_password], outputs=login_output)
|
63 |
register_button.click(register, inputs=[register_email, register_password], outputs=register_output)
|
64 |
-
|
65 |
-
# تشغيل التطبيق
|
66 |
-
demo.launch()
|
|
|
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 |
+
import json
|
7 |
import time
|
8 |
|
9 |
+
# تحميل نموذج Stable Diffusion الأخف
|
10 |
+
MODEL_NAME = "runwayml/stable-diffusion-v1-5"
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
12 |
|
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):
|
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 |
+
# وظيفة إنشاء الصور باستخدام البرومبتات
|
44 |
+
def generate_image(prompt, user_email=None):
|
45 |
+
if user_email and user_email in saved_models:
|
46 |
+
model_id = saved_models[user_email]
|
47 |
+
result = pipe(prompt).images[0]
|
48 |
+
else:
|
49 |
+
result = pipe(prompt).images[0]
|
50 |
+
|
51 |
+
return result
|
52 |
+
|
53 |
+
# وظيفة تسجيل المستخدمين
|
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:
|
|
|
75 |
|
76 |
with gr.Row():
|
77 |
with gr.Column():
|
78 |
+
image_input = gr.Files(label="📤 رفع صورك للتدريب") # ✅ تعديل هنا
|
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 |
|
|
|
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(share=True)
|