Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import requests | |
| import json | |
| from transformers import pipeline | |
| # Initialize the BERT-based NLP pipeline | |
| model_name = "your-huggingface-model-name" # Replace this with your model | |
| nlp_pipeline = pipeline("ner", model=model_name) | |
| # Function to analyze contract text | |
| def analyze_contract(contract_text): | |
| # Run the contract through the NLP pipeline | |
| results = nlp_pipeline(contract_text) | |
| # Parse and score clauses (this is a simplified version) | |
| risk_score = 0 | |
| high_risk_clauses = [] | |
| for result in results: | |
| # This assumes 'labels' are risk-related; adjust as per model output | |
| if result['label'] in ["PENALTY", "OBLIGATION", "DELAY"]: # Customize as per your model's tags | |
| high_risk_clauses.append(result['word']) | |
| risk_score += 10 # Example scoring logic, modify as needed | |
| return { | |
| "high_risk_clauses": high_risk_clauses, | |
| "risk_score": risk_score | |
| } | |
| # Streamlit UI | |
| st.title("Contract Risk Analyzer") | |
| # File upload | |
| contract_file = st.file_uploader("Upload Contract", type=["pdf", "docx", "txt"]) | |
| if contract_file is not None: | |
| contract_text = "" | |
| if contract_file.type == "application/pdf": | |
| import PyPDF2 | |
| # Read PDF | |
| pdf_reader = PyPDF2.PdfReader(contract_file) | |
| for page in pdf_reader.pages: | |
| contract_text += page.extract_text() | |
| elif contract_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document": | |
| import docx | |
| # Read DOCX | |
| doc = docx.Document(contract_file) | |
| for para in doc.paragraphs: | |
| contract_text += para.text | |
| elif contract_file.type == "text/plain": | |
| contract_text = contract_file.read().decode("utf-8") | |
| # Analyze the contract text | |
| if contract_text: | |
| analysis_results = analyze_contract(contract_text) | |
| # Display the high-risk clauses and risk score | |
| st.subheader("High Risk Clauses") | |
| st.write(", ".join(analysis_results["high_risk_clauses"])) | |
| st.subheader("Overall Risk Score") | |
| st.write(analysis_results["risk_score"]) | |
| # Generate the risk heatmap (simplified here, you might want a more complex rendering) | |
| st.subheader("Risk Heatmap") | |
| st.write(f"Risk Score: {analysis_results['risk_score']}") | |
| # Visualize as per your design (here we can display a simple score) | |
| # Here you could add logic to save the results to Salesforce or other systems | |