File size: 10,638 Bytes
ab8e415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""app.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1Bli_bGuux1CJr22uJYxsoLSQkr5LjXvD
"""

import random
import pandas as pd

# Complaint categories with 10โ€“12 synonym-rich templates each (no {} placeholders now)
categories = {
    "Garbage": [
        "Garbage not collected",
        "Trash piled up",
        "Waste scattered everywhere",
        "Debris dumped carelessly",
        "Rubbish overflowing",
        "Litter causing bad smell",
        "Uncollected scrap lying around",
        "Filth spread all over",
        "Junk thrown carelessly",
        "Refuse dumped openly",
        "Garbage heap blocking the way",
        "Dumping ground overflowing"
    ],
    "Water": [
        "Water pipeline leaking",
        "No water supply",
        "Contaminated tap water",
        "Low water pressure",
        "Water tanker not arrived",
        "Sewage water overflow",
        "Drainage issue",
        "Sewer blockage reported",
        "Flooding due to heavy rain",
        "Water logging problem",
        "Dirty water flowing",
        "Burst pipeline issue"
    ],
    "Roads": [
        "Big pothole on the road",
        "Damaged road surface",
        "Cracks on the road",
        "Uneven surface making driving difficult",
        "Broken speed breaker",
        "Debris blocking the road",
        "Manhole cover missing",
        "Broken pavement",
        "Damaged footpath",
        "Road erosion reported",
        "Construction waste dumped on road",
        "Street blocked due to cave-in"
    ],
    "Electricity": [
        # General electricity
        "Frequent power cuts",
        "Load shedding problem",
        "Voltage fluctuation issue",
        "Transformer not working",
        "Wire hanging dangerously",
        "No electricity supply",
        "Complete blackout",
        "Short circuit issue reported",
        "Electrical failure in houses",
        "Electric spark observed",
        # Streetlight related
        "Streetlight not working",
        "Streetlight bulb fused",
        "Dark area due to broken streetlight",
        "Streetlight flickering",
        "Streetlight pole damaged",
        "Entire lane dark without lights"
    ]
}

# Number of complaints per category (balanced dataset)
num_samples = 300  # per category
data = []

for category, templates in categories.items():
    for _ in range(num_samples):
        template = random.choice(templates)
        data.append({
            "Complaint Text": template,
            "Category": category
        })

# Convert to DataFrame
df = pd.DataFrame(data)

# Shuffle
df = df.sample(frac=1).reset_index(drop=True)

# Save CSV
df.to_csv("synthetic_civic_complaints_no_location.csv", index=False, encoding="utf-8")

print("โœ… Final synonym-rich dataset created: synthetic_civic_complaints_no_location.csv")
display(df.head())

import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score, learning_curve
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix, ConfusionMatrixDisplay
import matplotlib.pyplot as plt
import numpy as np

# 1. Load dataset
df = pd.read_csv("synthetic_civic_complaints_rich.csv")

# ๐Ÿ”น Make all complaint text lowercase (case-insensitive)
df["Complaint Text"] = df["Complaint Text"].str.lower()

# 2. Train-test split
X = df["Complaint Text"]
y = df["Category"]

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# 3. Vectorizer + classifier
vectorizer = TfidfVectorizer(stop_words="english", max_features=5000)
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)

clf = LogisticRegression(max_iter=500)
clf.fit(X_train_vec, y_train)

# 4. Evaluate
y_pred = clf.predict(X_test_vec)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))

# 5. Confusion Matrix
labels = clf.classes_
cm = confusion_matrix(y_test, y_pred, labels=labels)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)
fig, ax = plt.subplots(figsize=(6, 5))
disp.plot(ax=ax, cmap="Blues", values_format="d")
plt.show()

# 6. Cross-validation
from sklearn.pipeline import Pipeline
pipe = Pipeline([
    ("tfidf", TfidfVectorizer(stop_words="english", max_features=5000)),
    ("clf", LogisticRegression(max_iter=500))
])

scores = cross_val_score(pipe, X, y, cv=5, scoring="accuracy")
print("Cross-validation scores:", scores)
print("Mean CV Accuracy:", scores.mean())

# 7. Learning Curve
train_sizes, train_scores, val_scores = learning_curve(
    pipe, X, y, cv=5, scoring="accuracy",
    train_sizes=np.linspace(0.1, 1.0, 5)
)

train_mean = train_scores.mean(axis=1)
val_mean = val_scores.mean(axis=1)

plt.plot(train_sizes, train_mean, label="Training score")
plt.plot(train_sizes, val_mean, label="Validation score")
plt.xlabel("Training Set Size")
plt.ylabel("Accuracy")
plt.title("Learning Curve")
plt.legend()
plt.grid(True)
plt.show()

import spacy
from spacy.training.example import Example

# Create blank English pipeline
nlp = spacy.blank("en")

# Add text categorizer instead of NER
textcat = nlp.add_pipe("textcat")
textcat.add_label("Garbage")
textcat.add_label("Water")
textcat.add_label("Roads")
textcat.add_label("Electricity")

# Prepare training data
TRAIN_DATA = []
for _, row in df.iterrows():
    text = row["Complaint Text"]
    label = row["Category"]
    cats = {cat: 0.0 for cat in textcat.labels}
    cats[label] = 1.0
    TRAIN_DATA.append((text, {"cats": cats}))

# Train the text classifier
optimizer = nlp.begin_training()
for i in range(20):  # epochs
    losses = {}
    for text, annotations in TRAIN_DATA:
        doc = nlp.make_doc(text)
        example = Example.from_dict(doc, annotations)
        nlp.update([example], sgd=optimizer, losses=losses)
    print(f"Epoch {i+1}, Losses: {losses}")

# Save model
nlp.to_disk("complaint_textcat_model")
print("โœ… Text classification model saved: complaint_textcat_model")

import spacy
from spacy.training.example import Example
import random

# ๐Ÿ”น Build text classification training data
TRAIN_DATA = []
for _, row in df.iterrows():
    text = row["Complaint Text"]
    label = row["Category"]
    cats = {
        "Garbage": 0.0,
        "Water": 0.0,
        "Roads": 0.0,
        "Electricity": 0.0
    }
    cats[label] = 1.0
    TRAIN_DATA.append((text, {"cats": cats}))

# ๐Ÿ”น Create blank pipeline with text categorizer
nlp = spacy.blank("en")
textcat = nlp.add_pipe("textcat")
for label in ["Garbage", "Water", "Roads", "Electricity"]:
    textcat.add_label(label)

nlp.initialize()

# ๐Ÿ”น Train model
for itn in range(10):  # epochs
    random.shuffle(TRAIN_DATA)
    losses = {}
    for text, ann in TRAIN_DATA:
        doc = nlp.make_doc(text)
        example = Example.from_dict(doc, ann)
        nlp.update([example], losses=losses)
    print(f"Epoch {itn+1}, Losses: {losses}")

# ๐Ÿ”น Complaint prediction function
def predict_complaint(text):
    doc = nlp(text)

    # Step 1 โ†’ Category prediction
    cats = doc.cats
    category = max(cats, key=cats.get)  # pick category with highest score

    # Step 2 โ†’ Priority detection
    text_lower = text.lower()
    urgent_words = ["urgent", "dangerous", "immediately", "accident", "severe"]
    medium_words = ["not working", "overflow", "leak", "delay", "low pressure"]

    priority = "Low"
    if any(word in text_lower for word in urgent_words):
        priority = "High"
    elif any(word in text_lower for word in medium_words):
        priority = "Medium"

    return {
        "Complaint": text,
        "Predicted Category": category,
        "Priority": priority
    }

# ๐Ÿ”น Test it
print(predict_complaint("Debris dumped behind chandni chowk"))
print(predict_complaint("Streetlight not working near ChANdni chowk, its very dangerous"))

import pickle

# Wrapper so spaCy model can be pickled
class ComplaintClassifier:
    def __init__(self, nlp_model):
        self.nlp = nlp_model

    def predict(self, text):
        doc = self.nlp(text)
        cats = doc.cats
        category = max(cats, key=cats.get)

        # Priority detection
        text_lower = text.lower()
        urgent_words = ["urgent", "dangerous", "immediately", "accident", "severe"]
        medium_words = ["not working", "overflow", "leak", "delay", "low pressure"]

        priority = "Low"
        if any(word in text_lower for word in urgent_words):
            priority = "High"
        elif any(word in text_lower for word in medium_words):
            priority = "Medium"

        return {
            "Complaint": text,
            "Predicted Category": category,
            "Priority": priority
        }

# Wrap trained spaCy model
classifier = ComplaintClassifier(nlp)

# Save with pickle
with open("complaint_model.pkl", "wb") as f:
    pickle.dump(classifier, f)

print("โœ… complaint_model.pkl saved successfully")

from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
import nest_asyncio
import pickle
import spacy

# ========== Load trained model ==========
# Make sure you have already trained & saved it as complaint_model.pkl
with open("complaint_model.pkl", "rb") as f:
    nlp = pickle.load(f)

# ========== Priority detection ==========
def detect_priority(text: str) -> str:
    text_lower = text.lower()
    urgent_words = ["urgent", "dangerous", "immediately", "accident", "severe"]
    medium_words = ["not working", "overflow", "leak", "delay", "low pressure"]

    if any(word in text_lower for word in urgent_words):
        return "High"
    elif any(word in text_lower for word in medium_words):
        return "Medium"
    return "Low"

# ========== FastAPI ==========
app = FastAPI()

class ComplaintInput(BaseModel):
    text: str

@app.post("/predict")
async def predict_complaint(input_data: ComplaintInput):
    doc = nlp(input_data.text)
    cats = doc.cats
    category = max(cats, key=cats.get)
    priority = detect_priority(input_data.text)

    return {
        "Complaint": input_data.text,
        "Predicted Category": category,
        "Priority": priority,
        "Raw Scores": cats
    }

# ========== Run in Colab only ==========
if __name__ == "__main__":
    try:
        nest_asyncio.apply()
        uvicorn.run(app, host="0.0.0.0", port=7860)
    except RuntimeError:
        # In Hugging Face or when uvicorn is auto-run, we skip this
        pass