akarshan11 commited on
Commit
5cd72ac
Β·
verified Β·
1 Parent(s): f990e61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -43
app.py CHANGED
@@ -10,17 +10,16 @@ import fitz # PyMuPDF
10
  import requests
11
  import uuid
12
  import io
 
13
 
14
  # Configuration
15
  MODEL_NAME = "google/gemma-2b-it"
16
  CURRENT_USER = "AkarshanGupta"
17
- CURRENT_TIME = "2025-03-23 04:53:36"
18
 
19
  # API Keys
20
  HF_TOKEN = os.getenv('HF_TOKEN')
21
  AZURE_TRANSLATION_KEY = os.getenv('AZURE_TRANSLATION_KEY')
22
- LLAMA_API_KEY = os.getenv('LLAMA_API_KEY')
23
- LLAMA_API_ENDPOINT = "https://api.llama.ai/v1/generate"
24
 
25
  class TextExtractor:
26
  @staticmethod
@@ -149,7 +148,6 @@ class LegalEaseAssistant:
149
 
150
  login(token=HF_TOKEN)
151
 
152
- # Initialize text_extractor first
153
  self.text_extractor = TextExtractor()
154
 
155
  self.tokenizer = AutoTokenizer.from_pretrained(
@@ -175,7 +173,8 @@ class LegalEaseAssistant:
175
  "simplify": f"Simplify the following legal text in clear, plain language. Provide the response as separate points:\n\n{text}\n\nSimplified explanation:",
176
  "summary": f"Provide a concise summary of the following legal document as separate key points:\n\n{text}\n\nSummary:",
177
  "key_terms": f"Identify and explain the key legal terms and obligations in this text as separate points:\n\n{text}\n\nKey Terms:",
178
- "risk": f"Perform a risk analysis on the following legal document and list each risk as a separate point:\n\n{text}\n\nRisk Assessment:"
 
179
  }
180
 
181
  prompt = task_prompts.get(task_type, f"Analyze the following text and provide points:\n\n{text}\n\nAnalysis:")
@@ -195,23 +194,54 @@ class LegalEaseAssistant:
195
  raw_response = response_parts[-1].strip() if len(response_parts) > 1 else response.strip()
196
 
197
  return self.format_response(raw_response)
198
-
199
- def generate_chatbot_response(self, user_input):
200
- if not LLAMA_API_KEY:
201
- return "LLaMA API key not found. Please set the LLAMA_API_KEY environment variable."
 
 
 
202
 
203
- response = requests.post(
204
- LLAMA_API_ENDPOINT,
205
- headers={"Authorization": f"Bearer {LLAMA_API_KEY}"},
206
- json={"prompt": user_input, "max_tokens": 150}
207
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
- if response.status_code == 401:
210
- return "Unauthorized: Please check your LLaMA API key."
211
- elif response.status_code != 200:
212
- return f"Error: Received {response.status_code} status code from LLaMA API."
213
 
214
- return response.json()["choices"][0]["text"].strip()
 
 
 
 
215
 
216
  def create_interface():
217
  assistant = LegalEaseAssistant()
@@ -445,35 +475,66 @@ def create_interface():
445
  outputs=risk_output
446
  )
447
 
448
- # Legal Assistant Chat Tab
449
- with gr.Tab("πŸ€– Legal Assistant Chat"):
450
- chatbot_input = gr.Textbox(
451
- label="πŸ’¬ Your Message",
452
- placeholder="Ask me anything about legal matters...",
453
- lines=2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
  )
455
- chatbot_output = gr.Textbox(
456
- label="πŸ€– Assistant Response",
457
- lines=10,
 
458
  show_copy_button=True
459
  )
460
- chatbot_btn = gr.Button(
461
- "πŸ’¬ Send Message",
462
- variant="primary"
463
- )
464
-
465
- def chatbot_handler(user_input, lang):
466
- if not user_input:
467
- return "Please type a message to start the conversation."
468
- response = assistant.generate_chatbot_response(user_input)
469
  if lang != "English":
470
- response = translator.translate_text(response, SUPPORTED_LANGUAGES[lang])
471
- return response
472
 
473
- chatbot_btn.click(
474
- fn=chatbot_handler,
475
- inputs=[chatbot_input, language_selector],
476
- outputs=chatbot_output
 
 
 
 
 
 
477
  )
478
 
479
  gr.HTML(f"""
 
10
  import requests
11
  import uuid
12
  import io
13
+ import difflib
14
 
15
  # Configuration
16
  MODEL_NAME = "google/gemma-2b-it"
17
  CURRENT_USER = "AkarshanGupta"
18
+ CURRENT_TIME = "2025-03-23 05:05:24"
19
 
20
  # API Keys
21
  HF_TOKEN = os.getenv('HF_TOKEN')
22
  AZURE_TRANSLATION_KEY = os.getenv('AZURE_TRANSLATION_KEY')
 
 
23
 
24
  class TextExtractor:
25
  @staticmethod
 
148
 
149
  login(token=HF_TOKEN)
150
 
 
151
  self.text_extractor = TextExtractor()
152
 
153
  self.tokenizer = AutoTokenizer.from_pretrained(
 
173
  "simplify": f"Simplify the following legal text in clear, plain language. Provide the response as separate points:\n\n{text}\n\nSimplified explanation:",
174
  "summary": f"Provide a concise summary of the following legal document as separate key points:\n\n{text}\n\nSummary:",
175
  "key_terms": f"Identify and explain the key legal terms and obligations in this text as separate points:\n\n{text}\n\nKey Terms:",
176
+ "risk": f"Perform a risk analysis on the following legal document and list each risk as a separate point:\n\n{text}\n\nRisk Assessment:",
177
+ "compare": f"Compare the following contract sections and identify key differences:\n\n{text}\n\nDifferences:"
178
  }
179
 
180
  prompt = task_prompts.get(task_type, f"Analyze the following text and provide points:\n\n{text}\n\nAnalysis:")
 
194
  raw_response = response_parts[-1].strip() if len(response_parts) > 1 else response.strip()
195
 
196
  return self.format_response(raw_response)
197
+
198
+ def compare_contracts(self, contract1, contract2):
199
+ """
200
+ Compare two contracts and highlight the differences
201
+ """
202
+ text1 = self.text_extractor.extract_text_from_input(contract1)
203
+ text2 = self.text_extractor.extract_text_from_input(contract2)
204
 
205
+ # Split texts into lines
206
+ lines1 = text1.splitlines()
207
+ lines2 = text2.splitlines()
208
+
209
+ # Generate the diff
210
+ differ = difflib.Differ()
211
+ diff = list(differ.compare(lines1, lines2))
212
+
213
+ # Format the differences
214
+ differences = {
215
+ 'added': [],
216
+ 'removed': [],
217
+ 'changed': []
218
+ }
219
+
220
+ for line in diff:
221
+ if line.startswith('+ '):
222
+ differences['added'].append(line[2:])
223
+ elif line.startswith('- '):
224
+ differences['removed'].append(line[2:])
225
+ elif line.startswith('? '):
226
+ continue
227
+ else:
228
+ differences['changed'].append(line[2:])
229
+
230
+ # Generate a summary of differences
231
+ summary = []
232
+ if differences['removed']:
233
+ summary.append("Removed Content:")
234
+ summary.extend(['β€’ ' + line for line in differences['removed']])
235
 
236
+ if differences['added']:
237
+ summary.append("\nAdded Content:")
238
+ summary.extend(['β€’ ' + line for line in differences['added']])
 
239
 
240
+ if differences['changed']:
241
+ summary.append("\nModified Content:")
242
+ summary.extend(['β€’ ' + line for line in differences['changed']])
243
+
244
+ return '\n'.join(summary)
245
 
246
  def create_interface():
247
  assistant = LegalEaseAssistant()
 
475
  outputs=risk_output
476
  )
477
 
478
+ # Contract Comparison Tab
479
+ with gr.Tab("πŸ“Š Contract Comparison"):
480
+ with gr.Row():
481
+ with gr.Column(scale=1):
482
+ contract1_input = gr.File(
483
+ label="πŸ“Ž Upload First Contract",
484
+ file_types=[".txt", ".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp"]
485
+ )
486
+ gr.HTML("<div style='height: 10px'></div>")
487
+ contract1_text = gr.Textbox(
488
+ label="✍ Or Type/Paste First Contract",
489
+ placeholder="Enter your first contract here...",
490
+ lines=4
491
+ )
492
+
493
+ with gr.Column(scale=1):
494
+ contract2_input = gr.File(
495
+ label="πŸ“Ž Upload Second Contract",
496
+ file_types=[".txt", ".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp"]
497
+ )
498
+ gr.HTML("<div style='height: 10px'></div>")
499
+ contract2_text = gr.Textbox(
500
+ label="✍ Or Type/Paste Second Contract",
501
+ placeholder="Enter your second contract here...",
502
+ lines=4
503
+ )
504
+
505
+ compare_btn = gr.Button(
506
+ "πŸ” Compare Contracts",
507
+ variant="primary"
508
  )
509
+
510
+ comparison_output = gr.Textbox(
511
+ label="πŸ“Š Comparison Results",
512
+ lines=15,
513
  show_copy_button=True
514
  )
515
+
516
+ def compare_handler(contract1_file, contract1_text, contract2_file, contract2_text, lang):
517
+ contract1 = contract1_file or contract1_text
518
+ contract2 = contract2_file or contract2_text
519
+
520
+ if not contract1 or not contract2:
521
+ return "Please provide both contracts for comparison."
522
+
523
+ comparison = assistant.compare_contracts(contract1, contract2)
524
  if lang != "English":
525
+ comparison = translator.translate_text(comparison, SUPPORTED_LANGUAGES[lang])
526
+ return comparison
527
 
528
+ compare_btn.click(
529
+ fn=compare_handler,
530
+ inputs=[
531
+ contract1_input,
532
+ contract1_text,
533
+ contract2_input,
534
+ contract2_text,
535
+ language_selector
536
+ ],
537
+ outputs=comparison_output
538
  )
539
 
540
  gr.HTML(f"""