Komal133 commited on
Commit
e78607f
·
verified ·
1 Parent(s): 129188e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -40
app.py CHANGED
@@ -2,30 +2,40 @@ import streamlit as st
2
  import requests
3
  import json
4
  from transformers import pipeline
 
 
 
 
5
 
6
  # Initialize the BERT-based NLP pipeline
7
- model_name = "your-huggingface-model-name" # Replace this with your model
8
  nlp_pipeline = pipeline("ner", model=model_name)
9
 
 
 
 
10
  # Function to analyze contract text
11
  def analyze_contract(contract_text):
12
- # Run the contract through the NLP pipeline
13
- results = nlp_pipeline(contract_text)
14
-
15
- # Parse and score clauses (this is a simplified version)
16
- risk_score = 0
17
- high_risk_clauses = []
18
-
19
- for result in results:
20
- # This assumes 'labels' are risk-related; adjust as per model output
21
- if result['label'] in ["PENALTY", "OBLIGATION", "DELAY"]: # Customize as per your model's tags
22
- high_risk_clauses.append(result['word'])
23
- risk_score += 10 # Example scoring logic, modify as needed
24
-
25
- return {
26
- "high_risk_clauses": high_risk_clauses,
27
- "risk_score": risk_score
28
- }
 
 
 
29
 
30
  # Streamlit UI
31
  st.title("Contract Risk Analyzer")
@@ -36,34 +46,63 @@ contract_file = st.file_uploader("Upload Contract", type=["pdf", "docx", "txt"])
36
  if contract_file is not None:
37
  contract_text = ""
38
  if contract_file.type == "application/pdf":
39
- import PyPDF2
40
  # Read PDF
41
- pdf_reader = PyPDF2.PdfReader(contract_file)
42
- for page in pdf_reader.pages:
43
- contract_text += page.extract_text()
 
 
 
 
44
  elif contract_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
45
- import docx
46
  # Read DOCX
47
- doc = docx.Document(contract_file)
48
- for para in doc.paragraphs:
49
- contract_text += para.text
 
 
 
 
50
  elif contract_file.type == "text/plain":
 
51
  contract_text = contract_file.read().decode("utf-8")
52
-
53
  # Analyze the contract text
54
  if contract_text:
55
  analysis_results = analyze_contract(contract_text)
56
 
57
- # Display the high-risk clauses and risk score
58
- st.subheader("High Risk Clauses")
59
- st.write(", ".join(analysis_results["high_risk_clauses"]))
60
-
61
- st.subheader("Overall Risk Score")
62
- st.write(analysis_results["risk_score"])
63
-
64
- # Generate the risk heatmap (simplified here, you might want a more complex rendering)
65
- st.subheader("Risk Heatmap")
66
- st.write(f"Risk Score: {analysis_results['risk_score']}")
67
- # Visualize as per your design (here we can display a simple score)
68
-
69
- # Here you could add logic to save the results to Salesforce or other systems
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import requests
3
  import json
4
  from transformers import pipeline
5
+ import PyPDF2
6
+ import docx
7
+ import matplotlib.pyplot as plt
8
+ from simple_salesforce import Salesforce
9
 
10
  # Initialize the BERT-based NLP pipeline
11
+ model_name = "dbmdz/bert-large-cased-finetuned-conll03-english" # Example, replace with your model
12
  nlp_pipeline = pipeline("ner", model=model_name)
13
 
14
+ # Salesforce Integration (optional - uncomment if needed)
15
+ # sf = Salesforce(username='your_username', password='your_password', security_token='your_token')
16
+
17
  # Function to analyze contract text
18
  def analyze_contract(contract_text):
19
+ try:
20
+ # Run the contract through the NLP pipeline
21
+ results = nlp_pipeline(contract_text)
22
+
23
+ # Parse and score clauses (this is a simplified version)
24
+ risk_score = 0
25
+ high_risk_clauses = []
26
+
27
+ for result in results:
28
+ # This assumes 'labels' are risk-related; adjust as per model output
29
+ if result['label'] in ["PENALTY", "OBLIGATION", "DELAY"]: # Customize as per your model's tags
30
+ high_risk_clauses.append(result['word'])
31
+ risk_score += 10 # Example scoring logic, modify as needed
32
+
33
+ return {
34
+ "high_risk_clauses": high_risk_clauses,
35
+ "risk_score": risk_score
36
+ }
37
+ except Exception as e:
38
+ return {"error": str(e)}
39
 
40
  # Streamlit UI
41
  st.title("Contract Risk Analyzer")
 
46
  if contract_file is not None:
47
  contract_text = ""
48
  if contract_file.type == "application/pdf":
 
49
  # Read PDF
50
+ try:
51
+ pdf_reader = PyPDF2.PdfReader(contract_file)
52
+ for page in pdf_reader.pages:
53
+ contract_text += page.extract_text()
54
+ except Exception as e:
55
+ st.error(f"Error reading PDF: {str(e)}")
56
+
57
  elif contract_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
 
58
  # Read DOCX
59
+ try:
60
+ doc = docx.Document(contract_file)
61
+ for para in doc.paragraphs:
62
+ contract_text += para.text
63
+ except Exception as e:
64
+ st.error(f"Error reading DOCX: {str(e)}")
65
+
66
  elif contract_file.type == "text/plain":
67
+ # Read TXT
68
  contract_text = contract_file.read().decode("utf-8")
69
+
70
  # Analyze the contract text
71
  if contract_text:
72
  analysis_results = analyze_contract(contract_text)
73
 
74
+ if "error" in analysis_results:
75
+ st.error(f"An error occurred during analysis: {analysis_results['error']}")
76
+ else:
77
+ # Display the high-risk clauses and risk score
78
+ st.subheader("High Risk Clauses")
79
+ st.write(", ".join(analysis_results["high_risk_clauses"]))
80
+
81
+ st.subheader("Overall Risk Score")
82
+ st.write(analysis_results["risk_score"])
83
+
84
+ # Generate the risk heatmap (simplified here)
85
+ st.subheader("Risk Heatmap")
86
+
87
+ # Create a simple bar chart to visualize the risk score
88
+ fig, ax = plt.subplots()
89
+ ax.barh(['Contract'], [analysis_results["risk_score"]], color='red')
90
+ ax.set_xlim(0, 100) # Assuming risk score ranges from 0 to 100
91
+ ax.set_xlabel("Risk Score")
92
+ st.pyplot(fig)
93
+
94
+ # Optionally save the results to Salesforce
95
+ # Uncomment the following block if you want to integrate with Salesforce
96
+ """
97
+ try:
98
+ contract_risk_scan = sf.Contract_Risk_Scan__c.create({
99
+ 'Contract_Title__c': 'Sample Contract',
100
+ 'Overall_Risk_Score__c': analysis_results['risk_score'],
101
+ 'High_Risk_Clauses__c': ', '.join(analysis_results['high_risk_clauses']),
102
+ 'Risk_Map_URL__c': 'generated_map_url', # You can generate a URL for the heatmap if required
103
+ 'Evaluation_Date__c': '2025-06-05' # You can adjust this to current date
104
+ })
105
+ st.success("Contract analysis results saved to Salesforce.")
106
+ except Exception as e:
107
+ st.error(f"Failed to save to Salesforce: {str(e)}")
108
+ """