File size: 5,700 Bytes
6976fd6
 
 
3954c69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e6bc54
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
import requests
import streamlit as st

# Set your API key and endpoint here
API_KEY = "3fbfe25109b647efb7bf2f45bd667163"  # Replace with your actual API key
API_URL = "https://aimlapi.com/"  # Replace with the actual API endpoint

def call_ai_ml_api(prompt):
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json",
    }
    payload = {
        "model": "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
        "prompt": prompt,
        "max_new_tokens": 500,
        "temperature": 0.7,
    }
    
    response = requests.post(API_URL, headers=headers, json=payload)
    
    if response.status_code == 200:
        return response.json()["generated_text"]
    else:
        st.error("Failed to generate response from AI/ML API.")
        return ""

def call_llama_for_response(clauses_data):
    prompt = "As an AI assistant specializing in contract analysis, draft a professional and courteous response to a contract drafter based on the following clause analyses and decisions:\n\n"
    
    for clause in clauses_data:
        prompt += f"Clause: {clause['agent']}\n"
        prompt += f"Analysis: {clause['analysis']}\n"
        prompt += f"Recommendation: {clause['recommendation']}\n"
        prompt += f"Decision: {clause['action']}\n"
        if clause['action'] == 'Negotiate':
            prompt += f"Negotiation points: {clause['negotiation_points']}\n"
        prompt += "\n"
    
    prompt += "Draft a response that addresses each clause, explaining our position on acceptance, rejection, or negotiation. The tone should be professional, courteous, and constructive."

    response = call_ai_ml_api(prompt)
    return response

st.title("Contract Negotiation Assistant")

# Use session state to store the uploaded file and analysis results
if 'uploaded_file' not in st.session_state:
    st.session_state.uploaded_file = None
if 'analysis_results' not in st.session_state:
    st.session_state.analysis_results = None

# File uploader
uploaded_file = st.file_uploader("Upload Contract", type=["pdf", "docx"])

# If a new file is uploaded, update the session state and clear previous results
if uploaded_file is not None and uploaded_file != st.session_state.uploaded_file:
    st.session_state.uploaded_file = uploaded_file
    st.session_state.analysis_results = None

# If we have an uploaded file, process it
if st.session_state.uploaded_file is not None:
    # Simulate analysis of the uploaded contract (you may replace this with your actual analysis logic)
    # For now, we just mock some analysis results for demonstration purposes
    st.write("Contract uploaded successfully. Analyzing...")
    
    # Example of simulated analysis results
    st.session_state.analysis_results = {
        "crew_analysis": {
            "final_recommendation": {
                "tasks_output": [
                    {
                        "agent": "Clause 1",
                        "pydantic": {
                            "analysis": "This clause limits liability.",
                            "recommendation": "Consider revising for fairness."
                        }
                    },
                    {
                        "agent": "Clause 2",
                        "pydantic": {
                            "analysis": "This clause outlines payment terms.",
                            "recommendation": "This is acceptable."
                        }
                    }
                ]
            }
        }
    }

    # If we have analysis results, display them and allow user interaction
    if st.session_state.analysis_results is not None:
        data = st.session_state.analysis_results
        crew_analysis = data.get("crew_analysis", {})

        # Extract the tasks_output from the nested structure
        tasks_output = crew_analysis.get("final_recommendation", {}).get("tasks_output", [])

        clauses_data = []
        for task in tasks_output:
            agent = task.get("agent", "")
            if task.get("pydantic"):
                clause_analysis = task["pydantic"].get("analysis", "")
                recommendation = task["pydantic"].get("recommendation", "")
                
                st.subheader(f"Clause: {agent}")
                st.write("Analysis:")
                st.write(clause_analysis)
                st.write("Recommendation:")
                st.write(recommendation)

                action = st.selectbox(
                    f"Action for {agent}",
                    ["Accept", "Negotiate", "Reject"],
                    key=f"action_{agent}"
                )
                negotiation_points = ""
                if action == "Negotiate":
                    negotiation_points = st.text_area("Enter your negotiation points:", key=f"negotiate_{agent}")
                
                clauses_data.append({
                    "agent": agent,
                    "analysis": clause_analysis,
                    "recommendation": recommendation,
                    "action": action,
                    "negotiation_points": negotiation_points
                })
                
                st.markdown("---")  # Add a separator between clauses

        # Finalize Contract button
        if st.button("Finalize Contract"):
            with st.spinner("Generating response..."):
                response_to_drafter = call_llama_for_response(clauses_data)
            st.subheader("Response to Contract Drafter:")
            st.text_area("", response_to_drafter, height=400)
            st.success("Contract negotiation completed. Response generated for review.")

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