File size: 2,286 Bytes
b8f71c2
 
 
d3bc1c4
b8f71c2
 
d3bc1c4
 
b8f71c2
d3bc1c4
b8f71c2
d3bc1c4
b8f71c2
d3bc1c4
 
b8f71c2
d3bc1c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f71c2
d3bc1c4
 
 
 
 
 
 
 
 
 
 
 
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
# app.py

import os
import gradio as gr
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from huggingface_hub import InferenceClient

# Load FAISS index and embedding model
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
db = FAISS.load_local("faiss_index", embedding_model)

# Load Hugging Face Inference API client
client = InferenceClient(
    model="HuggingFaceH4/zephyr-7b-beta",
    token=os.getenv("HF_TOKEN")  # Make sure this is set in your environment
)

def ask_law_bot(query):
    try:
        results = db.similarity_search(query, k=5, filter={"section": "PPC"})
        if not results:
            return "❌ No relevant content found for this topic."

        context = "\n\n".join([doc.page_content for doc in results if len(doc.page_content.strip()) > 100])

        prompt = f"""You are a legal assistant helping users understand Pakistani law.
Respond to the question using the given legal context. Your answer must follow these rules:
- Use numbered bullet points (1. 2. 3.)
- Reference relevant law sections like (section 220(b))
- Be concise, clear, and avoid repetition
- Use "YES" or "NO" if the question requires binary response
Context:
{context}
Question: {query}
Answer:"""

        response = client.chat_completion(
            messages=[
                {"role": "system", "content": "You are a helpful and concise legal assistant for Pakistani law."},
                {"role": "user", "content": prompt}
            ],
            max_tokens=512
        )

        return response.choices[0].message["content"].strip()

    except Exception as e:
        return f"❌ Error: {e}"

# Gradio UI
gr.Interface(
    fn=ask_law_bot,
    inputs=gr.Textbox(lines=2, placeholder="e.g., What is the punishment for theft?"),
    outputs=gr.Textbox(label="πŸ“˜ Legal Answer"),
    title="βš–οΈ Ask Pakistan Law β€” Powered by Zephyr 7B",
    description="Ask questions from Pakistan's law using FAISS retrieval + Zephyr-7B via Hugging Face API.",
    examples=[
        "What is the punishment for theft?",
        "What are the duties of the Commission?",
        "What is the process of appeal under this law?"
    ]
).launch(share=True, debug=True)