File size: 10,151 Bytes
50b9359
92f78f4
 
3a3fd59
 
 
 
ab67fde
3a3fd59
 
 
ee24415
3a3fd59
 
 
 
 
f7c612c
3a3fd59
 
 
ee24415
3a3fd59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dd5497
 
3a3fd59
 
ee35773
19179e5
 
ee35773
 
4dd5497
 
ee35773
 
 
 
 
 
 
 
 
 
4dd5497
 
ee35773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19179e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee35773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19179e5
 
ee35773
19179e5
ee35773
 
 
 
 
 
4dd5497
ee35773
 
 
 
4dd5497
ee35773
 
 
4dd5497
 
ee35773
 
 
 
 
 
 
4dd5497
ee35773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'''import streamlit as st
import json
import base64
from together import Together

# Setup for the 3rd party AI/ML API
api_key = "<3fbfe25109b647efb7bf2f45bd667163>"  # Replace with your actual API key
base_url = "https://api.aimlapi.com/chat"

client = Together(base_url=base_url, api_key=api_key)

def call_ai_api(prompt, max_tokens=1000):
    """
    Function to call the AI API.
    """
    try:
        response = client.chat.completions.create(
    model="meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo",  # Using Llama-3 model
            messages=[
                {
                    "role": "user",
                    "content": prompt  # Directly use the prompt string
                }
            ],
            max_tokens=max_tokens,
        )
        return response.choices[0].message.content
    except Exception as e:
        st.error(f"An error occurred while calling the API: {str(e)}")
        return None

def analyze_contract(file_content):
    # Encode the file content as base64
    encoded_content = base64.b64encode(file_content).decode('utf-8')
    
    prompt = f"""Analyze the following contract document and provide a detailed breakdown of its clauses, including their titles, content, risk level, and a brief explanation for each. The document content is base64 encoded below:

{encoded_content}

Decode the content and analyze it. Format your response as a JSON object with a 'clauses' key containing an array of clause objects. Each clause object should have 'title', 'content', 'risk_level', and 'explanation' keys.
"""

    # Call AI API to analyze the contract
    response = call_ai_api(prompt, max_tokens=2000)
    
    # Parse the JSON response
    if response:
        try:
            analysis_result = json.loads(response)
            return analysis_result
        except json.JSONDecodeError:
            st.error("Failed to parse the API response as JSON.")
            return {"clauses": []}
    else:
        return {"clauses": []}

def generate_response(prompt):
    return call_ai_api(prompt, max_tokens=1000)

st.title("Contract Negotiation Assistant")

uploaded_file = st.file_uploader("Upload your contract document", type=["txt", "pdf", "docx"])

if uploaded_file is not None:
    file_content = uploaded_file.read()
    
    st.write("Analyzing contract...")
    analysis_result = analyze_contract(file_content)
    
    st.write("Contract Analysis Results:")
    
    clauses = analysis_result.get("clauses", [])
    clause_decisions = {}
    
    for i, clause in enumerate(clauses):
        st.subheader(f"Clause {i+1}: {clause['title']}")
        st.write(clause['content'])
        st.write(f"Risk Level: {clause['risk_level']}")
        st.write(f"Explanation: {clause['explanation']}")
        
        decision = st.radio(f"Decision for Clause {i+1}", ["Accept", "Negotiate", "Reject"], key=f"decision_{i}")
        clause_decisions[i] = decision
        
        if decision == "Negotiate":
            negotiation_points = st.text_area(f"Enter negotiation points for Clause {i+1}", key=f"negotiation_{i}")
            clause_decisions[f"{i}_points"] = negotiation_points

    if st.button("Generate Response"):
        st.write("Generating response...")
        
        prompt = """As a professional contract negotiator, draft a courteous email response to the contract drafter based on the following decisions:

"""
        
        for i, clause in enumerate(clauses):
            decision = clause_decisions[i]
            prompt += f"Clause {i+1} ({clause['title']}): {decision}\n"
            if decision == "Negotiate":
                prompt += f"Negotiation points: {clause_decisions.get(f'{i}_points', 'No specific points provided.')}\n"
            prompt += "\n"
        
        prompt += "Please draft a professional and polite email response addressing these points and suggesting next steps for the negotiation process."
        
        response = generate_response(prompt)
        
        st.subheader("Generated Response:")
        st.write(response)
        
        if st.button("Save Response"):
            # Implement saving functionality here
            st.write("Response saved successfully!")

else:
    st.write("Please upload a contract to begin the analysis.")



'''


import streamlit as st
import json
import openai
import fitz  # PyMuPDF
import docx  # python-docx

# Setup for the 3rd party OpenAI API
base_url = "https://api.aimlapi.com/v1"
api_key = '3fbfe25109b647efb7bf2f45bd667163'

# Set the API key for OpenAI
openai.api_key = api_key
openai.api_base = base_url

def call_ai_api(prompt, max_tokens=1000, temperature=0.7):
    """
    Function to call the 3rd party OpenAI API.
    """
    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",  # or the appropriate model for your API
            messages=[{"role": "user", "content": prompt}],
            max_tokens=max_tokens,
            temperature=temperature
        )
        return response.choices[0].message.content
    except Exception as e:
        st.error(f"An error occurred while calling the API: {str(e)}")
        return None

def chunk_text(text, max_chunk_size=3000):
    """
    Split the text into chunks based on a maximum size.
    """
    chunks = []
    words = text.split()
    current_chunk = []
    
    for word in words:
        current_chunk.append(word)
        if len(' '.join(current_chunk)) > max_chunk_size:
            chunks.append(' '.join(current_chunk[:-1]))
            current_chunk = [word]
    
    if current_chunk:
        chunks.append(' '.join(current_chunk))
        
    return chunks

def extract_text_from_pdf(file_content):
    """
    Extract text from a PDF file using PyMuPDF.
    """
    pdf_document = fitz.open(stream=file_content, filetype="pdf")
    text = ""
    for page in pdf_document:
        text += page.get_text()
    return text

def extract_text_from_docx(file_content):
    """
    Extract text from a DOCX file using python-docx.
    """
    doc = docx.Document(file_content)
    text = []
    for para in doc.paragraphs:
        text.append(para.text)
    return "\n".join(text)

def analyze_contract(file_content, file_type):
    # Extract the text based on the file type
    if file_type == "pdf":
        decoded_content = extract_text_from_pdf(file_content)
    elif file_type == "docx":
        decoded_content = extract_text_from_docx(file_content)
    else:
        decoded_content = file_content.decode('utf-8')  # Assuming it's a text file

    # Chunk the contract content
    chunks = chunk_text(decoded_content, max_chunk_size=3000)  # Adjust the size as needed
    analysis_results = {"clauses": []}
    
    for chunk in chunks:
        prompt = f"""Analyze the following contract section and provide a detailed breakdown of its clauses, including their titles, content, risk level, and a brief explanation for each. The document content is as follows:
{chunk}
Format your response as a JSON object with a 'clauses' key containing an array of clause objects. Each clause object should have 'title', 'content', 'risk_level', and 'explanation' keys.
"""
        # Call AI API to analyze each chunk
        response = call_ai_api(prompt, max_tokens=2000)
        
        # Parse the JSON response
        if response:
            try:
                analysis_result = json.loads(response)
                analysis_results["clauses"].extend(analysis_result.get("clauses", []))
            except json.JSONDecodeError:
                st.error("Failed to parse the API response as JSON.")
    
    return analysis_results

st.title("Contract Negotiation Assistant")

uploaded_file = st.file_uploader("Upload your contract document", type=["txt", "pdf", "docx"])

if uploaded_file is not None:
    file_content = uploaded_file.read()
    file_type = uploaded_file.type.split('/')[1]  # Get file type (e.g., pdf, docx)

    st.write("Analyzing contract...")
    analysis_result = analyze_contract(file_content, file_type)
    
    if analysis_result and analysis_result.get("clauses"):
        clauses = analysis_result.get("clauses", [])
        clause_decisions = {}
        
        for i, clause in enumerate(clauses):
            st.subheader(f"Clause {i + 1}: {clause['title']}")
            st.write(clause['content'])
            st.write(f"Risk Level: {clause['risk_level']}")
            st.write(f"Explanation: {clause['explanation']}")
            
            decision = st.radio(f"Decision for Clause {i + 1}", ["Accept", "Negotiate", "Reject"], key=f"decision_{i}")
            clause_decisions[i] = decision
            
            if decision == "Negotiate":
                negotiation_points = st.text_area(f"Enter negotiation points for Clause {i + 1}", key=f"negotiation_{i}")
                clause_decisions[f"{i}_points"] = negotiation_points  # Save negotiation points

        # Finalize Contract button
        if st.button("Finalize Contract"):
            prompt = """As a professional contract negotiator, draft a courteous email response to the contract drafter based on the following decisions:\n\n"""
            
            for i, clause in enumerate(clauses):
                decision = clause_decisions[i]
                prompt += f"Clause {i + 1} ({clause['title']}): {decision}\n"
                if decision == "Negotiate":
                    prompt += f"Negotiation points: {clause_decisions.get(f'{i}_points', 'No specific points provided.')}\n"
                prompt += "\n"
            
            prompt += "Please draft a professional and polite email response addressing these points and suggesting next steps for the negotiation process."
            
            response = call_ai_api(prompt)
            
            st.subheader("Generated Response:")
            st.write(response)
            
            if st.button("Save Response"):
                # Implement saving functionality here
                st.write("Response saved successfully!")

    else:
        st.write("No clauses found in the contract analysis. Please try again.")
else:
    st.write("Please upload a contract to begin the analysis.")