File size: 3,649 Bytes
012e3a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
import tensorflow as tf
from PIL import Image
import io, base64, numpy as np
import pandas as pd
import re

app = FastAPI(title="Sign โ†” Text API")

# ุฏุงู„ุฉ ุชู†ุธูŠู ูˆุชูˆุญูŠุฏ ุงู„ู†ุต ุงู„ุนุฑุจูŠ
def normalize_arabic(text):
    text = str(text).strip()
    text = re.sub("[ุฅุฃุข]", "ุง", text)
    text = re.sub("ุฉ", "ู‡", text)
    return text

# 1. ุชุญู…ูŠู„ ู…ู„ู ุงู„ุฅูƒุณู„ ูˆุนู…ู„ ุงู„ู‚ุงู…ูˆุณ
word_to_id = {}
try:
    file_name = "KARSL-100_Labels (1).xlsx"
    df = pd.read_excel(file_name)
    df.columns = [str(col).strip() for col in df.columns]
    
    if 'Sign-Arabic' in df.columns and 'SignID' in df.columns:
        for index, row in df.iterrows():
            if pd.notna(row['Sign-Arabic']):
                key = normalize_arabic(row['Sign-Arabic'])
                word_to_id[key] = row['SignID']
        print(f"โœ… ุชู… ุชุญู…ูŠู„ {len(word_to_id)} ูƒู„ู…ุฉ ุจู†ุฌุงุญ")
except Exception as e:
    print(f"โŒ ุฎุทุฃ ููŠ ุงู„ุฅูƒุณู„: {e}")

# 2. ุชุญู…ูŠู„ ุงู„ู…ูˆุฏูŠู„ุงุช
try:
    sign_to_text_model = tf.keras.models.load_model("best_model.h5", compile=False)
    text_to_sign_model = tf.keras.models.load_model("word_to_sign_model.h5", compile=False)
    print("โœ… ุชู… ุชุญู…ูŠู„ ุงู„ู…ูˆุฏูŠู„ุงุช ุจู†ุฌุงุญ")
except Exception as e:
    print(f"โŒ ุฎุทุฃ ููŠ ุงู„ู…ูˆุฏูŠู„ุงุช: {e}")

@app.post("/text-to-sign")
async def text_to_sign(text: str = Form(...)):
    try:
        clean_text = normalize_arabic(text)
        label_id = word_to_id.get(clean_text)
        
        if label_id is None:
            raise ValueError(f"ุงู„ูƒู„ู…ุฉ '{text}' ู…ุด ู…ูˆุฌูˆุฏุฉ ููŠ ุงู„ุฅูƒุณู„.")

        # --- ุงู„ุญู„ ุงู„ุฌุฐุฑูŠ ู„ู…ุดูƒู„ุฉ ุงู„ู€ Shape (100) ---
        # ุจู†ุนู…ู„ ู…ุตููˆูุฉ ููŠู‡ุง 100 ุตูุฑ
        one_hot_input = np.zeros((1, 100), dtype=np.float32)
        
        # ุจู†ู†ูˆุฑ ุงู„ุฑู‚ู… ุจุชุงุน ุงู„ูƒู„ู…ุฉ (ู…ุซู„ุงู‹ 72) ุจู†ุฎู„ูŠ ู‚ูŠู…ุชู‡ 1
        # ู…ู„ุญูˆุธุฉ: ู„ูˆ ุงู„ู€ IDs ููŠ ุงู„ุฅูƒุณู„ ุจุชุจุฏุฃ ู…ู† 1ุŒ ุจู†ุทุฑุญ 1 ุนุดุงู† ุงู„ู€ index ุจูŠุจุฏุฃ ู…ู† 0
        idx = int(label_id) - 1 if int(label_id) > 0 else 0
        
        if idx < 100:
            one_hot_input[0, idx] = 1.0
        else:
            raise ValueError(f"ุงู„ู€ ID ุฑู‚ู… {label_id} ุฃูƒุจุฑ ู…ู† ุญุฌู… ุงู„ู…ูˆุฏูŠู„ (100)")

        # ุงู„ุชูˆู‚ุน (ุฏู„ูˆู‚ุชูŠ ุงู„ู€ Shape ุจู‚ุง 100 ูˆู…ุธุจูˆุท)
        prediction = text_to_sign_model.predict(one_hot_input)
        # ---------------------------------------

        if isinstance(prediction, np.ndarray):
            img_data = prediction[0]
            if img_data.max() <= 1.0: img_data = (img_data * 255).astype("uint8")
            else: img_data = img_data.astype("uint8")
            
            if len(img_data.shape) == 3 and img_data.shape[-1] == 1:
                img_data = img_data.reshape(img_data.shape[0], img_data.shape[1])
            img = Image.fromarray(img_data)
        else:
            img = prediction

        buf = io.BytesIO()
        img.save(buf, format="PNG")
        encoded = base64.b64encode(buf.getvalue()).decode("utf-8")

        return {
            "status": "success",
            "word": clean_text,
            "sign_id": int(label_id),
            "image_base64": encoded
        }

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"ุฎุทุฃ ููŠ ุงู„ู…ูˆุฏูŠู„: {str(e)}")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)