File size: 6,160 Bytes
c430d22
8f996bb
bf5af54
 
 
8f996bb
c430d22
8f996bb
 
 
bf5af54
 
8f996bb
 
 
bf5af54
c430d22
8f996bb
 
 
bf5af54
c430d22
8f996bb
 
 
bf5af54
 
c430d22
bf5af54
 
 
c430d22
8f996bb
 
c430d22
8f996bb
c430d22
8f996bb
 
 
bf5af54
 
8f996bb
bf5af54
8f996bb
bf5af54
 
8f996bb
bf5af54
8f996bb
 
 
bf5af54
 
 
 
 
 
 
 
 
 
 
8f996bb
bf5af54
8f996bb
 
bf5af54
8f996bb
bf5af54
8f996bb
bf5af54
 
 
 
 
 
 
 
 
8f996bb
bf5af54
8f996bb
bf5af54
8f996bb
bf5af54
8f996bb
 
 
bf5af54
8f996bb
 
bf5af54
8f996bb
 
 
 
 
 
bf5af54
8f996bb
bf5af54
8f996bb
bf5af54
8f996bb
bf5af54
8f996bb
 
bf5af54
 
 
 
8f996bb
 
 
 
bf5af54
8f996bb
 
bf5af54
8f996bb
 
 
 
 
bf5af54
8f996bb
 
 
 
bf5af54
 
8f996bb
 
 
bf5af54
8f996bb
bf5af54
 
 
8f996bb
bf5af54
 
8f996bb
 
 
 
 
 
 
 
 
 
 
 
bf5af54
 
8f996bb
 
bf5af54
8f996bb
 
 
 
bf5af54
8f996bb
 
bf5af54
8f996bb
bf5af54
 
8f996bb
bf5af54
 
 
 
8f996bb
 
bf5af54
8f996bb
 
bf5af54
8f996bb
 
bf5af54
8f996bb
c430d22
8f996bb
 
 
bf5af54
 
 
8f996bb
 
 
 
c430d22
bf5af54
 
8f996bb
 
 
c430d22
8f996bb
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import gradio as gr
from transformers import pipeline
from sentence_transformers import SentenceTransformer
import pandas as pd
import numpy as np
import zipfile, os, re, torch

# -----------------------------
# Load Mistral (FP16, GPU if available)
# -----------------------------
llm = pipeline(
    "text-generation",
    model="mistralai/Mistral-7B-Instruct-v0.2",
    torch_dtype=torch.float16,
    device_map="auto"
)

# -----------------------------
# Load embedding model
# -----------------------------
embedding_model = SentenceTransformer("nlpaueb/legal-bert-base-uncased")

# -----------------------------
# Extract ZIP with provincial legal texts
# -----------------------------
zip_path = "/app/provinces.zip"
extract_folder = "/app/provinces_texts"

if os.path.exists(extract_folder):
    import shutil
    shutil.rmtree(extract_folder)

with zipfile.ZipFile(zip_path, "r") as z:
    z.extractall(extract_folder)

date_pattern = re.compile(r"(\d{4}[-_]\d{2}[-_]\d{2})")

# -----------------------------
# Parse documents
# -----------------------------
def parse_metadata_and_content(raw):
    if "CONTENT:" not in raw:
        raise ValueError("Missing CONTENT block")

    header, content = raw.split("CONTENT:", 1)
    metadata = {}
    pdfs = []

    for line in header.split("\n"):
        if ":" in line and not line.startswith("-"):
            k, v = line.split(":", 1)
            metadata[k.strip().upper()] = v.strip()
        elif line.strip().startswith("-"):
            pdfs.append(line.strip())

    if pdfs:
        metadata["PDF_LINKS"] = "\n".join(pdfs)

    return metadata, content.strip()

documents = []
for root, dirs, files in os.walk(extract_folder):
    for filename in files:
        if not filename.endswith(".txt") or filename.startswith("._"):
            continue

        path = os.path.join(root, filename)
        try:
            raw = open(path, "r", encoding="latin-1").read()
            metadata, content = parse_metadata_and_content(raw)

            for p in [x.strip() for x in content.split("\n\n") if x.strip()]:
                documents.append({
                    "source_title": metadata.get("SOURCE_TITLE", "Unknown"),
                    "province": metadata.get("PROVINCE", "Unknown"),
                    "last_updated": metadata.get("LAST_UPDATED", "Unknown"),
                    "url": metadata.get("URL", "N/A"),
                    "pdf_links": metadata.get("PDF_LINKS", ""),
                    "text": p
                })

        except Exception as e:
            print("Skipped:", path, e)

print("Loaded paragraphs:", len(documents))

# -----------------------------
# Build embeddings dataframe
# -----------------------------
df = pd.DataFrame(documents)
texts = df["text"].tolist()
embeddings = embedding_model.encode(texts).astype("float16")

df["Embedding"] = list(embeddings)
print("Embedding index ready:", len(df))

# -----------------------------
# Retrieval
# -----------------------------
def retrieve_with_pandas(query, province=None, top_k=2):
    query_emb = embedding_model.encode([query])[0]

    subset = df if province is None else df[df["province"] == province]

    subset = subset.copy()
    subset["Similarity"] = subset["Embedding"].apply(
        lambda x: np.dot(query_emb, x) /
                  (np.linalg.norm(query_emb) * np.linalg.norm(x))
    )

    return subset.sort_values("Similarity", ascending=False).head(top_k)

# -----------------------------
# Province detection
# -----------------------------
def detect_province(q):
    provinces = {
        "yukon": "Yukon", "alberta": "Alberta", "bc": "British Columbia",
        "british columbia": "British Columbia", "manitoba": "Manitoba",
        "newfoundland": "Newfoundland and Labrador",
        "saskatchewan": "Saskatchewan", "sask": "Saskatchewan",
        "ontario": "Ontario", "pei": "Prince Edward Island",
        "quebec": "Quebec", "new brunswick": "New Brunswick",
        "nova scotia": "Nova Scotia", "nunavut": "Nunavut",
        "northwest territories": "Northwest Territories"
    }
    q = q.lower()
    for key, prov in provinces.items():
        if key in q:
            return prov
    return None

# -----------------------------
# Filters
# -----------------------------
def is_disallowed(q):
    banned = ["kill", "suicide", "bomb", "weapon", "harm yourself"]
    return any(b in q.lower() for b in banned)

def is_off_topic(q):
    keys = ["tenant","landlord","rent","evict","lease","repair","notice","unit"]
    return not any(k in q.lower() for k in keys)

# -----------------------------
# Intro (sent once)
# -----------------------------
INTRO = (
    "Hi! I'm a Canadian rental housing assistant. I help summarize and explain "
    "information from Residential Tenancies Acts across Canada.\n\n"
    "**Note:** I'm not a lawyer — this is not legal advice.\n\n"
)

# -----------------------------
# RAG Generation
# -----------------------------
def generate_with_rag(query):
    if is_disallowed(query):
        return "Sorry — I can’t help with harmful topics."

    if is_off_topic(query):
        return "Sorry — I only answer questions about Canadian tenancy law."

    prov = detect_province(query)
    docs = retrieve_with_pandas(query, province=prov, top_k=2)

    if len(docs) == 0:
        return "I couldn’t find anything relevant in the tenancy database."

    context = " ".join(docs["text"].tolist())

    prompt = f"""
Use only the context below. Do NOT invent laws.

Context:
{context}

Question:
{query}

Answer conversationally:
"""

    out = llm(prompt, max_new_tokens=150)[0]["generated_text"]
    answer = out.split("Answer conversationally:", 1)[-1].strip()

    return answer

# -----------------------------
# Gradio Chat (Intro only once)
# -----------------------------
def start_chat():
    return [(None, INTRO)]

def respond(msg, history):
    answer = generate_with_rag(msg)
    history.append((msg, answer))
    return history

with gr.Blocks() as demo:
    chatbot = gr.Chatbot(value=start_chat())
    inp = gr.Textbox(label="Ask a question:")

    inp.submit(respond, [inp, chatbot], chatbot)

demo.launch(share=True)