-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
228 lines (162 loc) · 9.12 KB
/
main.py
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
import json
import streamlit as st
import pandas as pd
from PyPDF2 import PdfReader
from openai import OpenAI
import os
from pymongo import MongoClient
import jwt
import datetime
import tempfile
import fitz
openai = OpenAI(api_key=st.secrets["openai"])
#create a connection to the database
client = MongoClient(st.secrets["connection_string"])
db = client["KSP"]
user_table = db["User"]
token_table = db["Token"]
# Generate JWT token
def generate_token(user_id):
expiry = datetime.datetime.utcnow() + datetime.timedelta(days=1)
payload = {
"user_id": user_id,
"exp": expiry
}
token = jwt.encode(payload, key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', algorithm="HS256")
return token, expiry
# Validate JWT token
def validate_token(token):
try:
payload = jwt.decode(token, key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', algorithms=["HS256"])
return payload["user_id"], payload["exp"]
except jwt.ExpiredSignatureError:
return None, "Token expired"
except jwt.InvalidTokenError:
return None, "Invalid token"
# Save token to database
def save_token(user_id, token, expiry):
token_table.insert_one({
"user_id": user_id,
"token": token,
"expiry": expiry
})
# Check login credentials
def check_login(username, password):
user_table.find_one({"username": username, "password": password})
return user_table.find_one({"username": username, "password": password})
def read_file(pdf_file):
text = ""
pdf_reader = PdfReader(pdf_file)
for page in pdf_reader.pages:
text += page.extract_text()
return extract_entities(text)
def extract_entities(text):
prompt = "Without having any comment on this , I have FIR file in EngLish and Kannada Languages, Analyse the pdf in both languages to answer my query, in this all the personal identifiers like names, addresses in exact word format\n\n" + \
str(text)+"\n\n. I need result in a dict format with key and value , key describes about either it is name , address with values. Can you provide me the result in this format ?\n\n"
completion = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": prompt,
},
],
)
return completion.choices[0].message.content
def search_replace(path, text):
doc = fitz.open(path)
for page in doc:
instances = page.search_for(text)
for inst in instances:
page.add_redact_annot(inst, fill=(0, 0, 0))
page.apply_redactions()
directory = os.path.dirname(path)
redacted_pdf_path = os.path.join(directory, 'redacted_document.pdf')
doc.save(redacted_pdf_path)
doc.close()
#download the redacted file to the local system
st.download_button(label="Download Redacted PDF", data=open(redacted_pdf_path, 'rb').read(), file_name='redacted_document.pdf', mime='application/pdf')
# st.success(f"Redacted PDF saved to: {redacted_pdf_path}")
st.success("File redacted successfully")
def main():
st.title("KSP Datathon 24")
tab1, tab2, tab3, tab4 = st.tabs(["#### Home", "#### Login", "#### Redact", "#### About"])
with tab1:
st.write("""
Welcome to the FIR Redactor, an advanced tool designed to protect sensitive information in First Information Reports (FIRs) for the Karnataka Police Department. Our application harnesses the power of AI and cutting-edge PDF processing technology to identify and redact personal identifiers, ensuring privacy and compliance with legal standards.
In the digital age, the privacy and security of personal information have become paramount. FIRs often contain sensitive data, including names, addresses, and other personal identifiers. Unauthorized disclosure of this information can lead to privacy violations and potentially compromise the safety of individuals. Ensuring that sensitive data is redacted before sharing or publishing FIRs is crucial for maintaining public trust and compliance with privacy laws.
### Benefits for the Karnataka Police Department:
- **Enhanced Privacy Protection**
- **Compliance with Legal Standards**
- **Efficiency and Accuracy**
- **Improved Public Trust**
""")
with tab2:
username = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.button("Login"):
user = check_login(username, password)
if user:
token, expiry = generate_token(user["username"])
save_token(user["_id"], token, expiry)
st.success("Login successful!")
st.session_state["token"] = token
else:
st.error("Invalid username or password")
with tab3:
if "token" in st.session_state:
user_id, error = validate_token(st.session_state["token"])
if user_id:
st.write("Upload a FIR document")
uploaded_file = st.file_uploader("Upload PDF", type=['pdf'])
if uploaded_file is not None:
if st.button("AI suggestion"):
res_str = read_file(uploaded_file)
res_dict = json.loads(res_str)
st.write("AI Suggested replacements:")
for key in list(res_dict.keys()):
if not res_dict[key]:
del res_dict[key]
df = pd.DataFrame(res_dict.items(), columns=["Entity", "Value"])
st.write(df)
# path = st.text_input("Input the path of file", key="path")
text = st.text_input("Enter the text to Redact in the file", key="text")
if st.button("Redact"):
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(uploaded_file.getvalue())
uploaded_file = temp_file.name
search_replace(uploaded_file, text)
else:
st.error(error)
# st.experimental_rerun()
else:
st.error("Please login to access this page.")
# st.experimental_rerun()
with tab4:
st.write("""
#### FIR Redactor for Karnataka Police Department
Welcome to the FIR Redactor, a powerful and intuitive tool designed specifically for the Karnataka Police Department. Our application aims to enhance the privacy and security of sensitive information within First Information Reports (FIRs). This tool leverages advanced language models and PDF processing libraries to identify and redact personal identifiers from FIR documents written in both English and Kannada.
#### Key Features:
1. **Dual Language Support:**
- Handles FIRs in both English and Kannada, ensuring comprehensive coverage for diverse linguistic needs.
2. **AI-Powered Entity Extraction:**
- Utilizes the cutting-edge LLM model to accurately extract personal identifiers such as names, addresses, and other sensitive information from FIR texts.
3. **Automated Redaction:**
- Employs PyMuPDF to seamlessly redact identified sensitive information from PDF documents, replacing it with blacked-out text to maintain confidentiality.
4. **User-Friendly Interface:**
- Provides an intuitive and easy-to-navigate interface for users, including tabs for Home, Login, Redaction, and comprehensive information about the tool.
#### How It Works:
- **Upload FIR Documents:**
- Users can upload FIR PDFs through a simple file uploader. The application reads and extracts text from these documents for further processing.
- **AI-Suggested Redactions:**
- After analyzing the text, the application suggests redactions by identifying sensitive entities. These suggestions are presented in a structured format for user review.
- **Redaction Execution:**
- Users can input the specific text they wish to redact, and the tool will automatically find and redact these instances within the document. The redacted PDF is then saved securely.
#### Security and Privacy:
The FIR Redactor ensures that the privacy of individuals involved in FIRs is protected by securely handling all documents and utilizing state-of-the-art redaction techniques. All data processing is performed with strict adherence to security protocols to prevent unauthorized access and maintain the integrity of sensitive information.
#### Intended Use:
This tool is designed to assist the Karnataka Police Department in safeguarding personal information within FIRs, facilitating compliance with privacy regulations and enhancing the trust and confidence of the public.
### Developed by: Rubix Cube
""")
if __name__ == "__main__":
main()