deepthi6 commited on
Commit
55ae994
Β·
verified Β·
1 Parent(s): 6edbcf8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -79
app.py CHANGED
@@ -19,19 +19,12 @@ from io import BytesIO
19
  import spacy
20
 
21
  # -----------------------------
22
- # STREAMLIT CONFIG
23
  # -----------------------------
24
  st.set_page_config(page_title="βš–οΈ ClauseWise", page_icon="βš–οΈ", layout="wide")
25
 
26
- st.title("βš–οΈ ClauseWise: Multilingual Legal AI Assistant")
27
- st.markdown("""
28
- Simplify, translate, and analyze legal documents in **10+ languages**.
29
- ClauseWise helps you understand clauses, fairness, and contract structure β€” plus chat with an AI legal assistant.
30
- ---
31
- """)
32
-
33
  # -----------------------------
34
- # LANGUAGE SUPPORT
35
  # -----------------------------
36
  LANG_MAP = {
37
  "English": "en", "French": "fr", "Spanish": "es", "German": "de",
@@ -41,7 +34,7 @@ LANG_MAP = {
41
  LANG_NAMES = list(LANG_MAP.keys())
42
 
43
  # -----------------------------
44
- # LOAD ALL MODELS
45
  # -----------------------------
46
  @st.cache_resource
47
  def load_models():
@@ -53,7 +46,7 @@ def load_models():
53
  gen_tokenizer = AutoTokenizer.from_pretrained(gen_model_id)
54
  gen_model = AutoModelForCausalLM.from_pretrained(gen_model_id)
55
 
56
- # βœ… Safe SpaCy load
57
  try:
58
  nlp = spacy.load("en_core_web_sm")
59
  except OSError:
@@ -63,7 +56,6 @@ def load_models():
63
 
64
  classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
65
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
66
-
67
  return tokenizer_simplify, simplify_model, gen_tokenizer, gen_model, nlp, classifier, summarizer
68
 
69
  tokenizer_simplify, simplify_model, gen_tokenizer, gen_model, nlp, classifier, summarizer = load_models()
@@ -85,9 +77,9 @@ def extract_text(file):
85
  if name.endswith(".pdf"):
86
  reader = PdfReader(tmp_path)
87
  for page in reader.pages:
88
- page_text = page.extract_text()
89
- if page_text:
90
- text += page_text + "\n"
91
  elif name.endswith(".docx"):
92
  doc = Document(tmp_path)
93
  text = "\n".join([p.text for p in doc.paragraphs])
@@ -134,7 +126,7 @@ def clause_simplification(text, mode):
134
  def fairness_score_visual(text, lang):
135
  pos = len(re.findall(r"(mutual|both parties|shared)", text, re.I))
136
  neg = len(re.findall(r"(sole|unilateral|exclusive right)", text, re.I))
137
- score = max(0, min(100, 70 + pos - 2*neg))
138
 
139
  st.subheader("βš–οΈ Fairness Balance Meter")
140
  fairness_df = pd.DataFrame({
@@ -153,73 +145,86 @@ def chat_response(prompt, lang):
153
  return translate_text(response, lang)
154
 
155
  # -----------------------------
156
- # APP INTERFACE
157
  # -----------------------------
158
- tab1, tab2, tab3, tab4 = st.tabs(["πŸ“„ Analyzer", "🌐 Translate & Audio", "πŸ’¬ Chatbot", "ℹ️ About"])
159
-
160
- # TAB 1: ANALYZER
161
- with tab1:
162
- st.subheader("πŸ“ Upload or Paste Document")
163
- lang = st.selectbox("Select Language:", LANG_NAMES, index=0)
164
- file = st.file_uploader("Upload a Legal Document (PDF/DOCX/TXT)", type=["pdf", "docx", "txt"])
165
- text_input = st.text_area("Or Paste Text Here:", height=200)
166
-
167
- if file or text_input:
168
- text = extract_text(file) if file else text_input
169
-
170
- mode = st.radio("Simplify Mode", ["Explain like I'm 5", "Simplified", "Professional"])
171
-
172
- if st.button("🧾 Simplify Clauses"):
173
- with st.spinner("Simplifying..."):
174
- simplified = clause_simplification(text, mode)
175
- translated = translate_text(simplified, lang)
176
- st.success(translated)
177
- audio_data = text_to_speech(translated, lang)
178
- if audio_data:
179
- st.audio(audio_data, format="audio/mp3")
180
 
181
- if st.button("βš–οΈ Fairness Analysis"):
182
- fairness_score_visual(text, lang)
183
-
184
- # TAB 2: TRANSLATION + AUDIO
185
- with tab2:
186
- st.subheader("🌐 Translate & Listen")
187
- text_input = st.text_area("Enter text:", height=200)
188
- lang = st.selectbox("Translate to:", LANG_NAMES, index=4)
189
-
190
- if st.button("Translate"):
191
- translated = translate_text(text_input, lang)
192
- st.success(translated)
193
- if st.button("🎧 Generate Audio"):
194
- audio_data = text_to_speech(text_input, lang)
195
- if audio_data:
196
- st.audio(audio_data, format="audio/mp3")
197
-
198
- # TAB 3: CHATBOT
199
- with tab3:
200
- st.subheader("πŸ’¬ Chat with ClauseWise (Multilingual)")
201
- lang = st.selectbox("Chat Language:", LANG_NAMES, index=4)
202
- query = st.text_area("Ask about clauses, fairness, or legal meaning:", height=150)
203
- if st.button("Ask"):
204
- with st.spinner("Thinking..."):
205
- response = chat_response(f"You are a legal assistant. Answer helpfully: {query}", lang)
206
- st.success(response)
207
- audio_data = text_to_speech(response, lang)
 
 
 
 
 
 
 
 
 
 
208
  if audio_data:
209
  st.audio(audio_data, format="audio/mp3")
210
 
211
- # TAB 4: ABOUT
212
- with tab4:
213
- st.markdown("""
214
- ### βš–οΈ About ClauseWise
215
- ClauseWise is a multilingual AI-powered legal assistant that helps users:
216
- - Simplify complex clauses
217
- - Translate and listen in 10+ languages
218
- - Assess fairness visually
219
- - Chat interactively
 
 
 
220
 
221
- **Languages Supported:**
222
- English, French, Spanish, German, Hindi, Tamil, Telugu, Kannada, Marathi, Gujarati, Bengali
 
 
 
 
 
 
 
223
 
224
- **Disclaimer:** Educational purposes only, not legal advice.
225
- """)
 
 
 
 
 
 
 
 
 
 
19
  import spacy
20
 
21
  # -----------------------------
22
+ # STREAMLIT PAGE CONFIG
23
  # -----------------------------
24
  st.set_page_config(page_title="βš–οΈ ClauseWise", page_icon="βš–οΈ", layout="wide")
25
 
 
 
 
 
 
 
 
26
  # -----------------------------
27
+ # LANGUAGE MAP
28
  # -----------------------------
29
  LANG_MAP = {
30
  "English": "en", "French": "fr", "Spanish": "es", "German": "de",
 
34
  LANG_NAMES = list(LANG_MAP.keys())
35
 
36
  # -----------------------------
37
+ # MODEL LOADING (with caching)
38
  # -----------------------------
39
  @st.cache_resource
40
  def load_models():
 
46
  gen_tokenizer = AutoTokenizer.from_pretrained(gen_model_id)
47
  gen_model = AutoModelForCausalLM.from_pretrained(gen_model_id)
48
 
49
+ # βœ… Auto-download SpaCy if missing
50
  try:
51
  nlp = spacy.load("en_core_web_sm")
52
  except OSError:
 
56
 
57
  classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
58
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
59
  return tokenizer_simplify, simplify_model, gen_tokenizer, gen_model, nlp, classifier, summarizer
60
 
61
  tokenizer_simplify, simplify_model, gen_tokenizer, gen_model, nlp, classifier, summarizer = load_models()
 
77
  if name.endswith(".pdf"):
78
  reader = PdfReader(tmp_path)
79
  for page in reader.pages:
80
+ t = page.extract_text()
81
+ if t:
82
+ text += t + "\n"
83
  elif name.endswith(".docx"):
84
  doc = Document(tmp_path)
85
  text = "\n".join([p.text for p in doc.paragraphs])
 
126
  def fairness_score_visual(text, lang):
127
  pos = len(re.findall(r"(mutual|both parties|shared)", text, re.I))
128
  neg = len(re.findall(r"(sole|unilateral|exclusive right)", text, re.I))
129
+ score = max(0, min(100, 70 + pos - 2 * neg))
130
 
131
  st.subheader("βš–οΈ Fairness Balance Meter")
132
  fairness_df = pd.DataFrame({
 
145
  return translate_text(response, lang)
146
 
147
  # -----------------------------
148
+ # MAIN STREAMLIT APP FUNCTION
149
  # -----------------------------
150
+ def main():
151
+ st.title("βš–οΈ ClauseWise: Multilingual Legal AI Assistant")
152
+ st.markdown("""
153
+ **Simplify**, **translate**, and **analyze** legal documents with AI β€” in your language.
154
+ ---
155
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
+ tab1, tab2, tab3, tab4 = st.tabs(["πŸ“„ Analyzer", "🌐 Translate & Audio", "πŸ’¬ Chatbot", "ℹ️ About"])
158
+
159
+ # TAB 1: ANALYZER
160
+ with tab1:
161
+ st.subheader("πŸ“ Upload or Paste Legal Document")
162
+ lang = st.selectbox("Select Language:", LANG_NAMES, index=0)
163
+ file = st.file_uploader("Upload a Legal Document (PDF/DOCX/TXT)", type=["pdf", "docx", "txt"])
164
+ text_input = st.text_area("Or Paste Text Here:", height=200)
165
+
166
+ if file or text_input:
167
+ text = extract_text(file) if file else text_input
168
+
169
+ mode = st.radio("Simplify Mode", ["Explain like I'm 5", "Simplified", "Professional"])
170
+
171
+ if st.button("🧾 Simplify Clauses"):
172
+ with st.spinner("Simplifying..."):
173
+ simplified = clause_simplification(text, mode)
174
+ translated = translate_text(simplified, lang)
175
+ st.success(translated)
176
+ audio_data = text_to_speech(translated, lang)
177
+ if audio_data:
178
+ st.audio(audio_data, format="audio/mp3")
179
+
180
+ if st.button("βš–οΈ Fairness Analysis"):
181
+ fairness_score_visual(text, lang)
182
+
183
+ # TAB 2: TRANSLATION + AUDIO
184
+ with tab2:
185
+ st.subheader("🌐 Translate & Listen")
186
+ text_input = st.text_area("Enter text:", height=200)
187
+ lang = st.selectbox("Translate to:", LANG_NAMES, index=4)
188
+
189
+ if st.button("Translate"):
190
+ translated = translate_text(text_input, lang)
191
+ st.success(translated)
192
+ if st.button("🎧 Generate Audio"):
193
+ audio_data = text_to_speech(text_input, lang)
194
  if audio_data:
195
  st.audio(audio_data, format="audio/mp3")
196
 
197
+ # TAB 3: CHATBOT
198
+ with tab3:
199
+ st.subheader("πŸ’¬ Chat with ClauseWise (Multilingual)")
200
+ lang = st.selectbox("Chat Language:", LANG_NAMES, index=4)
201
+ query = st.text_area("Ask about clauses, fairness, or legal meaning:", height=150)
202
+ if st.button("Ask"):
203
+ with st.spinner("Thinking..."):
204
+ response = chat_response(f"You are a legal assistant. Answer helpfully: {query}", lang)
205
+ st.success(response)
206
+ audio_data = text_to_speech(response, lang)
207
+ if audio_data:
208
+ st.audio(audio_data, format="audio/mp3")
209
 
210
+ # TAB 4: ABOUT
211
+ with tab4:
212
+ st.markdown("""
213
+ ### βš–οΈ About ClauseWise
214
+ ClauseWise is a multilingual AI-powered legal assistant that helps users:
215
+ - Simplify complex clauses
216
+ - Translate and listen in 10+ languages
217
+ - Assess fairness visually
218
+ - Chat interactively
219
 
220
+ **Languages Supported:**
221
+ English, French, Spanish, German, Hindi, Tamil, Telugu, Kannada, Marathi, Gujarati, Bengali
222
+
223
+ **Disclaimer:** Educational purposes only, not legal advice.
224
+ """)
225
+
226
+ # -----------------------------
227
+ # RUN STREAMLIT APP SAFELY
228
+ # -----------------------------
229
+ if __name__ == "__main__":
230
+ main()