|
import requests |
|
import streamlit as st |
|
|
|
|
|
API_KEY = "3fbfe25109b647efb7bf2f45bd667163" |
|
API_URL = "https://aimlapi.com/" |
|
|
|
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") |
|
|
|
|
|
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 |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload Contract", type=["pdf", "docx"]) |
|
|
|
|
|
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 st.session_state.uploaded_file is not None: |
|
|
|
|
|
st.write("Contract uploaded successfully. Analyzing...") |
|
|
|
|
|
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 st.session_state.analysis_results is not None: |
|
data = st.session_state.analysis_results |
|
crew_analysis = data.get("crew_analysis", {}) |
|
|
|
|
|
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("---") |
|
|
|
|
|
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.") |