Sai809701 commited on
Commit
5d68747
·
1 Parent(s): ad25530

first commit

Browse files
BERT_Model_10000/config.json ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertForSequenceClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "classifier_dropout": null,
7
+ "dtype": "float32",
8
+ "gradient_checkpointing": false,
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 768,
12
+ "id2label": {
13
+ "0": "Adoption",
14
+ "1": "Alimony",
15
+ "2": "Child Custody",
16
+ "3": "Child Support",
17
+ "4": "Consumer Rights",
18
+ "5": "Contracts & Consent",
19
+ "6": "Court Orders",
20
+ "7": "Criminal Law",
21
+ "8": "Cyber Harassment",
22
+ "9": "Domestic Violence",
23
+ "10": "Dowry Harassment",
24
+ "11": "Education Rights",
25
+ "12": "Employment Contracts",
26
+ "13": "Equal Pay",
27
+ "14": "Harassment Online",
28
+ "15": "Healthcare Rights",
29
+ "16": "Housing Rights",
30
+ "17": "Human Trafficking",
31
+ "18": "Immigrant Rights",
32
+ "19": "Insurance",
33
+ "20": "Legal Aid",
34
+ "21": "Legal Procedures",
35
+ "22": "Maintenance",
36
+ "23": "Marriage Rights",
37
+ "24": "Maternity Leave",
38
+ "25": "Medical Negligence",
39
+ "26": "Mental Health",
40
+ "27": "Mobile Privacy",
41
+ "28": "NGO Support",
42
+ "29": "POCSO",
43
+ "30": "POSH",
44
+ "31": "Passport Issues",
45
+ "32": "Pension Rights",
46
+ "33": "Police Complaint",
47
+ "34": "Property Rights",
48
+ "35": "Registration of Marriage",
49
+ "36": "Reproductive Rights",
50
+ "37": "Reservation",
51
+ "38": "Right to Information",
52
+ "39": "Sexual Assault",
53
+ "40": "Sexual and Reproductive Health",
54
+ "41": "Special Schemes",
55
+ "42": "Stalking",
56
+ "43": "Succession Law",
57
+ "44": "Surrogacy",
58
+ "45": "Trafficking",
59
+ "46": "Travel Consent",
60
+ "47": "Witness Protection",
61
+ "48": "Workplace Benefits",
62
+ "49": "Workplace Harassment"
63
+ },
64
+ "initializer_range": 0.02,
65
+ "intermediate_size": 3072,
66
+ "label2id": {
67
+ "Adoption": 0,
68
+ "Alimony": 1,
69
+ "Child Custody": 2,
70
+ "Child Support": 3,
71
+ "Consumer Rights": 4,
72
+ "Contracts & Consent": 5,
73
+ "Court Orders": 6,
74
+ "Criminal Law": 7,
75
+ "Cyber Harassment": 8,
76
+ "Domestic Violence": 9,
77
+ "Dowry Harassment": 10,
78
+ "Education Rights": 11,
79
+ "Employment Contracts": 12,
80
+ "Equal Pay": 13,
81
+ "Harassment Online": 14,
82
+ "Healthcare Rights": 15,
83
+ "Housing Rights": 16,
84
+ "Human Trafficking": 17,
85
+ "Immigrant Rights": 18,
86
+ "Insurance": 19,
87
+ "Legal Aid": 20,
88
+ "Legal Procedures": 21,
89
+ "Maintenance": 22,
90
+ "Marriage Rights": 23,
91
+ "Maternity Leave": 24,
92
+ "Medical Negligence": 25,
93
+ "Mental Health": 26,
94
+ "Mobile Privacy": 27,
95
+ "NGO Support": 28,
96
+ "POCSO": 29,
97
+ "POSH": 30,
98
+ "Passport Issues": 31,
99
+ "Pension Rights": 32,
100
+ "Police Complaint": 33,
101
+ "Property Rights": 34,
102
+ "Registration of Marriage": 35,
103
+ "Reproductive Rights": 36,
104
+ "Reservation": 37,
105
+ "Right to Information": 38,
106
+ "Sexual Assault": 39,
107
+ "Sexual and Reproductive Health": 40,
108
+ "Special Schemes": 41,
109
+ "Stalking": 42,
110
+ "Succession Law": 43,
111
+ "Surrogacy": 44,
112
+ "Trafficking": 45,
113
+ "Travel Consent": 46,
114
+ "Witness Protection": 47,
115
+ "Workplace Benefits": 48,
116
+ "Workplace Harassment": 49
117
+ },
118
+ "layer_norm_eps": 1e-12,
119
+ "max_position_embeddings": 512,
120
+ "model_type": "bert",
121
+ "num_attention_heads": 12,
122
+ "num_hidden_layers": 12,
123
+ "pad_token_id": 0,
124
+ "position_embedding_type": "absolute",
125
+ "problem_type": "single_label_classification",
126
+ "transformers_version": "4.57.1",
127
+ "type_vocab_size": 2,
128
+ "use_cache": true,
129
+ "vocab_size": 30522
130
+ }
BERT_Model_10000/special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
BERT_Model_10000/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
BERT_Model_10000/tokenizer_config.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": false,
45
+ "cls_token": "[CLS]",
46
+ "do_lower_case": true,
47
+ "extra_special_tokens": {},
48
+ "mask_token": "[MASK]",
49
+ "model_max_length": 512,
50
+ "pad_token": "[PAD]",
51
+ "sep_token": "[SEP]",
52
+ "strip_accents": null,
53
+ "tokenize_chinese_chars": true,
54
+ "tokenizer_class": "BertTokenizer",
55
+ "unk_token": "[UNK]"
56
+ }
BERT_Model_10000/vocab.txt ADDED
The diff for this file is too large to render. See raw diff
 
Dockerfile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Start from a slim Python 3.10 base image
2
+ FROM python:3.10-slim
3
+
4
+ # Set the working directory inside the container
5
+ WORKDIR /app
6
+
7
+ # Copy the requirements file first to leverage Docker cache
8
+ COPY requirements.txt .
9
+
10
+ # Install the Python dependencies
11
+ # --no-cache-dir saves space
12
+ RUN pip install --no-cache-dir -r requirements.txt
13
+
14
+ # Copy the rest of the application files
15
+ # This includes app.py AND your model folder (bert-indian-legal-intent)
16
+ COPY . .
17
+
18
+ # Hugging Face Spaces *requires* the app to run on port 7860
19
+ EXPOSE 7860
20
+
21
+ # Command to run the FastAPI server using uvicorn
22
+ # 0.0.0.0 makes it accessible outside the container
23
+ # app:app refers to the 'app' object in the 'app.py' file
24
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import json
3
+ from fastapi import FastAPI
4
+ from pydantic import BaseModel
5
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
6
+
7
+ # --- 1. Load Model and Tokenizer ---
8
+
9
+ # Define the path to your trained model
10
+ MODEL_PATH = "./BERT_Model_10000"
11
+ # Define the path to your knowledge base
12
+ KNOWLEDGE_BASE_PATH = "womens_legal_rights_india_10000.json"
13
+
14
+ print("Loading tokenizer...")
15
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
16
+
17
+ print("Loading classification model...")
18
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
19
+
20
+ # Set device to GPU (cuda:0) if available, otherwise CPU
21
+ device = 0 if torch.cuda.is_available() else -1
22
+
23
+ print(f"Creating classification pipeline on device: {'cuda' if device == 0 else 'cpu'}...")
24
+ # Create the text-classification pipeline
25
+ classifier = pipeline(
26
+ "text-classification",
27
+ model=model,
28
+ tokenizer=tokenizer,
29
+ device=device
30
+ )
31
+ print("Classification pipeline loaded successfully.")
32
+
33
+ # --- 2. Load Knowledge Base (Answers) ---
34
+
35
+ intent_to_answer_map = {}
36
+ print(f"Loading knowledge base from: {KNOWLEDGE_BASE_PATH}")
37
+ try:
38
+ with open(KNOWLEDGE_BASE_PATH, 'r', encoding='utf-8') as f:
39
+ knowledge_base_data = json.load(f)
40
+
41
+ # Create a simple lookup map: Intent -> Answer
42
+ # This assumes the answer is the same for all questions of the same intent.
43
+ intent_to_answer_map = {
44
+ item['intent']: item['answer'] for item in knowledge_base_data
45
+ }
46
+ print(f"Knowledge base loaded with {len(intent_to_answer_map)} intent-to-answer mappings.")
47
+
48
+ except FileNotFoundError:
49
+ print(f"CRITICAL ERROR: Knowledge base file not found at {KNOWLEDGE_BASE_PATH}")
50
+ # In a real app, you might want to exit if the KB can't be loaded
51
+ except Exception as e:
52
+ print(f"Error loading knowledge base: {e}")
53
+
54
+
55
+ # --- 3. Initialize FastAPI App ---
56
+
57
+ app = FastAPI(
58
+ title="Legal Intent & Answer API",
59
+ description="API to predict the intent of a legal question and provide a suitable answer.",
60
+ version="1.1.0"
61
+ )
62
+
63
+ # --- 4. Define Request and Response Models ---
64
+
65
+ # This is what the user must send in their POST request
66
+ class Query(BaseModel):
67
+ text: str
68
+
69
+ # This is what the API will return
70
+ class PredictionResponse(BaseModel):
71
+ query: str
72
+ predicted_intent: str
73
+ confidence_score: float
74
+ answer: str
75
+
76
+ # --- 5. Define API Endpoints ---
77
+
78
+ @app.get("/")
79
+ def read_root():
80
+ """
81
+ Root endpoint for health check.
82
+ """
83
+ return {"status": "API is running",
84
+ "message": "Post to /predict with a 'text' field to get an intent and answer."}
85
+
86
+
87
+ @app.post("/predict", response_model=PredictionResponse)
88
+ def predict_intent(query: Query):
89
+ """
90
+ Predicts the intent of a given legal question and provides a suitable answer.
91
+ """
92
+ print(f"Received query: {query.text}")
93
+
94
+ # 1. Get prediction from model
95
+ model_result = classifier(query.text)[0]
96
+ predicted_intent = model_result['label']
97
+ confidence_score = model_result['score']
98
+
99
+ # 2. Retrieve answer from our knowledge base
100
+ fallback_answer = "Could not find a specific answer for this intent. Please rephrase your question or contact a legal professional for advice."
101
+
102
+ answer = intent_to_answer_map.get(predicted_intent, fallback_answer)
103
+
104
+ # 3. Return the combined response
105
+ return {
106
+ "query": query.text,
107
+ "predicted_intent": predicted_intent,
108
+ "confidence_score": confidence_score,
109
+ "answer": answer
110
+ }
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ torch
4
+ transformers
5
+ pydantic
womens_legal_rights_india_10000.json ADDED
The diff for this file is too large to render. See raw diff