ahmedda commited on
Commit
e781b5c
·
verified ·
1 Parent(s): 548b808

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +28 -20
ui.py CHANGED
@@ -5,36 +5,38 @@ import io, base64, numpy as np
5
  import pandas as pd
6
  import re
7
 
8
- # إعداد الصفحة
9
  st.set_page_config(page_title="مترجم لغة الإشارة", layout="centered")
10
 
11
- # دالة تنظيف النص العربي
12
  def normalize_arabic(text):
13
  text = str(text).strip()
14
  text = re.sub("[إأآ]", "ا", text)
15
  text = re.sub("ة", "ه", text)
16
  return text
17
 
18
- # تحميل الموديل والبيانات (بيتحملوا مرة واحدة بس)
19
  @st.cache_resource
20
- def load_all():
21
- # تحميل الإكسل
22
  df = pd.read_excel("KARSL-100_Labels (1).xlsx")
 
23
  word_to_id = {normalize_arabic(row['Sign-Arabic']): row['SignID'] for _, row in df.iterrows()}
24
- # تحميل الموديل
25
  model = tf.keras.models.load_model("word_to_sign_model.h5", compile=False)
26
  return word_to_id, model
27
 
28
- # تنفيذ التحميل
29
  try:
30
- word_to_id, text_to_sign_model = load_all()
31
  except Exception as e:
32
  st.error(f"خطأ في تحميل الملفات الأساسية: {e}")
33
 
34
- # تصميم الواجهة
35
  st.title("📂 مترجم النص إلى لغة إشارة")
36
- st.write("اكتب الكلمة لتحويلها إلى صورة إشارة (مثلاً: مناعة، قصبة هوائية)")
37
 
 
38
  word_input = st.text_input("أدخل الكلمة بالعربية:", key="word_input")
39
 
40
  if st.button("تحويل"):
@@ -43,32 +45,38 @@ if st.button("تحويل"):
43
  label_id = word_to_id.get(clean_text)
44
 
45
  if label_id is not None:
46
- # 1. تجهيز الـ One-Hot Encoding (100 عنصر)
47
  one_hot_input = np.zeros((1, 100), dtype=np.float32)
48
  idx = int(label_id) - 1 if int(label_id) > 0 else 0
49
  if idx < 100:
50
  one_hot_input[0, idx] = 1.0
51
 
52
- # 2. التنبؤ بالصورة
53
- with st.spinner('جاري إنشاء الصورة...'):
54
  prediction = text_to_sign_model.predict(one_hot_input)
55
 
56
- # 3. معالجة مصفوفة الصورة للعرض
57
  img_data = prediction[0]
 
 
58
  if img_data.max() <= 1.0:
59
  img_data = (img_data * 255).astype("uint8")
60
  else:
61
  img_data = img_data.astype("uint8")
62
 
63
- # ضبط أبعاد الصورة لو كانت Grayscale
64
  if len(img_data.shape) == 3 and img_data.shape[-1] == 1:
65
  img_data = img_data.reshape(img_data.shape[0], img_data.shape[1])
66
 
67
- # عرض الصورة
68
  img = Image.fromarray(img_data)
69
- st.success(f"تم العثور على إشارة: {word_input}")
70
- st.image(img, caption=f"إشارة {word_input}", use_container_width=True)
 
 
 
 
71
  else:
72
- st.error(f"عذراً، الكلمة '{word_input}' غير موجودة في القاموس الحالي.")
73
  else:
74
- st.warning("من فضلك اكتب كلمة أولاً.")
 
5
  import pandas as pd
6
  import re
7
 
8
+ # إعداد الصفحة لتكون بشكل احترافي
9
  st.set_page_config(page_title="مترجم لغة الإشارة", layout="centered")
10
 
11
+ # دالة تنظيف النص العربي لضمان تطابق الكلمات مع الإكسل
12
  def normalize_arabic(text):
13
  text = str(text).strip()
14
  text = re.sub("[إأآ]", "ا", text)
15
  text = re.sub("ة", "ه", text)
16
  return text
17
 
18
+ # تحميل الموديل والبيانات مرة واحدة فقط (لتسريع الأداء)
19
  @st.cache_resource
20
+ def load_all_assets():
21
+ # تحميل ملف الإكسل الخاص بالكلمات
22
  df = pd.read_excel("KARSL-100_Labels (1).xlsx")
23
+ # إنشاء قاموس لربط الكلمة بالـ ID
24
  word_to_id = {normalize_arabic(row['Sign-Arabic']): row['SignID'] for _, row in df.iterrows()}
25
+ # تحميل موديل التنبؤ بالصور
26
  model = tf.keras.models.load_model("word_to_sign_model.h5", compile=False)
27
  return word_to_id, model
28
 
29
+ # تنفيذ عملية التحميل
30
  try:
31
+ word_to_id, text_to_sign_model = load_all_assets()
32
  except Exception as e:
33
  st.error(f"خطأ في تحميل الملفات الأساسية: {e}")
34
 
35
+ # واجهة المستخدم الرسومية
36
  st.title("📂 مترجم النص إلى لغة إشارة")
37
+ st.write("اكتب الكلمة لتحويلها إلى صورة إشارة (مثال: هيكل عظمي، مناعة، قصبة هوائية)")
38
 
39
+ # حقل إدخال الكلمة
40
  word_input = st.text_input("أدخل الكلمة بالعربية:", key="word_input")
41
 
42
  if st.button("تحويل"):
 
45
  label_id = word_to_id.get(clean_text)
46
 
47
  if label_id is not None:
48
+ # 1. تجهيز المدخلات للموديل (One-Hot Encoding)
49
  one_hot_input = np.zeros((1, 100), dtype=np.float32)
50
  idx = int(label_id) - 1 if int(label_id) > 0 else 0
51
  if idx < 100:
52
  one_hot_input[0, idx] = 1.0
53
 
54
+ # 2. التنبؤ بالصورة باستخدام الموديل
55
+ with st.spinner('جاري معالجة الصورة...'):
56
  prediction = text_to_sign_model.predict(one_hot_input)
57
 
58
+ # 3. معالجة مصفوفة الصورة (هنا حل مشكلة الخط الأسود)
59
  img_data = prediction[0]
60
+
61
+ # تحويل القيم لمجال الألوان الطبيعي (0-255)
62
  if img_data.max() <= 1.0:
63
  img_data = (img_data * 255).astype("uint8")
64
  else:
65
  img_data = img_data.astype("uint8")
66
 
67
+ # ضبط الأبعاد إذا كانت الصورة Grayscale (أبيض وأسود)
68
  if len(img_data.shape) == 3 and img_data.shape[-1] == 1:
69
  img_data = img_data.reshape(img_data.shape[0], img_data.shape[1])
70
 
71
+ # تحويل المصفوفة لصورة PIL وضبط نمط الألوان
72
  img = Image.fromarray(img_data)
73
+ if img.mode != 'L':
74
+ img = img.convert('L')
75
+
76
+ # عرض النتيجة
77
+ st.success(f"✅ تم العثور على إشارة: {word_input}")
78
+ st.image(img, caption=f"إشارة {word_input}", width=400)
79
  else:
80
+ st.error(f"❌ الكلمة '{word_input}' غير موجودة في القاموس.")
81
  else:
82
+ st.warning("⚠️ يرجى كتابة كلمة أولاً.")