deepthi6 commited on
Commit
15bcaa0
Β·
verified Β·
1 Parent(s): e54e9ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +191 -144
app.py CHANGED
@@ -1,178 +1,225 @@
1
  import streamlit as st
2
-
3
- st.set_page_config(
4
- page_title="ClauseWise β€” NDA Legal Assistant",
5
- page_icon="πŸ“„",
6
- layout="wide"
7
- )
8
- import os
9
- import json
10
- from pypdf import PdfReader
11
  from transformers import AutoTokenizer, AutoModelForCausalLM
12
- from multilingual import UI_TEXT, translate_text
 
 
13
  from util import (
14
  extract_text_from_pdf,
 
 
 
15
  split_into_clauses,
16
  simplify_clause,
17
  extract_entities,
18
- generate_risks,
19
- fairness_score,
20
- alternative_clauses
21
  )
22
 
23
- # ------------------------------------------------------------
24
- # βœ… Load Local Model (DistilGPT2)
25
- # ------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
26
  @st.cache_resource
27
- def load_model():
28
  tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
29
  model = AutoModelForCausalLM.from_pretrained("distilgpt2")
30
  return tokenizer, model
31
 
32
- tokenizer, model = load_model()
33
 
34
- # ------------------------------------------------------------
35
- # βœ… Chatbot Response
36
- # ------------------------------------------------------------
37
- def chat_response(prompt):
 
 
 
38
  inputs = tokenizer.encode(prompt, return_tensors="pt")
39
- outputs = model.generate(inputs, max_length=180, num_return_sequences=1)
40
- text = tokenizer.decode(outputs[0], skip_special_tokens=True)
41
- return text
 
 
 
 
42
 
43
- # ------------------------------------------------------------
44
- # βœ… Streamlit Layout
45
- # ------------------------------------------------------------
46
- st.set_page_config(page_title="ClauseWise", layout="wide")
47
 
48
- # ------------------------------------------------------------
49
- # βœ… Session State
50
- # ------------------------------------------------------------
 
 
 
 
 
 
51
  if "chat_history" not in st.session_state:
52
  st.session_state.chat_history = []
53
 
54
- # ------------------------------------------------------------
55
- # βœ… Language Selector
56
- # ------------------------------------------------------------
57
- lang = st.selectbox(
58
- "🌐 Select Language",
 
 
59
  ["en", "hi", "ta", "te", "kn"],
60
- format_func=lambda x: {
61
- "en": "English",
62
- "hi": "Hindi",
63
- "ta": "Tamil",
64
- "te": "Telugu",
65
- "kn": "Kannada"
66
- }[x]
67
  )
68
 
69
- st.markdown("### **πŸ“„ ClauseWise β€” Legal Document Assistant**")
70
- st.info("Educational information only. Not legal advice.")
71
 
72
- # ------------------------------------------------------------
73
- # βœ… File Upload
74
- # ------------------------------------------------------------
75
- st.markdown(f"### {UI_TEXT['upload_title'][lang]}")
 
 
 
 
 
 
 
76
 
77
  uploaded_file = st.file_uploader(
78
- UI_TEXT["upload_instruction"][lang],
79
  type=["pdf", "txt", "docx"]
80
  )
81
 
82
- text_data = ""
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- if uploaded_file is not None:
85
- # βœ… Extract text depending on file type
86
- if uploaded_file.type == "application/pdf":
87
- text_data = extract_text_from_pdf(uploaded_file)
88
- elif uploaded_file.type == "text/plain":
89
- text_data = uploaded_file.read().decode("utf-8")
90
- elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
91
- import docx
92
- doc = docx.Document(uploaded_file)
93
- text_data = "\n".join([p.text for p in doc.paragraphs])
94
 
95
  # βœ… NDA Detection
96
- if "confidential" not in text_data.lower():
97
- st.error(UI_TEXT["error_not_nda"][lang])
98
  else:
99
- st.success(UI_TEXT["success_nda"][lang])
100
-
101
- # ------------------------------------------------------------
102
- # βœ… Document Tabs
103
- # ------------------------------------------------------------
104
- tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs([
105
- UI_TEXT["tab_clauses"][lang],
106
- UI_TEXT["tab_risks"][lang],
107
- UI_TEXT["tab_fairness"][lang],
108
- UI_TEXT["tab_entities"][lang],
109
- UI_TEXT["tab_alternatives"][lang],
110
- UI_TEXT["tab_chat"][lang]
111
- ])
112
-
113
- clauses = split_into_clauses(text_data)
114
-
115
- # ------------------------------------------------------------
116
- # βœ… Clause Simplification
117
- # ------------------------------------------------------------
118
- with tab1:
119
- st.subheader(UI_TEXT["clause_simplify"][lang])
120
- mode = st.selectbox(
121
- UI_TEXT["choose_mode"][lang],
122
- ["ELI5", "Simple", "Professional"]
123
- )
124
- for cl in clauses:
125
- st.markdown(f"### Clause:\n{cl}")
126
- st.markdown("**Explanation:**")
127
- st.write(simplify_clause(cl, mode))
128
-
129
- # ------------------------------------------------------------
130
- # βœ… Risk Analysis
131
- # ------------------------------------------------------------
132
- with tab2:
133
- st.subheader(UI_TEXT["risk_title"][lang])
134
- risks = generate_risks(text_data)
135
- for r in risks:
136
- st.warning(f"⚠️ {r}")
137
-
138
- # ------------------------------------------------------------
139
- # βœ… Fairness Meter
140
- # ------------------------------------------------------------
141
- with tab3:
142
- st.subheader(UI_TEXT["fairness_title"][lang])
143
- emp, comp = fairness_score(text_data)
144
- st.write(f"**{UI_TEXT['your_position'][lang]}:** {emp}")
145
- st.write(f"**{UI_TEXT['company_position'][lang]}:** {comp}")
146
-
147
- # ------------------------------------------------------------
148
- # βœ… Entity Extraction
149
- # ------------------------------------------------------------
150
- with tab4:
151
- st.subheader(UI_TEXT["entities_title"][lang])
152
- ents = extract_entities(text_data)
153
- st.json(ents)
154
-
155
- # ------------------------------------------------------------
156
- # βœ… Alternative Clauses
157
- # ------------------------------------------------------------
158
- with tab5:
159
- st.subheader(UI_TEXT["alt_title"][lang])
160
- for cl in clauses:
161
- st.write(f"πŸ“„ **Original:** {cl}")
162
- st.write(f"βœ… **Alternative:** {alternative_clauses(cl)}")
163
-
164
- # ------------------------------------------------------------
165
- # βœ… Legal Chat Assistant
166
- # ------------------------------------------------------------
167
- with tab6:
168
- st.subheader(UI_TEXT["chat_title"][lang])
169
- user_input = st.text_input(UI_TEXT["chat_placeholder"][lang])
170
-
171
- if st.button("Send"):
172
- if user_input.strip():
173
- reply = chat_response(user_input)
174
- st.session_state.chat_history.append(("You", user_input))
175
- st.session_state.chat_history.append(("ClauseWise", reply))
176
-
177
- for sender, msg in st.session_state.chat_history:
178
- st.write(f"**{sender}:** {msg}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # Import utility functions
6
  from util import (
7
  extract_text_from_pdf,
8
+ extract_text_from_txt,
9
+ extract_text_from_docx,
10
+ detect_if_nda,
11
  split_into_clauses,
12
  simplify_clause,
13
  extract_entities,
14
+ extract_top_risks,
15
+ compute_fairness,
16
+ get_alternative_clauses
17
  )
18
 
19
+ # Multilingual UI
20
+ from multilingual import UI_TEXT, translate_text
21
+
22
+
23
+ # -----------------------------------
24
+ # βœ… INITIAL STREAMLIT PAGE CONFIG
25
+ # -----------------------------------
26
+ st.set_page_config(page_title="ClauseWise", layout="wide")
27
+
28
+
29
+ # -----------------------------------
30
+ # βœ… LOAD LOCAL SMALL MODEL (HF SAFE)
31
+ # -----------------------------------
32
  @st.cache_resource
33
+ def load_llm():
34
  tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
35
  model = AutoModelForCausalLM.from_pretrained("distilgpt2")
36
  return tokenizer, model
37
 
 
38
 
39
+ tokenizer, model = load_llm()
40
+
41
+
42
+ # -----------------------------------
43
+ # βœ… LLM GENERATION FUNCTION
44
+ # -----------------------------------
45
+ def chat_ai(prompt):
46
  inputs = tokenizer.encode(prompt, return_tensors="pt")
47
+ outputs = model.generate(
48
+ inputs,
49
+ max_new_tokens=150,
50
+ temperature=0.7,
51
+ pad_token_id=tokenizer.eos_token_id
52
+ )
53
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
54
 
 
 
 
 
55
 
56
+ # -----------------------------------
57
+ # βœ… SESSION STATE
58
+ # -----------------------------------
59
+ if "language" not in st.session_state:
60
+ st.session_state.language = "en"
61
+
62
+ if "uploaded_text" not in st.session_state:
63
+ st.session_state.uploaded_text = ""
64
+
65
  if "chat_history" not in st.session_state:
66
  st.session_state.chat_history = []
67
 
68
+
69
+ # -----------------------------------
70
+ # βœ… LANGUAGE SELECTION
71
+ # -----------------------------------
72
+ st.sidebar.title("🌐 Language")
73
+ lang = st.sidebar.selectbox(
74
+ "Choose your language",
75
  ["en", "hi", "ta", "te", "kn"],
76
+ index=["en", "hi", "ta", "te", "kn"].index(st.session_state.language)
 
 
 
 
 
 
77
  )
78
 
79
+ st.session_state.language = lang
80
+ T = UI_TEXT[lang]
81
 
82
+
83
+ # -----------------------------------
84
+ # βœ… PAGE TITLE
85
+ # -----------------------------------
86
+ st.title("πŸ“ ClauseWise β€” Multilingual NDA Legal Assistant")
87
+
88
+
89
+ # -----------------------------------
90
+ # βœ… FILE UPLOAD SECTION
91
+ # -----------------------------------
92
+ st.header(T["upload_section"])
93
 
94
  uploaded_file = st.file_uploader(
95
+ T["upload_instruction"],
96
  type=["pdf", "txt", "docx"]
97
  )
98
 
99
+ if uploaded_file:
100
+ file_ext = uploaded_file.name.split(".")[-1].lower()
101
+
102
+ if file_ext == "pdf":
103
+ text = extract_text_from_pdf(uploaded_file)
104
+ elif file_ext == "txt":
105
+ text = extract_text_from_txt(uploaded_file)
106
+ elif file_ext == "docx":
107
+ text = extract_text_from_docx(uploaded_file)
108
+ else:
109
+ st.error(T["unsupported"])
110
+ st.stop()
111
 
112
+ st.session_state.uploaded_text = text
 
 
 
 
 
 
 
 
 
113
 
114
  # βœ… NDA Detection
115
+ if detect_if_nda(text):
116
+ st.success(T["nda_detected"])
117
  else:
118
+ st.warning(T["not_nda"])
119
+
120
+
121
+ # =====================================================================
122
+ # βœ… TABS
123
+ # =====================================================================
124
+ if st.session_state.uploaded_text.strip():
125
+ tabs = st.tabs([
126
+ T["tab_simplify"],
127
+ T["tab_risks"],
128
+ T["tab_fairness"],
129
+ T["tab_entities"],
130
+ T["tab_alternatives"],
131
+ T["tab_chat"]
132
+ ])
133
+
134
+ text = st.session_state.uploaded_text
135
+
136
+ # -----------------------------------------------------------
137
+ # βœ… TAB 1 β€” Clause Simplification
138
+ # -----------------------------------------------------------
139
+ with tabs[0]:
140
+ st.subheader(T["simplify_title"])
141
+
142
+ mode = st.selectbox(
143
+ T["choose_simplify_mode"],
144
+ ["simple", "eli5", "pro"],
145
+ format_func=lambda m: {
146
+ "simple": T["simple"],
147
+ "eli5": T["eli5"],
148
+ "pro": T["pro"]
149
+ }[m]
150
+ )
151
+
152
+ clauses = split_into_clauses(text)
153
+
154
+ for c in clauses:
155
+ st.write("### πŸ“Œ " + T["clause"])
156
+ st.write(c)
157
+
158
+ simp = simplify_clause(c, mode=mode)
159
+ st.info(simp)
160
+ st.divider()
161
+
162
+ # -----------------------------------------------------------
163
+ # βœ… TAB 2 β€” Risk Analysis
164
+ # -----------------------------------------------------------
165
+ with tabs[1]:
166
+ st.subheader(T["risk_title"])
167
+ risks = extract_top_risks(text)
168
+
169
+ for r in risks:
170
+ st.warning("⚠️ " + r)
171
+
172
+ # -----------------------------------------------------------
173
+ # βœ… TAB 3 β€” Fairness Meter
174
+ # -----------------------------------------------------------
175
+ with tabs[2]:
176
+ st.subheader(T["fairness_title"])
177
+
178
+ fairness = compute_fairness(text)
179
+
180
+ st.metric(label=T["your_position"], value=f"{fairness['your_position']} / 100")
181
+ st.metric(label=T["their_position"], value=f"{fairness['their_position']} / 100")
182
+
183
+ # -----------------------------------------------------------
184
+ # βœ… TAB 4 β€” Entities
185
+ # -----------------------------------------------------------
186
+ with tabs[3]:
187
+ st.subheader(T["entities_title"])
188
+
189
+ ents = extract_entities(text)
190
+ st.write("### " + T["names"])
191
+ st.write(ents["names"])
192
+
193
+ st.write("### " + T["dates"])
194
+ st.write(ents["dates"])
195
+
196
+ # -----------------------------------------------------------
197
+ # βœ… TAB 5 β€” Alternative Clauses
198
+ # -----------------------------------------------------------
199
+ with tabs[4]:
200
+ st.subheader(T["alt_title"])
201
+
202
+ alts = get_alternative_clauses(text)
203
+ for a in alts:
204
+ st.info("βœ… " + a)
205
+
206
+ # -----------------------------------------------------------
207
+ # βœ… TAB 6 β€” Legal Chat Assistant
208
+ # -----------------------------------------------------------
209
+ with tabs[5]:
210
+ st.subheader(T["chat_title"])
211
+
212
+ # show chat history
213
+ for item in st.session_state.chat_history:
214
+ st.chat_message(item["role"]).write(item["content"])
215
+
216
+ # chat input
217
+ user_input = st.chat_input(T["chat_placeholder"])
218
+ if user_input:
219
+ st.session_state.chat_history.append({"role": "user", "content": user_input})
220
+ st.chat_message("user").write(user_input)
221
+
222
+ # model response
223
+ response = chat_ai(user_input)
224
+ st.session_state.chat_history.append({"role": "assistant", "content": response})
225
+ st.chat_message("assistant").write(response)