ahmedda commited on
Commit
012e3a7
ยท
verified ยท
1 Parent(s): a556ebe

Upload 6 files

Browse files
.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