alaax159/AI_hackathon

Python

0

2 commits

updated Apr 25, 2026

See the code

README

AI Legal Aid for Palestine πŸ‡΅πŸ‡Έ

A voice-based AI legal assistant that helps Palestinian citizens understand their rights under the Palestinian Basic Law (2003, amended 2005), auto-fill legal forms, and find nearby legal aid clinics β€” available in Arabic and English.


Project Overview

Most people who face legal problems β€” eviction, labor disputes, custody issues β€” have no access to a lawyer. This project puts a legal expert in their pocket, for free.

The system works like this:

  1. User speaks their problem in Arabic or English
  2. Whisper transcribes the speech to text
  3. The intent is classified (eviction, labor, family, criminal, etc.)
  4. Relevant articles from the Palestinian Basic Law are retrieved via RAG
  5. A local legal LLM reasons over the retrieved law and explains the user's rights in plain language
  6. The system optionally auto-fills a legal form (PDF) or refers to a nearby clinic

Repository Structure

legal-aid-palestine/
β”‚
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/
β”‚   β”‚   └── palestine_basic_law_en.txt        # Full text from constituteproject.org
β”‚   β”œβ”€β”€ processed/
β”‚   β”‚   β”œβ”€β”€ articles.jsonl                    # Parsed articles (id, title, text)
β”‚   β”‚   β”œβ”€β”€ training_qa.jsonl                 # Fine-tuning Q&A pairs
β”‚   β”‚   └── rag_chunks.jsonl                  # RAG vector store chunks
β”‚   └── forms/
β”‚       └── templates/                        # PDF form templates
β”‚
β”œβ”€β”€ pipeline/
β”‚   └── palestine_law_pipeline.py             # Fetch β†’ parse β†’ generate Q&A + RAG chunks
β”‚
β”œβ”€β”€ model/
β”‚   β”œβ”€β”€ finetune.py                           # QLoRA fine-tuning script
β”‚   β”œβ”€β”€ inference.py                          # Local inference wrapper
β”‚   └── rag.py                               # ChromaDB vector store + retrieval
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                              # FastAPI backend
β”‚   β”œβ”€β”€ transcribe.py                        # Whisper speech-to-text
β”‚   β”œβ”€β”€ intent.py                            # Legal intent classifier
β”‚   β”œβ”€β”€ form_filler.py                       # PDF auto-fill
β”‚   └── referral.py                          # Nearby clinic lookup
β”‚
β”œβ”€β”€ frontend/
β”‚   └── index.html                           # Simple voice UI (Arabic + English)
β”‚
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .env.example
└── README.md

Tech Stack

LayerTechnologyWhy
Speech-to-textfaster-whisperBest Arabic transcription, runs offline
Legal reasoningSaulLM-7B or Mistral-7B (QLoRA fine-tuned)Open source, no API dependency
Vector storeChromaDBLightweight, local, easy to set up
Embeddingssentence-transformers/paraphrase-multilingual-mpnet-base-v2Arabic + English support
BackendFastAPIFast, async, easy to deploy
PDF formsreportlab + pypdfGenerate and fill Arabic-ready PDFs
FrontendPlain HTML + JSWorks on any device, low bandwidth

Setup

1. Clone and install

git clone https://github.com/your-username/legal-aid-palestine
cd legal-aid-palestine
pip install -r requirements.txt

2. Build the data pipeline

# Fetch, parse, and generate training data from the Palestinian Basic Law
python pipeline/palestine_law_pipeline.py

# Output files:
# data/processed/articles.jsonl        β€” structured articles
# data/processed/training_qa.jsonl     β€” Q&A pairs for fine-tuning
# data/processed/rag_chunks.jsonl      β€” chunks for vector DB

3. Build the vector store

python model/rag.py --build
# Embeds all RAG chunks into ChromaDB
# Stored locally at: ./chroma_db/

4. (Optional) Fine-tune the model

Only needed if you want a fully custom model. Skip this for a quick demo β€” just use SaulLM-7B directly with RAG.

python model/finetune.py \
  --base_model "Equall/Saul-7B-Instruct-v1" \
  --data "data/processed/training_qa.jsonl" \
  --output_dir "./legal-model-lora" \
  --epochs 3

Requires a GPU with 8GB+ VRAM. Works on Google Colab T4.

5. Run the app

uvicorn app.main:app --reload --port 8000
# Open: http://localhost:8000

Data Pipeline Details

Source

The Palestinian Basic Law (2003, amended 2005) is sourced from:

These are public domain government texts. No copyright restrictions.

Pipeline steps

constituteproject.org
        β”‚
        β–Ό
   fetch_constitution()
        β”‚  Raw HTML/text
        β–Ό
   parse_articles()
        β”‚  List of {id, title, text}
        β–Ό
   β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
   β–Ό         β–Ό
generate_   generate_
qa_pairs()  rag_chunks()
   β”‚              β”‚
   β–Ό              β–Ό
training_      rag_
qa.jsonl      chunks.jsonl

Training data format (JSONL)

{"instruction": "Under Palestinian law, what are my rights regarding personal freedom and unlawful arrest?", "response": "Under Article 11 of the Palestinian Basic Law: Personal freedom is a natural right, shall be guaranteed and may not be violated. It is unlawful to arrest, search, imprison, restrict the freedom, or prevent the movement of any person, except by judicial order in accordance with the provisions of the law. Note: This is general legal information. For specific legal advice, please consult a qualified Palestinian lawyer.", "source": "Palestine Basic Law 2003 (rev. 2005)", "article": "Article 11"}
{"instruction": "What does Article 14 of the Palestinian Basic Law say about presumption of innocence and right to a lawyer?", "response": "According to Article 14 of the Palestinian Basic Law (2003, amended 2005): An accused person is considered innocent until proven guilty in a court of law. Any person accused in a criminal case shall be represented by a lawyer.", "source": "Palestine Basic Law 2003 (rev. 2005)", "article": "Article 14"}

RAG chunk format (JSONL)

{"id": "palestine_basic_law_Article_11", "text": "Article 11 (TITLE TWO β€” PUBLIC RIGHTS AND LIBERTIES): Personal freedom is a natural right, shall be guaranteed and may not be violated...", "metadata": {"source": "Palestine Basic Law 2003", "article": "Article 11", "title": "TITLE TWO β€” PUBLIC RIGHTS AND LIBERTIES", "keywords": ["arrest", "freedom", "detention", "judicial order"]}}

Model Details

Option A β€” RAG only (fastest, good for hackathon demo)

Use SaulLM-7B-Instruct directly. Feed it the relevant Basic Law articles at inference time via RAG. No training needed.

# model/inference.py
from transformers import pipeline
from model.rag import retrieve

llm = pipeline("text-generation", model="Equall/Saul-7B-Instruct-v1")

def answer(user_question):
    chunks = retrieve(user_question, top_k=3)
    context = "\n\n".join(chunks)
    prompt = f"""[INST]
You are a legal aid assistant for Palestinian citizens.
Use ONLY the following legal articles to answer. Always add a note to consult a real lawyer.

Legal context:
{context}

User question: {user_question}
[/INST]"""
    return llm(prompt, max_new_tokens=400, temperature=0.2)[0]["generated_text"]

Option B β€” Fine-tuned model (best accuracy)

QLoRA fine-tune on data/processed/training_qa.jsonl using the script in model/finetune.py. See Setup step 4.

Recommended base models:

ModelBest for
Equall/Saul-7B-Instruct-v1English legal text, strong base
mistralai/Mistral-7B-Instruct-v0.2General multilingual, easy fine-tune
inceptionai/jais-13b-chatArabic-first, best for Arabic users
FreedomIntelligence/AceGPT-7BArabic-optimized alternative

The model is trained on all 121 articles of the Basic Law. High-priority articles for the legal aid use case:

ArticleTopic
9Equality before the law β€” no discrimination
11Personal freedom β€” unlawful arrest
12Rights of detained persons β€” right to a lawyer
13Prohibition of torture β€” confessions under duress are void
14Presumption of innocence
15No collective punishment
17Home is inviolable β€” search warrants required
21Property rights β€” expropriation only with fair compensation
23Right to housing
24Right to free education
25Right to work β€” labor protections
30Right to access courts
110–113State of emergency rights

Demo Script (Hackathon)

The most powerful demo scenario:

"My landlord verbally told me I have 3 days to leave. What are my rights?"

Expected flow:

  1. Whisper transcribes the Arabic voice input
  2. Intent: eviction
  3. RAG retrieves Articles 11, 17, 21, 30
  4. Model responds: explains that a verbal notice has no legal standing, the landlord must provide written notice, and the user has the right to contest in court
  5. System generates a PDF demand letter to the landlord
  6. Shows the nearest free legal clinic in Nablus/Ramallah

System Prompt

You are a legal aid assistant helping Palestinian citizens understand their rights
under the Palestinian Basic Law (2003, amended 2005).

Rules:
- Answer ONLY based on the provided legal context
- Use plain, simple language (Grade 8 reading level)
- Always end with: "This is general legal information. For your specific situation, consult a licensed Palestinian lawyer or visit a legal aid center."
- Never give advice that could harm the user
- If the answer is not in the provided articles, say so clearly
- Support both Arabic and English responses

Roadmap

  • Data pipeline (fetch + parse + generate training data)
  • RAG vector store with Palestinian Basic Law
  • Fine-tuning script (QLoRA)
  • FastAPI backend
  • Arabic voice input (Whisper integration)
  • PDF form auto-fill (eviction notice response, labor complaint)
  • Referral database (legal aid clinics in West Bank + Gaza)
  • Mobile-friendly frontend
  • Expand to: Labour Law, Family Law, Criminal Procedure Law
  • Jordanian Civil Law coverage (still applicable in West Bank)

Adding More Laws

The pipeline is designed to be extended. To add a new law:

  1. Add the raw text to data/raw/
  2. Update parse_articles() in the pipeline to handle its format
  3. Add topic mappings to ARTICLE_TOPICS
  4. Re-run the pipeline
  5. Rebuild the vector store

Recommended next laws to add:

  • Palestinian Labour Law No. 7 of 2000
  • Palestinian Civil Procedure Law
  • Palestinian Penal Code
  • Jordanian Civil Law (applicable in West Bank)
  • Personal Status Law

Requirements

transformers>=4.40.0
peft>=0.10.0
trl>=0.8.0
bitsandbytes>=0.43.0
accelerate>=0.29.0
sentence-transformers>=2.7.0
chromadb>=0.5.0
faster-whisper>=1.0.0
fastapi>=0.111.0
uvicorn>=0.29.0
reportlab>=4.1.0
pypdf>=4.2.0
datasets>=2.19.0
torch>=2.2.0
requests>=2.31.0
python-multipart>=0.0.9

Important Note

This system provides general legal information only, not legal advice. It is not a substitute for a qualified Palestinian lawyer. Always direct users to professional legal help for serious matters.


License

MIT License β€” free to use, modify, and distribute.


Resources

Similar repos

alaax159/AI_hackathon

Python

0

2 commits

updated Apr 25, 2026

See the code

README

AI Legal Aid for Palestine πŸ‡΅πŸ‡Έ

A voice-based AI legal assistant that helps Palestinian citizens understand their rights under the Palestinian Basic Law (2003, amended 2005), auto-fill legal forms, and find nearby legal aid clinics β€” available in Arabic and English.


Project Overview

Most people who face legal problems β€” eviction, labor disputes, custody issues β€” have no access to a lawyer. This project puts a legal expert in their pocket, for free.

The system works like this:

  1. User speaks their problem in Arabic or English
  2. Whisper transcribes the speech to text
  3. The intent is classified (eviction, labor, family, criminal, etc.)
  4. Relevant articles from the Palestinian Basic Law are retrieved via RAG
  5. A local legal LLM reasons over the retrieved law and explains the user's rights in plain language
  6. The system optionally auto-fills a legal form (PDF) or refers to a nearby clinic

Repository Structure

legal-aid-palestine/
β”‚
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/
β”‚   β”‚   └── palestine_basic_law_en.txt        # Full text from constituteproject.org
β”‚   β”œβ”€β”€ processed/
β”‚   β”‚   β”œβ”€β”€ articles.jsonl                    # Parsed articles (id, title, text)
β”‚   β”‚   β”œβ”€β”€ training_qa.jsonl                 # Fine-tuning Q&A pairs
β”‚   β”‚   └── rag_chunks.jsonl                  # RAG vector store chunks
β”‚   └── forms/
β”‚       └── templates/                        # PDF form templates
β”‚
β”œβ”€β”€ pipeline/
β”‚   └── palestine_law_pipeline.py             # Fetch β†’ parse β†’ generate Q&A + RAG chunks
β”‚
β”œβ”€β”€ model/
β”‚   β”œβ”€β”€ finetune.py                           # QLoRA fine-tuning script
β”‚   β”œβ”€β”€ inference.py                          # Local inference wrapper
β”‚   └── rag.py                               # ChromaDB vector store + retrieval
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                              # FastAPI backend
β”‚   β”œβ”€β”€ transcribe.py                        # Whisper speech-to-text
β”‚   β”œβ”€β”€ intent.py                            # Legal intent classifier
β”‚   β”œβ”€β”€ form_filler.py                       # PDF auto-fill
β”‚   └── referral.py                          # Nearby clinic lookup
β”‚
β”œβ”€β”€ frontend/
β”‚   └── index.html                           # Simple voice UI (Arabic + English)
β”‚
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .env.example
└── README.md

Tech Stack

LayerTechnologyWhy
Speech-to-textfaster-whisperBest Arabic transcription, runs offline
Legal reasoningSaulLM-7B or Mistral-7B (QLoRA fine-tuned)Open source, no API dependency
Vector storeChromaDBLightweight, local, easy to set up
Embeddingssentence-transformers/paraphrase-multilingual-mpnet-base-v2Arabic + English support
BackendFastAPIFast, async, easy to deploy
PDF formsreportlab + pypdfGenerate and fill Arabic-ready PDFs
FrontendPlain HTML + JSWorks on any device, low bandwidth

Setup

1. Clone and install

git clone https://github.com/your-username/legal-aid-palestine
cd legal-aid-palestine
pip install -r requirements.txt

2. Build the data pipeline

# Fetch, parse, and generate training data from the Palestinian Basic Law
python pipeline/palestine_law_pipeline.py

# Output files:
# data/processed/articles.jsonl        β€” structured articles
# data/processed/training_qa.jsonl     β€” Q&A pairs for fine-tuning
# data/processed/rag_chunks.jsonl      β€” chunks for vector DB

3. Build the vector store

python model/rag.py --build
# Embeds all RAG chunks into ChromaDB
# Stored locally at: ./chroma_db/

4. (Optional) Fine-tune the model

Only needed if you want a fully custom model. Skip this for a quick demo β€” just use SaulLM-7B directly with RAG.

python model/finetune.py \
  --base_model "Equall/Saul-7B-Instruct-v1" \
  --data "data/processed/training_qa.jsonl" \
  --output_dir "./legal-model-lora" \
  --epochs 3

Requires a GPU with 8GB+ VRAM. Works on Google Colab T4.

5. Run the app

uvicorn app.main:app --reload --port 8000
# Open: http://localhost:8000

Data Pipeline Details

Source

The Palestinian Basic Law (2003, amended 2005) is sourced from:

These are public domain government texts. No copyright restrictions.

Pipeline steps

constituteproject.org
        β”‚
        β–Ό
   fetch_constitution()
        β”‚  Raw HTML/text
        β–Ό
   parse_articles()
        β”‚  List of {id, title, text}
        β–Ό
   β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
   β–Ό         β–Ό
generate_   generate_
qa_pairs()  rag_chunks()
   β”‚              β”‚
   β–Ό              β–Ό
training_      rag_
qa.jsonl      chunks.jsonl

Training data format (JSONL)

{"instruction": "Under Palestinian law, what are my rights regarding personal freedom and unlawful arrest?", "response": "Under Article 11 of the Palestinian Basic Law: Personal freedom is a natural right, shall be guaranteed and may not be violated. It is unlawful to arrest, search, imprison, restrict the freedom, or prevent the movement of any person, except by judicial order in accordance with the provisions of the law. Note: This is general legal information. For specific legal advice, please consult a qualified Palestinian lawyer.", "source": "Palestine Basic Law 2003 (rev. 2005)", "article": "Article 11"}
{"instruction": "What does Article 14 of the Palestinian Basic Law say about presumption of innocence and right to a lawyer?", "response": "According to Article 14 of the Palestinian Basic Law (2003, amended 2005): An accused person is considered innocent until proven guilty in a court of law. Any person accused in a criminal case shall be represented by a lawyer.", "source": "Palestine Basic Law 2003 (rev. 2005)", "article": "Article 14"}

RAG chunk format (JSONL)

{"id": "palestine_basic_law_Article_11", "text": "Article 11 (TITLE TWO β€” PUBLIC RIGHTS AND LIBERTIES): Personal freedom is a natural right, shall be guaranteed and may not be violated...", "metadata": {"source": "Palestine Basic Law 2003", "article": "Article 11", "title": "TITLE TWO β€” PUBLIC RIGHTS AND LIBERTIES", "keywords": ["arrest", "freedom", "detention", "judicial order"]}}

Model Details

Option A β€” RAG only (fastest, good for hackathon demo)

Use SaulLM-7B-Instruct directly. Feed it the relevant Basic Law articles at inference time via RAG. No training needed.

# model/inference.py
from transformers import pipeline
from model.rag import retrieve

llm = pipeline("text-generation", model="Equall/Saul-7B-Instruct-v1")

def answer(user_question):
    chunks = retrieve(user_question, top_k=3)
    context = "\n\n".join(chunks)
    prompt = f"""[INST]
You are a legal aid assistant for Palestinian citizens.
Use ONLY the following legal articles to answer. Always add a note to consult a real lawyer.

Legal context:
{context}

User question: {user_question}
[/INST]"""
    return llm(prompt, max_new_tokens=400, temperature=0.2)[0]["generated_text"]

Option B β€” Fine-tuned model (best accuracy)

QLoRA fine-tune on data/processed/training_qa.jsonl using the script in model/finetune.py. See Setup step 4.

Recommended base models:

ModelBest for
Equall/Saul-7B-Instruct-v1English legal text, strong base
mistralai/Mistral-7B-Instruct-v0.2General multilingual, easy fine-tune
inceptionai/jais-13b-chatArabic-first, best for Arabic users
FreedomIntelligence/AceGPT-7BArabic-optimized alternative

The model is trained on all 121 articles of the Basic Law. High-priority articles for the legal aid use case:

ArticleTopic
9Equality before the law β€” no discrimination
11Personal freedom β€” unlawful arrest
12Rights of detained persons β€” right to a lawyer
13Prohibition of torture β€” confessions under duress are void
14Presumption of innocence
15No collective punishment
17Home is inviolable β€” search warrants required
21Property rights β€” expropriation only with fair compensation
23Right to housing
24Right to free education
25Right to work β€” labor protections
30Right to access courts
110–113State of emergency rights

Demo Script (Hackathon)

The most powerful demo scenario:

"My landlord verbally told me I have 3 days to leave. What are my rights?"

Expected flow:

  1. Whisper transcribes the Arabic voice input
  2. Intent: eviction
  3. RAG retrieves Articles 11, 17, 21, 30
  4. Model responds: explains that a verbal notice has no legal standing, the landlord must provide written notice, and the user has the right to contest in court
  5. System generates a PDF demand letter to the landlord
  6. Shows the nearest free legal clinic in Nablus/Ramallah

System Prompt

You are a legal aid assistant helping Palestinian citizens understand their rights
under the Palestinian Basic Law (2003, amended 2005).

Rules:
- Answer ONLY based on the provided legal context
- Use plain, simple language (Grade 8 reading level)
- Always end with: "This is general legal information. For your specific situation, consult a licensed Palestinian lawyer or visit a legal aid center."
- Never give advice that could harm the user
- If the answer is not in the provided articles, say so clearly
- Support both Arabic and English responses

Roadmap

  • Data pipeline (fetch + parse + generate training data)
  • RAG vector store with Palestinian Basic Law
  • Fine-tuning script (QLoRA)
  • FastAPI backend
  • Arabic voice input (Whisper integration)
  • PDF form auto-fill (eviction notice response, labor complaint)
  • Referral database (legal aid clinics in West Bank + Gaza)
  • Mobile-friendly frontend
  • Expand to: Labour Law, Family Law, Criminal Procedure Law
  • Jordanian Civil Law coverage (still applicable in West Bank)

Adding More Laws

The pipeline is designed to be extended. To add a new law:

  1. Add the raw text to data/raw/
  2. Update parse_articles() in the pipeline to handle its format
  3. Add topic mappings to ARTICLE_TOPICS
  4. Re-run the pipeline
  5. Rebuild the vector store

Recommended next laws to add:

  • Palestinian Labour Law No. 7 of 2000
  • Palestinian Civil Procedure Law
  • Palestinian Penal Code
  • Jordanian Civil Law (applicable in West Bank)
  • Personal Status Law

Requirements

transformers>=4.40.0
peft>=0.10.0
trl>=0.8.0
bitsandbytes>=0.43.0
accelerate>=0.29.0
sentence-transformers>=2.7.0
chromadb>=0.5.0
faster-whisper>=1.0.0
fastapi>=0.111.0
uvicorn>=0.29.0
reportlab>=4.1.0
pypdf>=4.2.0
datasets>=2.19.0
torch>=2.2.0
requests>=2.31.0
python-multipart>=0.0.9

Important Note

This system provides general legal information only, not legal advice. It is not a substitute for a qualified Palestinian lawyer. Always direct users to professional legal help for serious matters.


License

MIT License β€” free to use, modify, and distribute.


Resources

Similar repos