Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- .Dockerfile +15 -0
- KARSL-100_Labels (1).xlsx +0 -0
- app.py +94 -0
- best_model.h5 +3 -0
- requirements.txt +0 -0
- word_to_sign_model.h5 +3 -0
.Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ุงุฎุชูุงุฑ ูุณุฎุฉ ุจุงูุซูู ุฎูููุฉ ูุณุฑูุนุฉ
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# ุชุญุฏูุฏ ู
ูุงู ุงูุดุบู ุฌูู ุงูุณูุฑูุฑ
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# ูุณุฎ ู
ูู ุงูู
ูุชุจุงุช ูุชุณุทูุจูุง
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# ูุณุฎ ูู ู
ููุงุช ุงูู
ุดุฑูุน (ุงูููุฏุ ุงูู
ูุฏููุงุชุ ุงูุฅูุณู)
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# ุชุดุบูู ุงูุณูุฑูุฑ ุนูู ุจูุฑุช 7860 (ุงูุฎุงุต ุจูุงุฌููุฌ ููุณ)
|
| 15 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
KARSL-100_Labels (1).xlsx
ADDED
|
Binary file (12.8 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 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 |
+
app = FastAPI(title="Sign โ Text API")
|
| 9 |
+
|
| 10 |
+
# ุฏุงูุฉ ุชูุธูู ูุชูุญูุฏ ุงููุต ุงูุนุฑุจู
|
| 11 |
+
def normalize_arabic(text):
|
| 12 |
+
text = str(text).strip()
|
| 13 |
+
text = re.sub("[ุฅุฃุข]", "ุง", text)
|
| 14 |
+
text = re.sub("ุฉ", "ู", text)
|
| 15 |
+
return text
|
| 16 |
+
|
| 17 |
+
# 1. ุชุญู
ูู ู
ูู ุงูุฅูุณู ูุนู
ู ุงููุงู
ูุณ
|
| 18 |
+
word_to_id = {}
|
| 19 |
+
try:
|
| 20 |
+
file_name = "KARSL-100_Labels (1).xlsx"
|
| 21 |
+
df = pd.read_excel(file_name)
|
| 22 |
+
df.columns = [str(col).strip() for col in df.columns]
|
| 23 |
+
|
| 24 |
+
if 'Sign-Arabic' in df.columns and 'SignID' in df.columns:
|
| 25 |
+
for index, row in df.iterrows():
|
| 26 |
+
if pd.notna(row['Sign-Arabic']):
|
| 27 |
+
key = normalize_arabic(row['Sign-Arabic'])
|
| 28 |
+
word_to_id[key] = row['SignID']
|
| 29 |
+
print(f"โ
ุชู
ุชุญู
ูู {len(word_to_id)} ููู
ุฉ ุจูุฌุงุญ")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"โ ุฎุทุฃ ูู ุงูุฅูุณู: {e}")
|
| 32 |
+
|
| 33 |
+
# 2. ุชุญู
ูู ุงูู
ูุฏููุงุช
|
| 34 |
+
try:
|
| 35 |
+
sign_to_text_model = tf.keras.models.load_model("best_model.h5", compile=False)
|
| 36 |
+
text_to_sign_model = tf.keras.models.load_model("word_to_sign_model.h5", compile=False)
|
| 37 |
+
print("โ
ุชู
ุชุญู
ูู ุงูู
ูุฏููุงุช ุจูุฌุงุญ")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"โ ุฎุทุฃ ูู ุงูู
ูุฏููุงุช: {e}")
|
| 40 |
+
|
| 41 |
+
@app.post("/text-to-sign")
|
| 42 |
+
async def text_to_sign(text: str = Form(...)):
|
| 43 |
+
try:
|
| 44 |
+
clean_text = normalize_arabic(text)
|
| 45 |
+
label_id = word_to_id.get(clean_text)
|
| 46 |
+
|
| 47 |
+
if label_id is None:
|
| 48 |
+
raise ValueError(f"ุงูููู
ุฉ '{text}' ู
ุด ู
ูุฌูุฏุฉ ูู ุงูุฅูุณู.")
|
| 49 |
+
|
| 50 |
+
# --- ุงูุญู ุงูุฌุฐุฑู ูู
ุดููุฉ ุงูู Shape (100) ---
|
| 51 |
+
# ุจูุนู
ู ู
ุตูููุฉ ูููุง 100 ุตูุฑ
|
| 52 |
+
one_hot_input = np.zeros((1, 100), dtype=np.float32)
|
| 53 |
+
|
| 54 |
+
# ุจูููุฑ ุงูุฑูู
ุจุชุงุน ุงูููู
ุฉ (ู
ุซูุงู 72) ุจูุฎูู ููู
ุชู 1
|
| 55 |
+
# ู
ูุญูุธุฉ: ูู ุงูู IDs ูู ุงูุฅูุณู ุจุชุจุฏุฃ ู
ู 1ุ ุจูุทุฑุญ 1 ุนุดุงู ุงูู index ุจูุจุฏุฃ ู
ู 0
|
| 56 |
+
idx = int(label_id) - 1 if int(label_id) > 0 else 0
|
| 57 |
+
|
| 58 |
+
if idx < 100:
|
| 59 |
+
one_hot_input[0, idx] = 1.0
|
| 60 |
+
else:
|
| 61 |
+
raise ValueError(f"ุงูู ID ุฑูู
{label_id} ุฃูุจุฑ ู
ู ุญุฌู
ุงูู
ูุฏูู (100)")
|
| 62 |
+
|
| 63 |
+
# ุงูุชููุน (ุฏูููุชู ุงูู Shape ุจูุง 100 ูู
ุธุจูุท)
|
| 64 |
+
prediction = text_to_sign_model.predict(one_hot_input)
|
| 65 |
+
# ---------------------------------------
|
| 66 |
+
|
| 67 |
+
if isinstance(prediction, np.ndarray):
|
| 68 |
+
img_data = prediction[0]
|
| 69 |
+
if img_data.max() <= 1.0: img_data = (img_data * 255).astype("uint8")
|
| 70 |
+
else: img_data = img_data.astype("uint8")
|
| 71 |
+
|
| 72 |
+
if len(img_data.shape) == 3 and img_data.shape[-1] == 1:
|
| 73 |
+
img_data = img_data.reshape(img_data.shape[0], img_data.shape[1])
|
| 74 |
+
img = Image.fromarray(img_data)
|
| 75 |
+
else:
|
| 76 |
+
img = prediction
|
| 77 |
+
|
| 78 |
+
buf = io.BytesIO()
|
| 79 |
+
img.save(buf, format="PNG")
|
| 80 |
+
encoded = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 81 |
+
|
| 82 |
+
return {
|
| 83 |
+
"status": "success",
|
| 84 |
+
"word": clean_text,
|
| 85 |
+
"sign_id": int(label_id),
|
| 86 |
+
"image_base64": encoded
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
raise HTTPException(status_code=500, detail=f"ุฎุทุฃ ูู ุงูู
ูุฏูู: {str(e)}")
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
import uvicorn
|
| 94 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
best_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:37df2bcf2fba8300f0edf628da9125ed0e1e83f98740aa5f92b89b921014c20f
|
| 3 |
+
size 15994536
|
requirements.txt
ADDED
|
Binary file (256 Bytes). View file
|
|
|
word_to_sign_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3717d4142892171405a99ee7a4fe212cc4dab02091153f7dea6332b67f5835e6
|
| 3 |
+
size 9877464
|