Spaces:
Sleeping
Sleeping
Download app.py from ahmedda/Arabic-Sign-Language-API: direct link, hf CLI and curl.
- Browser
- Download file 3.65 kB
-
https://huggingface.co/spaces/ahmedda/Arabic-Sign-Language-API/resolve/main/app.py
- Command line
-
hf download hf://spaces/ahmedda/Arabic-Sign-Language-API/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ahmedda/Arabic-Sign-Language-API/resolve/main/app.py
3.65 kB
| 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}") | |
| 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) |