Spaces:
Sleeping
Sleeping
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) |