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

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +74 -34
ui.py CHANGED
@@ -1,34 +1,74 @@
1
- import streamlit as st
2
- import requests
3
- import base64
4
- from io import BytesIO
5
- from PIL import Image
6
-
7
- st.set_page_config(page_title="مترجم لغة الإشارة", layout="centered")
8
-
9
- st.title("📂 مترجم النص إلى لغة إشارة")
10
- st.write("اكتب الكلمة باللغة العربية لتحويلها إلى صورة إشارة")
11
-
12
- word = st.text_input("أدخل الكلمة:", placeholder="مثلاً: جمجمة")
13
-
14
- if st.button("تحويل"):
15
- if word:
16
- # رابط الـ API بتاعك (تأكد إنه نفس الرابط اللي شغال)
17
- api_url = "https://ahmedda-arabic-sign-language-api.hf.space" # لو على نفس السيرفر بنستخدم localhost
18
-
19
- try:
20
- response = requests.post(api_url, data={'text': word})
21
- data = response.json()
22
-
23
- if data['status'] == "success":
24
- st.success(f"الكلمة: {data['word']}")
25
- # فك تشفير الصورة وعرضها
26
- img_bytes = base64.b64decode(data['image_base64'])
27
- img = Image.open(BytesIO(img_bytes))
28
- st.image(img, caption=f"إشارة كلمة {word}", use_column_width=True)
29
- else:
30
- st.error("الكلمة غير موجودة في القاموس")
31
- except Exception as e:
32
- st.error(f"حدث خطأ في الاتصال بالسيرفر: {e}")
33
- else:
34
- st.warning("يرجى إدخال كلمة أولاً")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ from PIL import Image
4
+ 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("تحويل"):
41
+ if word_input:
42
+ clean_text = normalize_arabic(word_input)
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("من فضلك اكتب كلمة أولاً.")