Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,18 @@
|
|
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 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
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 |
-
|
|
|
|
|
27 |
|
28 |
-
# وظيفة
|
29 |
def train_model(user_email, images, progress):
|
30 |
if not user_email:
|
31 |
return "يجب تسجيل الدخول لحفظ النموذج."
|
@@ -33,41 +20,18 @@ def train_model(user_email, images, progress):
|
|
33 |
user_model_id = f"user_model_{user_email}"
|
34 |
saved_models[user_email] = user_model_id
|
35 |
|
36 |
-
# محاكاة عملية التدريب (يمكنك تعديل هذا الجزء ليتناسب مع عملية التدريب
|
37 |
-
for i in range(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
|
45 |
-
if
|
46 |
-
|
47 |
-
|
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,11 +39,11 @@ 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.
|
83 |
|
84 |
with gr.Column():
|
85 |
prompt_input = gr.Textbox(label="✏️ أدخل البرومبت لإنشاء صورة")
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
import time
|
3 |
|
4 |
+
# محاكاة وظيفة تسجيل الدخول
|
5 |
+
def login(email, password):
|
6 |
+
if email == "[email protected]" and password == "password":
|
7 |
+
return "تم تسجيل الدخول بنجاح!"
|
8 |
+
else:
|
9 |
+
return "البريد الإلكتروني أو كلمة المرور غير صحيحة."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# محاكاة وظيفة التسجيل
|
12 |
+
def register(email, password):
|
13 |
+
return f"تم تسجيل حساب جديد للبريد الإلكتروني: {email}"
|
14 |
|
15 |
+
# محاكاة وظيفة التدريب
|
16 |
def train_model(user_email, images, progress):
|
17 |
if not user_email:
|
18 |
return "يجب تسجيل الدخول لحفظ النموذج."
|
|
|
20 |
user_model_id = f"user_model_{user_email}"
|
21 |
saved_models[user_email] = user_model_id
|
22 |
|
23 |
+
# محاكاة عملية التدريب (يمكنك تعديل هذا الجزء ليتناسب مع عملية التدريب الفعلية)
|
24 |
+
for i in range(1, 101): # محاكاة من 0% إلى 100%
|
25 |
time.sleep(0.1) # تأخير زمني لمحاكاة الوقت
|
26 |
+
progress(f"{i}%") # تحديث التقدم النصي
|
27 |
|
28 |
return f"✅ تم حفظ النموذج بنجاح: {user_model_id}"
|
29 |
|
30 |
+
# محاكاة وظيفة إنشاء الصورة
|
31 |
+
def generate_image(prompt, user_email):
|
32 |
+
if not user_email:
|
33 |
+
return "يجب تسجيل الدخول لإنشاء صورة."
|
34 |
+
return f"📸 تم إنشاء صورة بناءً على البرومبت: {prompt}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# واجهة Gradio
|
37 |
with gr.Blocks() as demo:
|
|
|
39 |
|
40 |
with gr.Row():
|
41 |
with gr.Column():
|
42 |
+
image_input = gr.Files(label="📤 رفع صورك للتدريب")
|
43 |
user_email_input = gr.Textbox(label="📧 بريدك الإلكتروني (اختياري لحفظ النموذج)")
|
44 |
train_button = gr.Button("🔧 تدريب النموذج")
|
45 |
train_output = gr.Textbox(label="🔔 نتيجة التدريب")
|
46 |
+
progress_bar = gr.Textbox(label="🔄 التقدم", interactive=False) # تحديث التقدم هنا
|
47 |
|
48 |
with gr.Column():
|
49 |
prompt_input = gr.Textbox(label="✏️ أدخل البرومبت لإنشاء صورة")
|