Ashar086 commited on
Commit
6976fd6
·
verified ·
1 Parent(s): ad1f305

Upload 2 files

Browse files
Files changed (2) hide show
  1. app (4).py +113 -0
  2. requirements (3).txt +3 -0
app (4).py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import streamlit as st
3
+ from together import Together
4
+
5
+ # Initialize Together AI client
6
+ client = Together(api_key=st.secrets["TOGETHER_API_KEY"])
7
+
8
+ def call_llama_for_response(clauses_data):
9
+ 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"
10
+
11
+ for clause in clauses_data:
12
+ prompt += f"Clause: {clause['agent']}\n"
13
+ prompt += f"Analysis: {clause['analysis']}\n"
14
+ prompt += f"Recommendation: {clause['recommendation']}\n"
15
+ prompt += f"Decision: {clause['action']}\n"
16
+ if clause['action'] == 'Negotiate':
17
+ prompt += f"Negotiation points: {clause['negotiation_points']}\n"
18
+ prompt += "\n"
19
+
20
+ prompt += "Draft a response that addresses each clause, explaining our position on acceptance, rejection, or negotiation. The tone should be professional, courteous, and constructive."
21
+
22
+ response = client.chat.completions.create(
23
+ model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
24
+ messages=[{"role": "user", "content": prompt}],
25
+ max_tokens=2048,
26
+ temperature=0.3,
27
+ top_p=0.8,
28
+ top_k=50,
29
+ repetition_penalty=1,
30
+ stop=["<|eot_id|>", "<|eom_id|>"],
31
+ stream=False
32
+ )
33
+ return response.choices[0].message.content
34
+
35
+ st.title("Contract Negotiation Assistant")
36
+
37
+ # Use session state to store the uploaded file and analysis results
38
+ if 'uploaded_file' not in st.session_state:
39
+ st.session_state.uploaded_file = None
40
+ if 'analysis_results' not in st.session_state:
41
+ st.session_state.analysis_results = None
42
+
43
+ # File uploader
44
+ uploaded_file = st.file_uploader("Upload Contract", type=["pdf", "docx"])
45
+
46
+ # If a new file is uploaded, update the session state and clear previous results
47
+ if uploaded_file is not None and uploaded_file != st.session_state.uploaded_file:
48
+ st.session_state.uploaded_file = uploaded_file
49
+ st.session_state.analysis_results = None
50
+
51
+ # If we have an uploaded file, process it
52
+ if st.session_state.uploaded_file is not None:
53
+ # Only call the API if we don't have analysis results yet
54
+ if st.session_state.analysis_results is None:
55
+ files = {"file": st.session_state.uploaded_file}
56
+ response = requests.post("http://localhost:5002/upload", files=files)
57
+ if response.status_code == 200:
58
+ st.write("Contract uploaded successfully. Analyzing...")
59
+ st.session_state.analysis_results = response.json()
60
+ else:
61
+ st.error("Failed to analyze the contract. Please try again.")
62
+
63
+ # If we have analysis results, display them and allow user interaction
64
+ if st.session_state.analysis_results is not None:
65
+ data = st.session_state.analysis_results
66
+ segmented_contract = data.get("segmented_contract", {})
67
+ crew_analysis = data.get("crew_analysis", {})
68
+
69
+ # Extract the tasks_output from the nested structure
70
+ tasks_output = crew_analysis.get("final_recommendation", {}).get("tasks_output", [])
71
+
72
+ clauses_data = []
73
+ for task in tasks_output:
74
+ agent = task.get("agent", "")
75
+ if task.get("pydantic"):
76
+ clause_analysis = task["pydantic"].get("analysis", "")
77
+ recommendation = task["pydantic"].get("recommendation", "")
78
+
79
+ st.subheader(f"Clause: {agent}")
80
+ st.write("Analysis:")
81
+ st.write(clause_analysis)
82
+ st.write("Recommendation:")
83
+ st.write(recommendation)
84
+
85
+ action = st.selectbox(
86
+ f"Action for {agent}",
87
+ ["Accept", "Negotiate", "Reject"],
88
+ key=f"action_{agent}"
89
+ )
90
+ negotiation_points = ""
91
+ if action == "Negotiate":
92
+ negotiation_points = st.text_area("Enter your negotiation points:", key=f"negotiate_{agent}")
93
+
94
+ clauses_data.append({
95
+ "agent": agent,
96
+ "analysis": clause_analysis,
97
+ "recommendation": recommendation,
98
+ "action": action,
99
+ "negotiation_points": negotiation_points
100
+ })
101
+
102
+ st.markdown("---") # Add a separator between clauses
103
+
104
+ # Finalize Contract button
105
+ if st.button("Finalize Contract"):
106
+ with st.spinner("Generating response..."):
107
+ response_to_drafter = call_llama_for_response(clauses_data)
108
+ st.subheader("Response to Contract Drafter:")
109
+ st.text_area("", response_to_drafter, height=400)
110
+ st.success("Contract negotiation completed. Response generated for review.")
111
+
112
+ else:
113
+ st.write("Please upload a contract to begin the analysis.")
requirements (3).txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit==1.38.0
2
+ together==1.2.12
3
+ requests==2.32.3