<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mabdullahali on Medium]]></title>
        <description><![CDATA[Stories by Mabdullahali on Medium]]></description>
        <link>https://medium.com/@mabdullahali8642?source=rss-22a6f1700e53------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*898mOa2Cr8PlDXMC</url>
            <title>Stories by Mabdullahali on Medium</title>
            <link>https://medium.com/@mabdullahali8642?source=rss-22a6f1700e53------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 13:27:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mabdullahali8642/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Learning to Summarize from Human Feedback: Implementing SFT, Reward Modeling, PPO, and DPO from…]]></title>
            <link>https://medium.com/@mabdullahali8642/learning-to-summarize-from-human-feedback-implementing-sft-reward-modeling-ppo-and-dpo-from-80d4a4b2d821?source=rss-22a6f1700e53------2</link>
            <guid isPermaLink="false">https://medium.com/p/80d4a4b2d821</guid>
            <dc:creator><![CDATA[Mabdullahali]]></dc:creator>
            <pubDate>Fri, 13 Nov 2026 11:08:12 GMT</pubDate>
            <atom:updated>2026-11-13T11:08:12.199Z</atom:updated>
            <content:encoded><![CDATA[<h3>Learning to Summarize from Human Feedback: Implementing SFT, Reward Modeling, PPO, and DPO from Scratch</h3><p>will discuss later</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80d4a4b2d821" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Text to SQL Generator From Transformers]]></title>
            <link>https://medium.com/@mabdullahali8642/text-to-sql-generator-from-transformers-8999ebbc1512?source=rss-22a6f1700e53------2</link>
            <guid isPermaLink="false">https://medium.com/p/8999ebbc1512</guid>
            <dc:creator><![CDATA[Mabdullahali]]></dc:creator>
            <pubDate>Mon, 05 Oct 2026 11:38:44 GMT</pubDate>
            <atom:updated>2026-10-27T06:59:30.635Z</atom:updated>
            <content:encoded><![CDATA[<p>Every data-driven company shares a familiar bottleneck: business analysts, product managers, and executives need answers from data, while the relational databases holding those answers speak only SQL. The standard workaround is filing tickets, waiting on data engineers, or relying on rigid dashboard filters.</p><p>What if anyone could ask questions in plain English and have a machine learning model generate syntactically correct, schema-aware SQL queries on the fly?</p><p>In this article, I walk through how I built and fine-tuned a deep learning sequence-to-sequence (Seq2Seq) pipeline using the Hugging Face Transformers ecosystem to convert natural language queries directly into executable SQL statements.</p><h3>1. Framing Text-to-SQL as a Seq2Seq Problem</h3><p>At its core, Text-to-SQL is an NLP semantic parsing challenge. We treat it as a machine translation task: translating human intent (source language) into structured query syntax (target language).</p><p>Unlike standard language translation, however, the target language must satisfy strict grammatical constraints:</p><ul><li>Valid SQL clauses (SELECT, FROM, WHERE, GROUP BY, ORDER BY).</li><li>Correct table and column names matching the target database schema.</li><li>Appropriate condition operators (&gt;, =, LIKE, BETWEEN).</li></ul><pre>+--------------------------------------------------------+<br>| Input Prompt:                                          |<br>| &quot;List customers from New York with balance &gt; 5000&quot;    |<br>+--------------------------------------------------------+<br>                           │<br>                           ▼<br>             [ Transformer Seq2Seq Model ]<br>                           │<br>                           ▼<br>+--------------------------------------------------------+<br>| Output SQL:                                            |<br>| SELECT name FROM customers                             |<br>| WHERE city = &#39;New York&#39; AND balance &gt; 5000;            |<br>+--------------------------------------------------------+</pre><h3>2. Model Selection: Why Encoder-Decoder?</h3><p>For sequence generation conditioned on structured input, an <strong>encoder-decoder architecture</strong> is the natural choice. While causal decoder-only models (like GPT) excel at open-ended text, encoder-decoder models (such as T5 or BART) provide bidirectional context understanding on the prompt while auto-regressively decoding deterministic tokens.</p><p>We selected a T5-based checkpoint (t5-small / t5-base). T5 frames every task as a &quot;text-to-text&quot; problem, making it well-suited for mapping input strings directly to output SQL code:</p><p>Python</p><pre>from transformers import AutoTokenizer, AutoModelForSeq2SeqLM<br>model_name = &quot;t5-small&quot;<br>tokenizer = AutoTokenizer.from_pretrained(model_name)<br>model = AutoModelForSeq2SeqLM.from_pretrained(model_name)</pre><h3>3. Data Formatting and Preprocessing</h3><p>A Text-to-SQL model cannot rely solely on the question; it needs context about the database structure. Without the schema, the model has to guess table and column names, leading to hallucinations.</p><h3>Structuring the Input Sequence</h3><p>We prepend a task prefix and serialize the schema directly into the prompt:</p><p>Plaintext</p><pre>translate English to SQL: What is the highest salary in Engineering? | Table: employees (id, name, salary, department)</pre><h3>Tokenization Pipeline</h3><p>We handle tokenization using Hugging Face’s BatchEncoding, setting explicit truncation and padding rules to handle variable-length queries:</p><p>Python</p><pre>def preprocess_data(batch, tokenizer, max_input_len=256, max_target_len=128):<br>    inputs = [f&quot;translate English to SQL: {q} | Schema: {s}&quot; <br>              for q, s in zip(batch[&quot;question&quot;], batch[&quot;schema&quot;])]<br>    targets = batch[&quot;sql_query&quot;]<br>model_inputs = tokenizer(<br>        inputs, <br>        max_length=max_input_len, <br>        padding=&quot;max_length&quot;, <br>        truncation=True<br>    )<br>    <br>    # Tokenize target SQL queries<br>    labels = tokenizer(<br>        targets, <br>        max_length=max_target_len, <br>        padding=&quot;max_length&quot;, <br>        truncation=True<br>    )<br>    # Replace padding token id with -100 so PyTorch CrossEntropy ignores it<br>    labels[&quot;input_ids&quot;] = [<br>        [(label if label != tokenizer.pad_token_id else -100) for label in sequence]<br>        for sequence in labels[&quot;input_ids&quot;]<br>    ]<br>    model_inputs[&quot;labels&quot;] = labels[&quot;input_ids&quot;]<br>    return model_inputs</pre><h3>4. Fine-Tuning the Model</h3><p>We set up training using the Seq2SeqTrainer and Seq2SeqTrainingArguments, which handle gradient updates, learning rate decay, evaluation loops, and checkpoint saving.</p><p>Python</p><pre>from transformers import Seq2SeqTrainingArguments, Seq2SeqTrainer, DataCollatorForSeq2Seq<br>training_args = Seq2SeqTrainingArguments(<br>    output_dir=&quot;./text-to-sql-results&quot;,<br>    evaluation_strategy=&quot;epoch&quot;,<br>    learning_rate=3e-4,<br>    per_device_train_batch_size=8,<br>    per_device_eval_batch_size=8,<br>    weight_decay=0.01,<br>    save_total_limit=2,<br>    num_train_epochs=5,<br>    predict_with_generate=True,<br>    fp16=True, # Enable if running on a compatible GPU<br>    logging_dir=&quot;./logs&quot;,<br>    logging_steps=50,<br>)<br>data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)<br>trainer = Seq2SeqTrainer(<br>    model=model,<br>    args=training_args,<br>    train_dataset=tokenized_train_dataset,<br>    eval_dataset=tokenized_val_dataset,<br>    tokenizer=tokenizer,<br>    data_collator=data_collator,<br>)<br>trainer.train()</pre><h3>5. Evaluation: Exact Match vs. Execution Accuracy</h3><p>Evaluating Text-to-SQL is more nuanced than calculating standard BLEU or ROUGE scores. There are two primary evaluation standards:</p><ul><li><strong>Exact Match (EM):</strong> Compares the predicted SQL string against the ground-truth string character-for-character (or token-by-token).</li><li><em>Limitation:</em> SQL offers multiple ways to write the same logic (e.g., WHERE age &gt; 30 AND status = &#39;active&#39; vs. WHERE status = &#39;active&#39; AND age &gt; 30). EM marks valid variations as incorrect.</li><li><strong>Execution Accuracy (EX):</strong> Executes both the predicted query and the ground-truth query against an active SQLite or PostgreSQL database and compares the returned records.</li></ul><p>During validation, the loss converged steadily, achieving high syntactic validity on single-table queries and filters.</p><h3>6. Running Inference on Custom Questions</h3><p>Once fine-tuned, running inference requires passing natural text through the model’s auto-regressive decoding loop:</p><p>Python</p><pre>import torch<br>def generate_sql(question, schema, model, tokenizer, device=&quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;):<br>    model.to(device)<br>    model.eval()<br>    prompt = f&quot;translate English to SQL: {question} | Schema: {schema}&quot;<br>    inputs = tokenizer(prompt, return_tensors=&quot;pt&quot;, truncation=True, max_length=256).to(device)<br>    with torch.no_grad():<br>        outputs = model.generate(<br>            **inputs,<br>            max_length=128,<br>            num_beams=4,<br>            early_stopping=True<br>        )<br>    return tokenizer.decode(outputs[0], skip_special_tokens=True)<br># Example usage<br>schema_example = &quot;orders (order_id, customer_id, total_amount, order_date)&quot;<br>question_example = &quot;Show all orders with a total amount greater than 200&quot;<br>print(generate_sql(question_example, schema_example, model, tokenizer))<br># Output: SELECT * FROM orders WHERE total_amount &gt; 200;</pre><h3>7. Key Challenges &amp; Real-World Lessons</h3><ul><li><strong>Schema Grounding:</strong> When tables contain dozens of columns, small models can hallucinate plausible-sounding column names. Serializing schema representations clearly is critical to prevent errors.</li><li><strong>Join Predicates:</strong> Multi-table joins require the model to infer primary-key/foreign-key relationships implicitly from column names unless foreign-key constraints are explicitly provided in the prompt.</li><li><strong>Execution Guardrails:</strong> In production, generated queries should never execute directly against production databases without safeguards. Implementing read-only database roles, query timeouts, and parsing with an AST tool (such as sqlparse) prevents unintentional data mutations.</li></ul><h3>Final Thoughts &amp; Source Code</h3><p>Transformer-based Seq2Seq architectures offer a practical path toward making relational data accessible to non-technical users. While edge cases around complex nested queries remain an active area of research, fine-tuned lightweight models deliver dependable results on focused schemas.</p><p>The complete code, notebooks, and training scripts are available on GitHub: 👉 <a href="https://github.com/M-Abdullah-Ali-2468/Text-to-SQL-Generator-from-Transformers"><strong>GitHub Repository: Text-to-SQL Generator from Transformers</strong></a></p><p>If you found this project helpful, feel free to star the repo or connect on GitHub!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8999ebbc1512" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building an Urdu Question Generation System from Scratch]]></title>
            <link>https://medium.com/@mabdullahali8642/sequence-to-sequence-question-generation-for-urdu-9f3d6443e654?source=rss-22a6f1700e53------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f3d6443e654</guid>
            <category><![CDATA[generation-z]]></category>
            <category><![CDATA[encoder-decoder]]></category>
            <category><![CDATA[seq2seq]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Mabdullahali]]></dc:creator>
            <pubDate>Sat, 12 Sep 2026 16:45:50 GMT</pubDate>
            <atom:updated>2026-09-14T23:09:14.153Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction:<br></strong>In this blog we will deep dive in the discussion on how we built a Seq2Seq from scratch to generate urdu questions from answers. This is just like a teacher who prepares questions from text book for students. Also this development allows students to understand working or encoder and decoder at develop level depth which is far better than a student who just load and train the model on cleaned data.</p><p>For example, if a sentence contains an answer such as &lt;ans&gt;1947ء&lt;/ans&gt;, the model should generate a question like “پاکستان نے آزادی کب حاصل کی؟”. This enables the model to learn educational content. This is purely a Seq2Seq model as sequeucne of answers and questions matters.</p><p><strong>Dataset and Preprocessing:<br></strong>We used <a href="https://huggingface.co/datasets/uqa/UQA">UQA(Urdu Question Answering) dataset</a> for the model training. The original UQA dataset contains the columns <strong>id, </strong><strong>title, </strong><strong>context, </strong><strong>question, and </strong><strong>answers</strong>, where answers contains the <strong>answer text</strong> and its <strong>character start position (</strong><strong>answer_start). </strong>But training on this raw data that contains long paragraphs may take more time and more harder for the model to train on it. So we crafted this data set in such a way that in content we tagged answers with custom defined &lt;ans&gt; &lt;/ans&gt; tag. Also we extracted only those sentence in the paragraph that contained answer. And we named context columns with answer tag as <strong>source </strong>and question as <strong>target </strong>and stores them in the form of pairs.<br>The processed data statistics were:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*_XfjmVIbU6tmd7_JIk6ABg.png" /><figcaption><strong>Porcessed Data Statistics</strong></figcaption></figure><p><strong>Urdu Tokenization:<br></strong>For tokenization we trained our own SetencePiece tokenizer that used unigram method instead of using any pretrained tokenizer model. Our vocabulary contained 8000 words that we learned from source and target pairs stores seperately in the file. Also we reserved special tokens like <strong>PAD</strong>, <strong>UNK</strong>, <strong>BOS</strong> and <strong>EOS</strong>. Also <strong>&lt;ans&gt;&lt;/ans&gt;</strong> as custom token. This was an important step because urdu as many morphological variations, training on our data allows model to represents in this morphological form that used in the dataset usign subwords technique.</p><p><strong>Model Design:<br></strong>Our model were LSTM. We used premitives like nn.lstm, nn.linear and nn.embeddings from pytorch. But building and encoder decoder were completely from scratch. So encoder was 2 layered bidirectional lstm. We decided this architecture for encdoer because we want that model has correct context for each sentence also layers was intented for learn sentence level context. Decoder were 2 layered lstm. Also to not only depend a single context vector we used attention machanism. Just like encoder and decoder it was built completely from scracth. Here is the architecture details of model:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*0K2sGlhQWN3sXET-pEmkZw.png" /><figcaption><strong>Proposed model architecutre details</strong></figcaption></figure><p>This model was trained using google colab and after training it we pushed it on hugging face. During training we used teacher forcing with the intention of that we made our training correct by intentionally feeding model with correct answers. It was trained on 12 epochs and each epoch took 15 mintues in approx, total training time we approx 3 hours. It was trained using T4-GPU.</p><p>For inference we used <strong>greedy and beam search apprach with k=3. </strong>Greedy algorithm selects the highest probability word while beam search selected top k words higher in probaility in our case k=3.</p><p><strong>Results:<br></strong>We evaluated our trained model on <strong>UQA Validation </strong>dataset also another dataset <a href="https://huggingface.co/datasets/uqa/Wiki-UQA"><strong>Wiki-UQA dataset</strong></a><strong>. </strong>For evaluation we used 4 metrices: <strong>BLEU-4, ROUGE, PERPLEXITY </strong>and <strong>UNK-RATE. </strong>These were results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*BoEDJH07ePsig2i6QeJ31w.png" /><figcaption><strong>Evaluation Metrices Results</strong></figcaption></figure><p>The results show that beam search performed slightly better than greedy decoding on UQA validation and showed a larger improvement on Wiki-UQA. At the same time, the higher perplexity on Wiki-UQA indicates that the out-of-domain data was more difficult for the model.</p><p><strong>Training Porgress:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*Ybw8RAOWGDKRkxz63ow1Lg.png" /><figcaption><strong>Training and Validation Loss Per Epoch</strong></figcaption></figure><p>These curves shows that model worked well in the inital epochs but with progress validation error started to increase with training error keep decresing, this showed that as we progressed in epochs, model started to overfit somehow. But to avoid this we used checkpoint method to save the best model.</p><p><strong>Three Real Outputs:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*Mlw5oSOc0DdJa3BylVySgw.png" /><figcaption><strong>Output Generated Durign Inferencing</strong></figcaption></figure><p>This above image showed that beam searced generated more meaningfull questions as compared to greedy search.</p><p><strong>Failure Cases:<br></strong>Model biggest limitation were on answerability and revelance. Some questiosn were out or source, some indicated wrong question structure and irrelevent words.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/1*xGM_N8_u4uAV6w_kZ8izgQ.png" /></figure><p><strong>Human Evaluation:<br></strong>We saved 50 generated questions as samples and did human evaluation based on three key factors: Fluency, Revelance and Answerability. These were results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*7Dk6sK-A8lcjnN3eDlWP9Q.png" /></figure><p><strong>What we learned:<br></strong>First we learned how to design a model for sequence generation tasks. We got a deep understanding of implementing encoder decoder with attention mechanism. We learned data preprocessing, development from scratch, problem-solution designing, teacher forcing.</p><p>We also learned beam search can more correct results because we are not cancelling the expected ways in which answer may get high probaility of having a correct solution, in the start. We also observed model struggled in determining out of examples dataset due to some overfitting.</p><p>Github: <a href="https://github.com/M-Abdullah-Ali-2468/urdu-questions-generator.git">https://github.com/M-Abdullah-Ali-2468/urdu-questions-generator.git</a><br>Model link: <a href="https://huggingface.co/mabdullahali/questions_generator">https://huggingface.co/mabdullahali/questions_generator</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f3d6443e654" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>