Spaces:
Sleeping
Sleeping
Update ui.py
Browse files
ui.py
CHANGED
|
@@ -1,34 +1,74 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
st.
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("من فضلك اكتب كلمة أولاً.")
|