{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\nos.listdir(\"/kaggle/input/competitions/asl-signs\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:25.777248Z","iopub.execute_input":"2026-07-15T17:12:25.777601Z","iopub.status.idle":"2026-07-15T17:12:25.786785Z","shell.execute_reply.started":"2026-07-15T17:12:25.777572Z","shell.execute_reply":"2026-07-15T17:12:25.785753Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/input/competitions/asl-signs\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:25.788518Z","iopub.execute_input":"2026-07-15T17:12:25.78882Z","iopub.status.idle":"2026-07-15T17:12:25.802746Z","shell.execute_reply.started":"2026-07-15T17:12:25.788794Z","shell.execute_reply":"2026-07-15T17:12:25.801894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# train.csv loading\ntrain_df = pd.read_csv(\n    \"/kaggle/input/competitions/asl-signs/train.csv\"\n)\n\n# total unique classes (signs)\ntotal_classes = train_df[\"sign\"].nunique()\n\nprint(\"Total classes in dataset:\", total_classes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:36:55.408177Z","iopub.execute_input":"2026-07-15T17:36:55.4098Z","iopub.status.idle":"2026-07-15T17:36:55.63762Z","shell.execute_reply.started":"2026-07-15T17:36:55.409748Z","shell.execute_reply":"2026-07-15T17:36:55.636715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntrain = pd.read_csv(\n    \"/kaggle/input/competitions/asl-signs/train.csv\"\n)\n\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:26.459585Z","iopub.execute_input":"2026-07-15T17:12:26.459873Z","iopub.status.idle":"2026-07-15T17:12:26.638062Z","shell.execute_reply.started":"2026-07-15T17:12:26.459846Z","shell.execute_reply":"2026-07-15T17:12:26.636717Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"sign\"].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:26.63991Z","iopub.execute_input":"2026-07-15T17:12:26.640942Z","iopub.status.idle":"2026-07-15T17:12:26.65868Z","shell.execute_reply.started":"2026-07-15T17:12:26.6409Z","shell.execute_reply":"2026-07-15T17:12:26.657954Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **section 1**","metadata":{}},{"cell_type":"markdown","source":"# **step 1 — Import Libraries**","metadata":{}},{"cell_type":"code","source":"import re\nimport requests\nimport json","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:26.659591Z","iopub.execute_input":"2026-07-15T17:12:26.659916Z","iopub.status.idle":"2026-07-15T17:12:27.02379Z","shell.execute_reply.started":"2026-07-15T17:12:26.659882Z","shell.execute_reply":"2026-07-15T17:12:27.022619Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Step 2 — Configuration**","metadata":{}},{"cell_type":"code","source":"# =====================================\n# Mistral API Configuration\n# =====================================\n\nAPI_KEY = \"FGIx5cXm7c3WOMu0TVTIvJGWDZVrgggd\"\n\nBASE_URL = \"https://api.mistral.ai/v1\"\n\nHEADERS = {\n    \"Authorization\": f\"Bearer {API_KEY}\",\n    \"Content-Type\": \"application/json\"\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.025036Z","iopub.execute_input":"2026-07-15T17:12:27.025485Z","iopub.status.idle":"2026-07-15T17:12:27.031334Z","shell.execute_reply.started":"2026-07-15T17:12:27.025418Z","shell.execute_reply":"2026-07-15T17:12:27.030426Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Step 3 — Test API Connection**","metadata":{}},{"cell_type":"code","source":"response = requests.get(\n    f\"{BASE_URL}/models\",\n    headers=HEADERS\n)\n\nprint(\"Status Code:\", response.status_code)\n#print(response.text)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.032476Z","iopub.execute_input":"2026-07-15T17:12:27.033073Z","iopub.status.idle":"2026-07-15T17:12:27.52594Z","shell.execute_reply.started":"2026-07-15T17:12:27.033034Z","shell.execute_reply":"2026-07-15T17:12:27.524555Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Section 2 — Input Processing**\n# **Module 2.1 — Input Validation**","metadata":{}},{"cell_type":"code","source":"# ==========================================================\n# Input Validation\n# ==========================================================\n\nimport re\n\nMAX_INPUT_LENGTH = 500\n\n\ndef validate_input(text):\n    \"\"\"\n    Validate the user's input before sending it to the LLM.\n\n    Parameters\n    ----------\n    text : str\n        User input sentence.\n\n    Returns\n    -------\n    tuple\n        (True, \"\")\n            If the input is valid.\n\n        (False, error_message)\n            If the input is invalid.\n    \"\"\"\n\n    # Check if input is None\n    if text is None:\n        return False, \"Input cannot be None.\"\n\n    # Check input type\n    if not isinstance(text, str):\n        return False, \"Input must be a string.\"\n\n    # Remove leading and trailing spaces\n    text = text.strip()\n\n    # Check empty input\n    if len(text) == 0:\n        return False, \"Input cannot be empty.\"\n\n    # Check maximum length\n    if len(text) > MAX_INPUT_LENGTH:\n        return False, f\"Input exceeds the maximum limit of {MAX_INPUT_LENGTH} characters.\"\n\n    # Check whether at least one alphabet exists\n    if not re.search(r\"[A-Za-z]\", text):\n        return False, \"Input must contain at least one alphabetic character.\"\n\n    return True, \"\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.5272Z","iopub.execute_input":"2026-07-15T17:12:27.527966Z","iopub.status.idle":"2026-07-15T17:12:27.539171Z","shell.execute_reply.started":"2026-07-15T17:12:27.527922Z","shell.execute_reply":"2026-07-15T17:12:27.53809Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Testing Cell**","metadata":{}},{"cell_type":"code","source":"# ==========================================================\n# Test Cases\n# ==========================================================\n\ntest_cases = [\n    None,\n    \"\",\n    \"      \",\n    \"123456\",\n    \"@@@@@@@\",\n    \"Hello\",\n    \"I am going to school.\",\n    \"     I am hungry.      \",\n    \"A\" * 501\n]\n\nfor case in test_cases:\n\n    status, message = validate_input(case)\n\n    print(f\"Input: {repr(case)}\")\n    print(f\"Valid: {status}\")\n\n    if not status:\n        print(f\"Reason: {message}\")\n\n    print(\"-\" * 60)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.543Z","iopub.execute_input":"2026-07-15T17:12:27.543428Z","iopub.status.idle":"2026-07-15T17:12:27.566995Z","shell.execute_reply.started":"2026-07-15T17:12:27.543389Z","shell.execute_reply":"2026-07-15T17:12:27.566075Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Module 2.2 — Input Normalization**","metadata":{}},{"cell_type":"markdown","source":"**Objective**\n\n**Before sending text to the LLM, we want to clean it.**\n\n**Important Principle**\n\n**Normalization does NOT change the meaning of the sentence.**","metadata":{}},{"cell_type":"code","source":"# ==========================================================\n# Input Normalization - Helper Functions\n# ==========================================================\n\nimport re\n\n\ndef remove_extra_spaces(text):\n    \"\"\"\n    Remove leading, trailing, and multiple spaces.\n    \"\"\"\n\n    text = text.strip()\n    text = re.sub(r\"\\s+\", \" \", text)\n\n    return text\n\n\ndef expand_contractions(text):\n    \"\"\"\n    Expand common English contractions.\n    \"\"\"\n\n    contractions = {\n\n        \"I'm\": \"I am\",\n        \"I've\": \"I have\",\n        \"I'll\": \"I will\",\n        \"I'd\": \"I would\",\n\n        \"you're\": \"you are\",\n        \"you're\": \"you are\",\n        \"you've\": \"you have\",\n\n        \"he's\": \"he is\",\n        \"she's\": \"she is\",\n        \"it's\": \"it is\",\n\n        \"can't\": \"cannot\",\n        \"won't\": \"will not\",\n        \"don't\": \"do not\",\n        \"doesn't\": \"does not\",\n        \"didn't\": \"did not\",\n\n        \"isn't\": \"is not\",\n        \"aren't\": \"are not\",\n        \"wasn't\": \"was not\",\n        \"weren't\": \"were not\",\n\n        \"hasn't\": \"has not\",\n        \"haven't\": \"have not\",\n        \"hadn't\": \"had not\",\n\n        \"shouldn't\": \"should not\",\n        \"wouldn't\": \"would not\",\n        \"couldn't\": \"could not\",\n\n        \"that's\": \"that is\",\n        \"there's\": \"there is\",\n        \"what's\": \"what is\",\n        \"who's\": \"who is\",\n        \"where's\": \"where is\"\n    }\n\n    for contraction, expanded in contractions.items():\n        text = re.sub(\n            rf\"\\b{re.escape(contraction)}\\b\",\n            expanded,\n            text,\n            flags=re.IGNORECASE\n        )\n\n    return text\n\n\ndef remove_punctuation(text):\n    \"\"\"\n    Remove punctuation while preserving letters,\n    numbers and spaces.\n    \"\"\"\n\n    text = re.sub(r\"[^\\w\\s]\", \"\", text)\n\n    return text","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.568341Z","iopub.execute_input":"2026-07-15T17:12:27.568685Z","iopub.status.idle":"2026-07-15T17:12:27.587646Z","shell.execute_reply.started":"2026-07-15T17:12:27.56865Z","shell.execute_reply":"2026-07-15T17:12:27.586647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# Input Normalization\n# ==========================================================\n\ndef normalize_input(text):\n    \"\"\"\n    Normalize user input before prompt generation.\n\n    Steps:\n    1. Remove extra spaces\n    2. Expand contractions\n    3. Remove punctuation\n    \"\"\"\n\n    text = remove_extra_spaces(text)\n\n    text = expand_contractions(text)\n\n    text = remove_punctuation(text)\n\n    return text","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.589049Z","iopub.execute_input":"2026-07-15T17:12:27.589543Z","iopub.status.idle":"2026-07-15T17:12:27.615497Z","shell.execute_reply.started":"2026-07-15T17:12:27.589505Z","shell.execute_reply":"2026-07-15T17:12:27.614337Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Testing**","metadata":{}},{"cell_type":"code","source":"# ==========================================================\n# Test Cases\n# ==========================================================\n\ntest_cases = [\n\n    \"   Hello     World   \",\n\n    \"I'm going to school.\",\n\n    \"I can't come today.\",\n\n    \"Don't worry!!!\",\n\n    \"Hello,,,,,,, friend!!!\",\n\n    \"It's raining today.\",\n\n    \"Where's my book?\",\n\n    \"I've finished my homework.\",\n\n    \"He isn't here.\",\n\n    \"     I     am      hungry!!!     \"\n]\n\nfor sentence in test_cases:\n\n    print(\"=\" * 60)\n\n    print(\"Original   :\", sentence)\n\n    normalized = normalize_input(sentence)\n\n    print(\"Normalized :\", normalized)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.616851Z","iopub.execute_input":"2026-07-15T17:12:27.617245Z","iopub.status.idle":"2026-07-15T17:12:27.648728Z","shell.execute_reply.started":"2026-07-15T17:12:27.617206Z","shell.execute_reply":"2026-07-15T17:12:27.647703Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Section 3 — Prompt Engineering**\nSection 3\n│\n├── System Prompt\n├── Prompt Generator\n├── Message Generator\n└── Prompt Testing","metadata":{}},{"cell_type":"code","source":"# ==========================================================\n# Prompt Engineering - System Prompt\n# ==========================================================\n\nSYSTEM_PROMPT = \"\"\"\nYou are an expert American Sign Language (ASL) linguist.\n\nYour task is to convert English sentences into standard ASL Gloss.\n\nFollow these rules carefully:\n\n1. Preserve the original meaning.\n2. Follow standard ASL Gloss grammar.\n3. Remove unnecessary articles (a, an, the) where appropriate.\n4. Remove unnecessary helping verbs where appropriate.\n5. Use ONLY UPPERCASE words.\n6. Do NOT add punctuation.\n7. Do NOT explain your answer.\n8. Do NOT translate into natural English.\n9. Return ONLY the ASL Gloss.\n10. Assume the input is English only.\n11. Preserve important politeness markers such as PLEASE, THANK YOU, and SORRY whenever they appear in the input.\n\nExamples:\n\nEnglish:\nI am hungry.\n\nASL Gloss:\nI HUNGRY\n\n----------------------------------------\n\nEnglish:\nShe is reading a book.\n\nASL Gloss:\nSHE READ BOOK\n\n----------------------------------------\n\nEnglish:\nI will go to school tomorrow.\n\nASL Gloss:\nTOMORROW I GO SCHOOL\n\n----------------------------------------\n\nEnglish:\nWhere is my book?\n\nASL Gloss:\nMY BOOK WHERE\n\"\"\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:32:48.246802Z","iopub.execute_input":"2026-07-15T17:32:48.247296Z","iopub.status.idle":"2026-07-15T17:32:48.253291Z","shell.execute_reply.started":"2026-07-15T17:32:48.24723Z","shell.execute_reply":"2026-07-15T17:32:48.252214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# Prompt Generator\n# ==========================================================\n\ndef create_prompt(user_input):\n    \"\"\"\n    Create the user prompt for ASL Gloss generation.\n    \"\"\"\n\n    prompt = f\"\"\"\nConvert the following English sentence into ASL Gloss.\n\nEnglish:\n{user_input}\n\nASL Gloss:\n\"\"\"\n\n    return prompt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.668618Z","iopub.execute_input":"2026-07-15T17:12:27.668948Z","iopub.status.idle":"2026-07-15T17:12:27.693468Z","shell.execute_reply.started":"2026-07-15T17:12:27.668912Z","shell.execute_reply":"2026-07-15T17:12:27.692497Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# Message Generator\n# ==========================================================\n\ndef generate_messages(user_input):\n    \"\"\"\n    Generate messages for the Mistral Chat API.\n    \"\"\"\n\n    messages = [\n\n        {\n            \"role\": \"system\",\n            \"content\": SYSTEM_PROMPT\n        },\n\n        {\n            \"role\": \"user\",\n            \"content\": create_prompt(user_input)\n        }\n\n    ]\n\n    return messages","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.694641Z","iopub.execute_input":"2026-07-15T17:12:27.6951Z","iopub.status.idle":"2026-07-15T17:12:27.712471Z","shell.execute_reply.started":"2026-07-15T17:12:27.695064Z","shell.execute_reply":"2026-07-15T17:12:27.711519Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# Prompt Testing\n# ==========================================================\n\nsentence = \"I am going to school tomorrow.\"\n\nmessages = generate_messages(sentence)\n\nfor message in messages:\n\n    print(\"=\" * 60)\n    print(\"ROLE :\", message[\"role\"].upper())\n    print(\"=\" * 60)\n    print(message[\"content\"])\n    print()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.71361Z","iopub.execute_input":"2026-07-15T17:12:27.713954Z","iopub.status.idle":"2026-07-15T17:12:27.736527Z","shell.execute_reply.started":"2026-07-15T17:12:27.713918Z","shell.execute_reply":"2026-07-15T17:12:27.735353Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Section 4 — Mistral API Integration**","metadata":{}},{"cell_type":"code","source":"API_KEY = \"FGIx5cXm7c3WOMu0TVTIvJGWDZVrgggd\"\n\nMODEL_NAME = \"open-mistral-nemo\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.73767Z","iopub.execute_input":"2026-07-15T17:12:27.738031Z","iopub.status.idle":"2026-07-15T17:12:27.764362Z","shell.execute_reply.started":"2026-07-15T17:12:27.737966Z","shell.execute_reply":"2026-07-15T17:12:27.763348Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# Mistral API\n# ==========================================================\n\nimport requests\n\n\nAPI_URL = \"https://api.mistral.ai/v1/chat/completions\"\n\n\ndef call_mistral_api(messages):\n    \"\"\"\n    Send messages to the Mistral Chat API.\n    \"\"\"\n\n    headers = {\n        \"Authorization\": f\"Bearer {API_KEY}\",\n        \"Content-Type\": \"application/json\"\n    }\n\n    payload = {\n        \"model\": MODEL_NAME,\n        \"messages\": messages,\n        \"temperature\": 0.2\n    }\n\n    response = requests.post(\n        API_URL,\n        headers=headers,\n        json=payload\n    )\n\n    if response.status_code != 200:\n        raise Exception(f\"API Error {response.status_code}: {response.text}\")\n\n    result = response.json()\n\n    return result[\"choices\"][0][\"message\"][\"content\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.765533Z","iopub.execute_input":"2026-07-15T17:12:27.765893Z","iopub.status.idle":"2026-07-15T17:12:27.784298Z","shell.execute_reply.started":"2026-07-15T17:12:27.765858Z","shell.execute_reply":"2026-07-15T17:12:27.783383Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **testing**","metadata":{}},{"cell_type":"code","source":"sentence = \"I am going to school tomorrow.\"\n\nmessages = generate_messages(sentence)\n\nresponse = call_mistral_api(messages)\n\nprint(response)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:27.785754Z","iopub.execute_input":"2026-07-15T17:12:27.78611Z","iopub.status.idle":"2026-07-15T17:12:28.398887Z","shell.execute_reply.started":"2026-07-15T17:12:27.786072Z","shell.execute_reply":"2026-07-15T17:12:28.397996Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Section 5 - Output Validation**\n**Objective**\n\nClean and validate the ASL Gloss returned by the Mistral LLM before displaying it to the user.","metadata":{}},{"cell_type":"code","source":"# ==========================================================\n# Output Validation - Helper Functions\n# ==========================================================\n\nimport re\n\n\ndef remove_labels(text):\n    \"\"\"\n    Remove labels such as 'ASL Gloss:' from the response.\n    \"\"\"\n\n    text = re.sub(r\"ASL\\s*Gloss\\s*:\\s*\", \"\", text, flags=re.IGNORECASE)\n\n    return text\n\n\ndef remove_quotes(text):\n    \"\"\"\n    Remove single and double quotation marks.\n    \"\"\"\n\n    return text.replace('\"', \"\").replace(\"'\", \"\")\n\n\ndef remove_extra_spaces(text):\n    \"\"\"\n    Remove leading, trailing, and multiple spaces.\n    \"\"\"\n\n    text = text.strip()\n\n    text = re.sub(r\"\\s+\", \" \", text)\n\n    return text\n\n\ndef remove_trailing_punctuation(text):\n    \"\"\"\n    Remove punctuation from the output.\n    \"\"\"\n\n    text = re.sub(r\"[.,!?;:]\", \"\", text)\n\n    return text","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:28.399988Z","iopub.execute_input":"2026-07-15T17:12:28.400347Z","iopub.status.idle":"2026-07-15T17:12:28.406599Z","shell.execute_reply.started":"2026-07-15T17:12:28.400311Z","shell.execute_reply":"2026-07-15T17:12:28.405463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# Output Cleaning\n# ==========================================================\n\ndef clean_llm_output(response):\n    \"\"\"\n    Clean the raw response returned by the LLM.\n    \"\"\"\n\n    response = remove_labels(response)\n\n    response = remove_quotes(response)\n\n    response = remove_trailing_punctuation(response)\n\n    response = remove_extra_spaces(response)\n\n    response = response.upper()\n\n    return response","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:28.407832Z","iopub.execute_input":"2026-07-15T17:12:28.408865Z","iopub.status.idle":"2026-07-15T17:12:28.435345Z","shell.execute_reply.started":"2026-07-15T17:12:28.408825Z","shell.execute_reply":"2026-07-15T17:12:28.434339Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# Output Validation\n# ==========================================================\n\ndef validate_gloss(gloss):\n    \"\"\"\n    Validate the generated ASL Gloss.\n    \"\"\"\n\n    if gloss is None:\n        return False, \"Empty response from LLM.\"\n\n    if len(gloss.strip()) == 0:\n        return False, \"Generated gloss is empty.\"\n\n    if len(gloss) > 500:\n        return False, \"Generated gloss is unusually long.\"\n\n    return True, \"\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:28.436505Z","iopub.execute_input":"2026-07-15T17:12:28.437096Z","iopub.status.idle":"2026-07-15T17:12:28.458228Z","shell.execute_reply.started":"2026-07-15T17:12:28.437067Z","shell.execute_reply":"2026-07-15T17:12:28.457238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# Test Cases\n# ==========================================================\n\nresponses = [\n\n    'ASL Gloss: TOMORROW I GO SCHOOL',\n\n    '\"TOMORROW I GO SCHOOL\"',\n\n    \"TOMORROW     I      GO      SCHOOL\",\n\n    \"Tomorrow I Go School.\",\n\n    \"   TOMORROW I GO SCHOOL!!!   \",\n\n    \"ASL Gloss: 'Tomorrow I Go School.'\"\n\n]\n\nfor response in responses:\n\n    print(\"=\" * 60)\n\n    print(\"Original : \", response)\n\n    cleaned = clean_llm_output(response)\n\n    print(\"Cleaned  : \", cleaned)\n\n    valid, message = validate_gloss(cleaned)\n\n    print(\"Valid    :\", valid)\n\n    if not valid:\n        print(\"Reason   :\", message)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:28.459398Z","iopub.execute_input":"2026-07-15T17:12:28.45967Z","iopub.status.idle":"2026-07-15T17:12:28.47878Z","shell.execute_reply.started":"2026-07-15T17:12:28.459647Z","shell.execute_reply":"2026-07-15T17:12:28.477761Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Section 6 — Pipeline Integration**","metadata":{}},{"cell_type":"code","source":"# ==========================================================\n# English to ASL Gloss Pipeline\n# ==========================================================\n\ndef english_to_asl(user_input, debug=False):\n\n    # ----------------------------------\n    # Step 1 : Validate Input\n    # ----------------------------------\n\n    is_valid, message = validate_input(user_input)\n\n    if not is_valid:\n        raise ValueError(message)\n\n    if debug:\n        print(\"=\" * 60)\n        print(\"Original Input\")\n        print(\"=\" * 60)\n        print(user_input)\n\n    # ----------------------------------\n    # Step 2 : Normalize Input\n    # ----------------------------------\n\n    normalized_text = normalize_input(user_input)\n\n    if debug:\n        print(\"\\n\" + \"=\" * 60)\n        print(\"Normalized Input\")\n        print(\"=\" * 60)\n        print(normalized_text)\n\n    # ----------------------------------\n    # Step 3 : Generate Prompt\n    # ----------------------------------\n\n    messages = generate_messages(normalized_text)\n\n    if debug:\n        print(\"\\n\" + \"=\" * 60)\n        print(\"Generated Prompt\")\n        print(\"=\" * 60)\n        print(messages[1][\"content\"])\n\n    # ----------------------------------\n    # Step 4 : Call Mistral API\n    # ----------------------------------\n\n    raw_response = call_mistral_api(messages)\n\n    if debug:\n        print(\"\\n\" + \"=\" * 60)\n        print(\"Raw LLM Response\")\n        print(\"=\" * 60)\n        print(raw_response)\n\n    # ----------------------------------\n    # Step 5 : Clean Output\n    # ----------------------------------\n\n    gloss = clean_llm_output(raw_response)\n\n    if debug:\n        print(\"\\n\" + \"=\" * 60)\n        print(\"Cleaned Output\")\n        print(\"=\" * 60)\n        print(gloss)\n\n    # ----------------------------------\n    # Step 6 : Validate Output\n    # ----------------------------------\n\n    is_valid_gloss, message = validate_gloss(gloss)\n\n    if not is_valid_gloss:\n        raise ValueError(message)\n\n    if debug:\n        print(\"\\n\" + \"=\" * 60)\n        print(\"Pipeline Status\")\n        print(\"=\" * 60)\n        print(\"Translation Successful ✅\")\n\n    return gloss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:28.480046Z","iopub.execute_input":"2026-07-15T17:12:28.48037Z","iopub.status.idle":"2026-07-15T17:12:28.503143Z","shell.execute_reply.started":"2026-07-15T17:12:28.480344Z","shell.execute_reply":"2026-07-15T17:12:28.502225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sentence = \"TOday    is  veryyy  beautifull* day....!!@@\"\n\nresult = english_to_asl(sentence, debug=True)\n\nprint(\"\\n\" + \"=\" * 60)\nprint(\"Final ASL Gloss\")\nprint(\"=\" * 60)\nprint(result)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:28.504364Z","iopub.execute_input":"2026-07-15T17:12:28.504729Z","iopub.status.idle":"2026-07-15T17:12:29.107991Z","shell.execute_reply.started":"2026-07-15T17:12:28.504694Z","shell.execute_reply":"2026-07-15T17:12:29.10698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# End-to-End Test\n# ==========================================================\n\nsentence = \"TOday    is  veryyy  beautifull* day....!!@@\"\n\ngloss = english_to_asl(sentence)\n\nprint(\"=\" * 60)\nprint(\"English Sentence\")\nprint(\"=\" * 60)\nprint(sentence)\n\nprint()\n\nprint(\"=\" * 60)\nprint(\"Generated ASL Gloss\")\nprint(\"=\" * 60)\nprint(gloss)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:12:29.109772Z","iopub.execute_input":"2026-07-15T17:12:29.110134Z","iopub.status.idle":"2026-07-15T17:12:30.07238Z","shell.execute_reply.started":"2026-07-15T17:12:29.110098Z","shell.execute_reply":"2026-07-15T17:12:30.071617Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Section 7 – End-to-End Evaluation**\n**Objective**\nEvaluate the English → ASL Gloss translation pipeline using multiple English sentences and verify the quality of the generated ASL Gloss.","metadata":{}},{"cell_type":"code","source":"messy_sentences = [\n\n    # Spelling mistakes\n    \"TOday    is  veryyy  beautifull* day....!!@@\",\n\n    # Missing punctuation\n    \"where is my phone i cant find it\",\n\n    # Mixed upper/lower case\n    \"MY BroThEr Is ReAdInG a BoOk\",\n\n    # Extra spaces\n    \"     I      am      going      to      school     tomorrow      \",\n\n    # Short forms\n    \"I'm gonna visit my grandma tomorrow.\",\n\n    # Grammar mistakes\n    \"She don't like apples.\",\n\n    # Wrong tense\n    \"Yesterday I go to market.\",\n\n    # Random symbols\n    \"Please!!! open@@ the ### door $$$\",\n\n    # Repeated words\n    \"I am am am very very hungry.\",\n\n    # Long sentence\n    \"Although it was raining heavily, we still decided to go to the market because we needed to buy food.\",\n\n    # Multiple clauses\n    \"If you finish your homework, then we will go to the park together.\",\n\n    # Casual English\n    \"Hey bro can you help me with this homework?\",\n\n    # Question\n    \"Why didn't you call me yesterday?\",\n\n    # Negation\n    \"I don't want to eat pizza today.\",\n\n    # Time\n    \"Tomorrow morning I have to wake up at six o'clock.\",\n\n    # Location\n    \"The little cat is sleeping under the old wooden table.\",\n\n    # Family\n    \"My younger sister is watching television in the living room.\",\n\n    # Mixed punctuation\n    \"Can,,, you??? please!!! help me....\",\n\n    # Multiple actions\n    \"He washed his hands, ate breakfast, and went to school.\",\n\n    # Complex sentence\n    \"After I finish my work, I will visit my grandparents if the weather is good.\",\n\n    \"My brother, who lives in Islamabad, called me yesterday because my mother was sick.\",\n\n    \"Even though I was tired after working all day, I still helped my little brother finish his homework before dinner.\",\n    #mixed language\n    \"Bro kal I will come late because traffic bohot tha.\",\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:39:08.326906Z","iopub.execute_input":"2026-07-15T17:39:08.327431Z","iopub.status.idle":"2026-07-15T17:39:08.336328Z","shell.execute_reply.started":"2026-07-15T17:39:08.327364Z","shell.execute_reply":"2026-07-15T17:39:08.334762Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = []\n\nfor sentence in messy_sentences:\n\n    try:\n\n        gloss = english_to_asl(sentence)\n\n        results.append({\n            \"English\": sentence,\n            \"ASL Gloss\": gloss,\n            \"Status\": \"PASS\"\n        })\n\n    except Exception as e:\n\n        results.append({\n            \"English\": sentence,\n            \"ASL Gloss\": str(e),\n            \"Status\": \"FAIL\"\n        })\n\nimport pandas as pd\n\npd.DataFrame(results)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:33:23.712469Z","iopub.execute_input":"2026-07-15T17:33:23.713045Z","iopub.status.idle":"2026-07-15T17:33:40.040453Z","shell.execute_reply.started":"2026-07-15T17:33:23.712975Z","shell.execute_reply":"2026-07-15T17:33:40.039422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ==========================================================\n# Evaluation Statistics\n# ==========================================================\n\ntotal = len(results_df)\n\npassed = (results_df[\"Status\"] == \"PASS\").sum()\n\nfailed = (results_df[\"Status\"] == \"FAIL\").sum()\n\nprint(\"=\"*50)\nprint(f\"Total Test Cases : {total}\")\nprint(f\"Passed           : {passed}\")\nprint(f\"Failed           : {failed}\")\nprint(f\"Success Rate     : {(passed/total)*100:.2f}%\")\nprint(\"=\"*50)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-15T17:34:13.751094Z","iopub.execute_input":"2026-07-15T17:34:13.75234Z","iopub.status.idle":"2026-07-15T17:34:13.76097Z","shell.execute_reply.started":"2026-07-15T17:34:13.752295Z","shell.execute_reply":"2026-07-15T17:34:13.759325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}