Spaces:
Sleeping
Sleeping
Akshayram1
commited on
Commit
•
e4d4b3b
1
Parent(s):
48f70e3
Delete app.py
Browse files
app.py
DELETED
@@ -1,644 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from phi.agent import Agent
|
3 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
4 |
-
from phi.vectordb.qdrant import Qdrant
|
5 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
6 |
-
from phi.model.openai import OpenAIChatimport streamlit as st
|
7 |
-
from phi.agent import Agent
|
8 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
9 |
-
from phi.vectordb.qdrant import Qdrant
|
10 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
11 |
-
from phi.model.openai import OpenAIChat
|
12 |
-
from phi.embedder.openai import OpenAIEmbedder
|
13 |
-
import tempfile
|
14 |
-
import os
|
15 |
-
|
16 |
-
#initializing the session state variables
|
17 |
-
def init_session_state():
|
18 |
-
"""Initialize session state variables"""
|
19 |
-
if 'openai_api_key' not in st.session_state:
|
20 |
-
st.session_state.openai_api_key = None
|
21 |
-
if 'qdrant_api_key' not in st.session_state:
|
22 |
-
st.session_state.qdrant_api_key = None
|
23 |
-
if 'qdrant_url' not in st.session_state:
|
24 |
-
st.session_state.qdrant_url = None
|
25 |
-
if 'vector_db' not in st.session_state:
|
26 |
-
st.session_state.vector_db = None
|
27 |
-
if 'legal_team' not in st.session_state:
|
28 |
-
st.session_state.legal_team = None
|
29 |
-
if 'knowledge_base' not in st.session_state:
|
30 |
-
st.session_state.knowledge_base = None
|
31 |
-
|
32 |
-
def init_qdrant():
|
33 |
-
"""Initialize Qdrant vector database"""
|
34 |
-
if not st.session_state.qdrant_api_key:
|
35 |
-
raise ValueError("Qdrant API key not provided")
|
36 |
-
if not st.session_state.qdrant_url:import streamlit as st
|
37 |
-
from phi.agent import Agent
|
38 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
39 |
-
from phi.vectordb.qdrant import Qdrant
|
40 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
41 |
-
from phi.model.openai import OpenAIChat
|
42 |
-
main()
|
43 |
-
from phi.embedder.openai import OpenAIEmbedder
|
44 |
-
import tempfile
|
45 |
-
import os
|
46 |
-
|
47 |
-
#initializing the session state variables
|
48 |
-
def init_session_state():
|
49 |
-
"""Initialize session state variables"""
|
50 |
-
if 'openai_api_key' not in st.session_state:
|
51 |
-
st.session_state.openai_api_key = None
|
52 |
-
if 'qdrant_api_key' not in st.session_state:
|
53 |
-
st.session_state.qdrant_api_key = None
|
54 |
-
if 'qdrant_url' not in st.session_state:
|
55 |
-
st.session_state.qdrant_url = None
|
56 |
-
if 'vector_db' not in st.session_state:
|
57 |
-
st.session_state.vector_db = None
|
58 |
-
if 'legal_team' not in st.session_state:
|
59 |
-
st.session_state.legal_team = None
|
60 |
-
if 'knowledge_base' not in st.session_state:
|
61 |
-
st.session_state.knowledge_base = None
|
62 |
-
|
63 |
-
def init_qdrant():
|
64 |
-
"""Initialize Qdrant vector database"""
|
65 |
-
if not st.session_state.qdrant_api_key:
|
66 |
-
raise ValueError("Qdrant API key not provided")
|
67 |
-
if not st.session_state.qdrant_url:import streamlit as st
|
68 |
-
from phi.agent import Agent
|
69 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
70 |
-
from phi.vectordb.qdrant import Qdrant
|
71 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
72 |
-
from phi.model.openai import OpenAIChat
|
73 |
-
from phi.embedder.openai import OpenAIEmbedder
|
74 |
-
import tempfile
|
75 |
-
import os
|
76 |
-
|
77 |
-
#initializing the session state variables
|
78 |
-
def init_session_state():
|
79 |
-
"""Initialize session state variables"""
|
80 |
-
if 'openai_api_key' not in st.session_state:
|
81 |
-
st.session_state.openai_api_key = None
|
82 |
-
if 'qdrant_api_key' not in st.session_state:
|
83 |
-
st.session_state.qdrant_api_key = None
|
84 |
-
if 'qdrant_url' not in st.session_state:
|
85 |
-
st.session_state.qdrant_url = None
|
86 |
-
if 'vector_db' not in st.session_state:
|
87 |
-
st.session_state.vector_db = None
|
88 |
-
if 'legal_team' not in st.session_state:
|
89 |
-
st.session_state.legal_team = None
|
90 |
-
if 'knowledge_base' not in st.session_state:
|
91 |
-
st.session_state.knowledge_base = None
|
92 |
-
|
93 |
-
def init_qdrant():
|
94 |
-
"""Initialize Qdrant vector database"""
|
95 |
-
if not st.session_state.qdrant_api_key:
|
96 |
-
raise ValueError("Qdrant API key not provided")
|
97 |
-
if not st.session_state.qdrant_url:import streamlit as st
|
98 |
-
from phi.agent import Agent
|
99 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
100 |
-
from phi.vectordb.qdrant import Qdrant
|
101 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
102 |
-
from phi.model.openai import OpenAIChat
|
103 |
-
main()
|
104 |
-
raise ValueError("Qdrant URL not provided")
|
105 |
-
|
106 |
-
return Qdrant(
|
107 |
-
collection="legal_knowledge",
|
108 |
-
url=st.session_state.qdrant_url,
|
109 |
-
api_key=st.session_state.qdrant_api_key,
|
110 |
-
https=True,
|
111 |
-
timeout=None,
|
112 |
-
distance="cosine"
|
113 |
-
)
|
114 |
-
|
115 |
-
def process_document(uploaded_file, vector_db: Qdrant):
|
116 |
-
"""Process document, create embeddings and store in Qdrant vector database"""
|
117 |
-
if not st.session_state.openai_api_key:
|
118 |
-
raise ValueError("OpenAI API key not provided")
|
119 |
-
|
120 |
-
os.environ['OPENAI_API_KEY'] = st.session_state.openai_api_key
|
121 |
-
|
122 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
123 |
-
|
124 |
-
temp_file_path = os.path.join(temp_dir, uploaded_file.name)
|
125 |
-
with open(temp_file_path, "wb") as f:
|
126 |
-
f.write(uploaded_file.getbuffer())
|
127 |
-
|
128 |
-
try:import streamlit as st
|
129 |
-
from phi.agent import Agent
|
130 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
131 |
-
from phi.vectordb.qdrant import Qdrant
|
132 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
133 |
-
from phi.model.openai import OpenAIChat
|
134 |
-
from phi.embedder.openai import OpenAIEmbedder
|
135 |
-
import tempfile
|
136 |
-
import os
|
137 |
-
|
138 |
-
#initializing the session state variables
|
139 |
-
def init_session_state():
|
140 |
-
"""Initialize session state variables"""
|
141 |
-
if 'openai_api_key' not in st.session_state:
|
142 |
-
st.session_state.openai_api_key = None
|
143 |
-
if 'qdrant_api_key' not in st.session_state:
|
144 |
-
st.session_state.qdrant_api_key = None
|
145 |
-
if 'qdrant_url' not in st.session_state:
|
146 |
-
st.session_state.qdrant_url = None
|
147 |
-
if 'vector_db' not in st.session_state:
|
148 |
-
st.session_state.vector_db = None
|
149 |
-
if 'legal_team' not in st.session_state:
|
150 |
-
st.session_state.legal_team = None
|
151 |
-
if 'knowledge_base' not in st.session_state:
|
152 |
-
st.session_state.knowledge_base = None
|
153 |
-
|
154 |
-
def init_qdrant():
|
155 |
-
"""Initialize Qdrant vector database"""
|
156 |
-
if not st.session_state.qdrant_api_key:
|
157 |
-
raise ValueError("Qdrant API key not provided")
|
158 |
-
if not st.session_state.qdrant_url:import streamlit as st
|
159 |
-
from phi.agent import Agent
|
160 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
161 |
-
from phi.vectordb.qdrant import Qdrant
|
162 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
163 |
-
from phi.model.openai import OpenAIChat
|
164 |
-
main()
|
165 |
-
|
166 |
-
embedder = OpenAIEmbedder(
|
167 |
-
model="text-embedding-3-small",
|
168 |
-
api_key=st.session_state.openai_api_key
|
169 |
-
)
|
170 |
-
|
171 |
-
# Creating knowledge base with explicit Qdrant configuration
|
172 |
-
knowledge_base = PDFKnowledgeBase(
|
173 |
-
path=temp_dir,
|
174 |
-
vector_db=vector_db,
|
175 |
-
reader=PDFReader(chunk=True),import streamlit as st
|
176 |
-
from phi.agent import Agent
|
177 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
178 |
-
from phi.vectordb.qdrant import Qdrant
|
179 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
180 |
-
from phi.model.openai import OpenAIChat
|
181 |
-
from phi.embedder.openai import OpenAIEmbedder
|
182 |
-
import tempfile
|
183 |
-
import os
|
184 |
-
|
185 |
-
#initializing the session state variables
|
186 |
-
def init_session_state():
|
187 |
-
"""Initialize session state variables"""
|
188 |
-
if 'openai_api_key' not in st.session_state:
|
189 |
-
st.session_state.openai_api_key = None
|
190 |
-
if 'qdrant_api_key' not in st.session_state:
|
191 |
-
st.session_state.qdrant_api_key = None
|
192 |
-
if 'qdrant_url' not in st.session_state:
|
193 |
-
st.session_state.qdrant_url = None
|
194 |
-
if 'vector_db' not in st.session_state:
|
195 |
-
st.session_state.vector_db = None
|
196 |
-
if 'legal_team' not in st.session_state:
|
197 |
-
st.session_state.legal_team = None
|
198 |
-
if 'knowledge_base' not in st.session_state:
|
199 |
-
st.session_state.knowledge_base = None
|
200 |
-
|
201 |
-
def init_qdrant():
|
202 |
-
"""Initialize Qdrant vector database"""
|
203 |
-
if not st.session_state.qdrant_api_key:
|
204 |
-
raise ValueError("Qdrant API key not provided")
|
205 |
-
if not st.session_state.qdrant_url:import streamlit as st
|
206 |
-
from phi.agent import Agent
|
207 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
208 |
-
from phi.vectordb.qdrant import Qdrant
|
209 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
210 |
-
from phi.model.openai import OpenAIChat
|
211 |
-
main()
|
212 |
-
embedder=embedder,
|
213 |
-
recreate_vector_db=True
|
214 |
-
)
|
215 |
-
knowledge_base.load()
|
216 |
-
return knowledge_base
|
217 |
-
except Exception as e:
|
218 |
-
raise Exception(f"Error processing document: {str(e)}")
|
219 |
-
|
220 |
-
def main():
|
221 |
-
st.set_page_config(page_title="Legal Document Analyzer", layout="wide")
|
222 |
-
init_session_state()
|
223 |
-
|
224 |
-
st.title("AI Legal Agent Team 👨⚖️")
|
225 |
-
|
226 |
-
with st.sidebar:
|
227 |
-
st.header("🔑 API Configuration")
|
228 |
-
import streamlit as st
|
229 |
-
from phi.agent import Agent
|
230 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
231 |
-
from phi.vectordb.qdrant import Qdrant
|
232 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
233 |
-
from phi.model.openai import OpenAIChat
|
234 |
-
from phi.embedder.openai import OpenAIEmbedder
|
235 |
-
import tempfile
|
236 |
-
import os
|
237 |
-
|
238 |
-
#initializing the session state variables
|
239 |
-
def init_session_state():
|
240 |
-
"""Initialize session state variables"""
|
241 |
-
if 'openai_api_key' not in st.session_state:
|
242 |
-
st.session_state.openai_api_key = None
|
243 |
-
if 'qdrant_api_key' not in st.session_state:
|
244 |
-
st.session_state.qdrant_api_key = None
|
245 |
-
if 'qdrant_url' not in st.session_state:
|
246 |
-
st.session_state.qdrant_url = None
|
247 |
-
if 'vector_db' not in st.session_state:
|
248 |
-
st.session_state.vector_db = None
|
249 |
-
if 'legal_team' not in st.session_state:
|
250 |
-
st.session_state.legal_team = None
|
251 |
-
if 'knowledge_base' not in st.session_state:
|
252 |
-
st.session_state.knowledge_base = None
|
253 |
-
|
254 |
-
def init_qdrant():
|
255 |
-
"""Initialize Qdrant vector database"""
|
256 |
-
if not st.session_state.qdrant_api_key:
|
257 |
-
raise ValueError("Qdrant API key not provided")
|
258 |
-
if not st.session_state.qdrant_url:import streamlit as st
|
259 |
-
from phi.agent import Agent
|
260 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
261 |
-
from phi.vectordb.qdrant import Qdrant
|
262 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
263 |
-
from phi.model.openai import OpenAIChat
|
264 |
-
main()
|
265 |
-
openai_key = st.text_input(
|
266 |
-
"OpenAI API Key",
|
267 |
-
type="password",
|
268 |
-
value=st.session_state.openai_api_key if st.session_state.openai_api_key else "",
|
269 |
-
help="Enter your OpenAI API key"
|
270 |
-
)
|
271 |
-
if openai_key:
|
272 |
-
st.session_state.openai_api_key = openai_key
|
273 |
-
|
274 |
-
qdrant_key = st.text_input(
|
275 |
-
"Qdrant API Key",
|
276 |
-
type="password",
|
277 |
-
value=st.session_state.qdrant_api_key if st.session_state.qdrant_api_key else "",
|
278 |
-
help="Enter your Qdrant API key"
|
279 |
-
)
|
280 |
-
if qdrant_key:
|
281 |
-
st.session_state.qdrant_api_key = qdrant_key
|
282 |
-
|
283 |
-
qdrant_url = st.text_input(
|
284 |
-
"Qdrant URL",import streamlit as st
|
285 |
-
from phi.agent import Agent
|
286 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
287 |
-
from phi.vectordb.qdrant import Qdrant
|
288 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
289 |
-
from phi.model.openai import OpenAIChat
|
290 |
-
from phi.embedder.openai import OpenAIEmbedder
|
291 |
-
import tempfile
|
292 |
-
import os
|
293 |
-
|
294 |
-
#initializing the session state variables
|
295 |
-
def init_session_state():
|
296 |
-
"""Initialize session state variables"""
|
297 |
-
if 'openai_api_key' not in st.session_state:
|
298 |
-
st.session_state.openai_api_key = None
|
299 |
-
if 'qdrant_api_key' not in st.session_state:
|
300 |
-
st.session_state.qdrant_api_key = None
|
301 |
-
if 'qdrant_url' not in st.session_state:
|
302 |
-
st.session_state.qdrant_url = None
|
303 |
-
if 'vector_db' not in st.session_state:
|
304 |
-
st.session_state.vector_db = None
|
305 |
-
if 'legal_team' not in st.session_state:
|
306 |
-
st.session_state.legal_team = None
|
307 |
-
if 'knowledge_base' not in st.session_state:
|
308 |
-
st.session_state.knowledge_base = None
|
309 |
-
|
310 |
-
def init_qdrant():
|
311 |
-
"""Initialize Qdrant vector database"""
|
312 |
-
if not st.session_state.qdrant_api_key:
|
313 |
-
raise ValueError("Qdrant API key not provided")
|
314 |
-
if not st.session_state.qdrant_url:import streamlit as st
|
315 |
-
from phi.agent import Agent
|
316 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
317 |
-
from phi.vectordb.qdrant import Qdrant
|
318 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
319 |
-
from phi.model.openai import OpenAIChat
|
320 |
-
main()
|
321 |
-
value=st.session_state.qdrant_url if st.session_state.qdrant_url else "https://f499085c-b4bf-4bda-a9a5-227f62a9ca20.us-west-2-0.aws.cloud.qdrant.io:6333",
|
322 |
-
help="Enter your Qdrant instance URL"
|
323 |
-
)
|
324 |
-
if qdrant_url:
|
325 |
-
st.session_state.qdrant_url = qdrant_url
|
326 |
-
|
327 |
-
if all([st.session_state.qdrant_api_key, st.session_state.qdrant_url]):
|
328 |
-
try:
|
329 |
-
if not st.session_state.vector_db:
|
330 |
-
st.session_state.vector_db = init_qdrant()
|
331 |
-
st.success("Successfully connected to Qdrant!")
|
332 |
-
except Exception as e:
|
333 |
-
st.error(f"Failed to connect to Qdrant: {str(e)}")
|
334 |
-
|
335 |
-
st.divider()
|
336 |
-
|
337 |
-
if all([st.session_state.openai_api_key, st.session_state.vector_db]):import streamlit as st
|
338 |
-
from phi.agent import Agent
|
339 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
340 |
-
from phi.vectordb.qdrant import Qdrant
|
341 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
342 |
-
from phi.model.openai import OpenAIChat
|
343 |
-
from phi.embedder.openai import OpenAIEmbedder
|
344 |
-
import tempfile
|
345 |
-
import os
|
346 |
-
|
347 |
-
#initializing the session state variables
|
348 |
-
def init_session_state():
|
349 |
-
"""Initialize session state variables"""
|
350 |
-
if 'openai_api_key' not in st.session_state:
|
351 |
-
st.session_state.openai_api_key = None
|
352 |
-
if 'qdrant_api_key' not in st.session_state:
|
353 |
-
st.session_state.qdrant_api_key = None
|
354 |
-
if 'qdrant_url' not in st.session_state:
|
355 |
-
st.session_state.qdrant_url = None
|
356 |
-
if 'vector_db' not in st.session_state:
|
357 |
-
st.session_state.vector_db = None
|
358 |
-
if 'legal_team' not in st.session_state:
|
359 |
-
st.session_state.legal_team = None
|
360 |
-
if 'knowledge_base' not in st.session_state:
|
361 |
-
st.session_state.knowledge_base = None
|
362 |
-
|
363 |
-
def init_qdrant():
|
364 |
-
"""Initialize Qdrant vector database"""
|
365 |
-
if not st.session_state.qdrant_api_key:
|
366 |
-
raise ValueError("Qdrant API key not provided")
|
367 |
-
if not st.session_state.qdrant_url:import streamlit as st
|
368 |
-
from phi.agent import Agent
|
369 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
370 |
-
from phi.vectordb.qdrant import Qdrant
|
371 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
372 |
-
from phi.model.openai import OpenAIChat
|
373 |
-
main()
|
374 |
-
st.header("📄 Document Upload")
|
375 |
-
uploaded_file = st.file_uploader("Upload Legal Document", type=['pdf'])
|
376 |
-
|
377 |
-
if uploaded_file:
|
378 |
-
with st.spinner("Processing document..."):
|
379 |
-
try:
|
380 |
-
knowledge_base = process_document(uploaded_file, st.session_state.vector_db)
|
381 |
-
st.session_state.knowledge_base = knowledge_base
|
382 |
-
|
383 |
-
# Initialize agents
|
384 |
-
legal_researcher = Agent(
|
385 |
-
name="Legal Researcher",
|
386 |
-
role="Legal research specialist",
|
387 |
-
model=OpenAIChat(model="gpt-4o"),
|
388 |
-
tools=[DuckDuckGo()],
|
389 |
-
knowledge=st.session_state.knowledge_base,
|
390 |
-
search_knowledge=True,
|
391 |
-
instructions=[
|
392 |
-
"Find and cite relevant legal cases and precedents",import streamlit as st
|
393 |
-
from phi.agent import Agent
|
394 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
395 |
-
from phi.vectordb.qdrant import Qdrant
|
396 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
397 |
-
from phi.model.openai import OpenAIChat
|
398 |
-
from phi.embedder.openai import OpenAIEmbedder
|
399 |
-
import tempfile
|
400 |
-
import os
|
401 |
-
|
402 |
-
#initializing the session state variables
|
403 |
-
def init_session_state():
|
404 |
-
"""Initialize session state variables"""
|
405 |
-
if 'openai_api_key' not in st.session_state:
|
406 |
-
st.session_state.openai_api_key = None
|
407 |
-
if 'qdrant_api_key' not in st.session_state:
|
408 |
-
st.session_state.qdrant_api_key = None
|
409 |
-
if 'qdrant_url' not in st.session_state:
|
410 |
-
st.session_state.qdrant_url = None
|
411 |
-
if 'vector_db' not in st.session_state:
|
412 |
-
st.session_state.vector_db = None
|
413 |
-
if 'legal_team' not in st.session_state:
|
414 |
-
st.session_state.legal_team = None
|
415 |
-
if 'knowledge_base' not in st.session_state:
|
416 |
-
st.session_state.knowledge_base = None
|
417 |
-
|
418 |
-
def init_qdrant():
|
419 |
-
"""Initialize Qdrant vector database"""
|
420 |
-
if not st.session_state.qdrant_api_key:
|
421 |
-
raise ValueError("Qdrant API key not provided")
|
422 |
-
if not st.session_state.qdrant_url:import streamlit as st
|
423 |
-
from phi.agent import Agent
|
424 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
425 |
-
from phi.vectordb.qdrant import Qdrant
|
426 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
427 |
-
from phi.model.openai import OpenAIChat
|
428 |
-
main()
|
429 |
-
"Provide detailed research summaries with sources",
|
430 |
-
"Reference specific sections from the uploaded document",
|
431 |
-
"Always search the knowledge base for relevant information"
|
432 |
-
],
|
433 |
-
show_tool_calls=True,
|
434 |
-
markdown=True
|
435 |
-
)
|
436 |
-
|
437 |
-
contract_analyst = Agent(
|
438 |
-
name="Contract Analyst",
|
439 |
-
role="Contract analysis specialist",
|
440 |
-
model=OpenAIChat(model="gpt-4o"),
|
441 |
-
knowledge=knowledge_base,
|
442 |
-
search_knowledge=True,
|
443 |
-
instructions=[
|
444 |
-
"Review contracts thoroughly",
|
445 |
-
"Identify key terms and potential issues",
|
446 |
-
"Reference specific clauses from the document"
|
447 |
-
],
|
448 |
-
markdown=True
|
449 |
-
)
|
450 |
-
|
451 |
-
legal_strategist = Agent(
|
452 |
-
name="Legal Strategist",
|
453 |
-
role="Legal strategy specialist",
|
454 |
-
model=OpenAIChat(model="gpt-4o"),
|
455 |
-
knowledge=knowledge_base,
|
456 |
-
search_knowledge=True,
|
457 |
-
instructions=[
|
458 |
-
"Develop comprehensive legal strategies",
|
459 |
-
"Provide actionable recommendations",
|
460 |
-
"Consider both risks and opportunities"
|
461 |
-
],
|
462 |
-
markdown=True
|
463 |
-
)
|
464 |
-
|
465 |
-
# Legal Agent Team
|
466 |
-
st.session_state.legal_team = Agent(
|
467 |
-
name="Legal Team Lead",
|
468 |
-
role="Legal team coordinator",
|
469 |
-
model=OpenAIChat(model="gpt-4o"),
|
470 |
-
team=[legal_researcher, contract_analyst, legal_strategist],
|
471 |
-
knowledge=st.session_state.knowledge_base,
|
472 |
-
search_knowledge=True,
|
473 |
-
instructions=[
|
474 |
-
"Coordinate analysis between team members",
|
475 |
-
"Provide comprehensive responses",
|
476 |
-
"Ensure all recommendations are properly sourced",
|
477 |
-
"Reference specific parts of the uploaded document",
|
478 |
-
"Always search the knowledge base before delegating tasks"
|
479 |
-
],
|
480 |
-
show_tool_calls=True,
|
481 |
-
markdown=True
|
482 |
-
)
|
483 |
-
|
484 |
-
st.success("✅ Document processed and team initialized!")
|
485 |
-
|
486 |
-
except Exception as e:
|
487 |
-
st.error(f"Error processing document: {str(e)}")
|
488 |
-
|
489 |
-
st.divider()
|
490 |
-
st.header("��� Analysis Options")
|
491 |
-
analysis_type = st.selectbox(
|
492 |
-
"Select Analysis Type",
|
493 |
-
[
|
494 |
-
"Contract Review",
|
495 |
-
"Legal Research",
|
496 |
-
"Risk Assessment",
|
497 |
-
"Compliance Check",
|
498 |
-
"Custom Query"
|
499 |
-
]
|
500 |
-
)
|
501 |
-
else:
|
502 |
-
st.warning("Please configure all API credentials to proceed")
|
503 |
-
|
504 |
-
# Main content area
|
505 |
-
if not all([st.session_state.openai_api_key, st.session_state.vector_db]):
|
506 |
-
st.info("👈 Please configure your API credentials in the sidebar to begin")
|
507 |
-
elif not uploaded_file:
|
508 |
-
st.info("👈 Please upload a legal document to begin analysis")
|
509 |
-
elif st.session_state.legal_team:
|
510 |
-
# Create a dictionary for analysis type icons
|
511 |
-
analysis_icons = {
|
512 |
-
"Contract Review": "📑",
|
513 |
-
"Legal Research": "🔍",
|
514 |
-
"Risk Assessment": "⚠️",
|
515 |
-
"Compliance Check": "✅",
|
516 |
-
"Custom Query": "💭"
|
517 |
-
}
|
518 |
-
|
519 |
-
# Dynamic header with icon
|
520 |
-
st.header(f"{analysis_icons[analysis_type]} {analysis_type} Analysis")
|
521 |
-
|
522 |
-
analysis_configs = {
|
523 |
-
"Contract Review": {
|
524 |
-
"query": "Review this contract and identify key terms, obligations, and potential issues.",
|
525 |
-
"agents": ["Contract Analyst"],
|
526 |
-
"description": "Detailed contract analysis focusing on terms and obligations"
|
527 |
-
},
|
528 |
-
"Legal Research": {
|
529 |
-
"query": "Research relevant cases and precedents related to this document.",
|
530 |
-
"agents": ["Legal Researcher"],
|
531 |
-
"description": "Research on relevant legal cases and precedents"
|
532 |
-
},
|
533 |
-
"Risk Assessment": {
|
534 |
-
"query": "Analyze potential legal risks and liabilities in this document.",
|
535 |
-
"agents": ["Contract Analyst", "Legal Strategist"],
|
536 |
-
"description": "Combined risk analysis and strategic assessment"
|
537 |
-
},
|
538 |
-
"Compliance Check": {
|
539 |
-
"query": "Check this document for regulatory compliance issues.",
|
540 |
-
"agents": ["Legal Researcher", "Contract Analyst", "Legal Strategist"],
|
541 |
-
"description": "Comprehensive compliance analysis"
|
542 |
-
},
|
543 |
-
"Custom Query": {
|
544 |
-
"query": None,
|
545 |
-
"agents": ["Legal Researcher", "Contract Analyst", "Legal Strategist"],
|
546 |
-
"description": "Custom analysis using all available agents"
|
547 |
-
}
|
548 |
-
}
|
549 |
-
|
550 |
-
st.info(f"📋 {analysis_configs[analysis_type]['description']}")
|
551 |
-
st.write(f"🤖 Active Legal AI Agents: {', '.join(analysis_configs[analysis_type]['agents'])}") #dictionary!!
|
552 |
-
|
553 |
-
# Replace the existing user_query section with this:
|
554 |
-
if analysis_type == "Custom Query":
|
555 |
-
user_query = st.text_area(
|
556 |
-
"Enter your specific query:",
|
557 |
-
help="Add any specific questions or points you want to analyze"
|
558 |
-
)
|
559 |
-
else:
|
560 |
-
user_query = None # Set to None for non-custom queries
|
561 |
-
|
562 |
-
|
563 |
-
if st.button("Analyze"):
|
564 |
-
if analysis_type == "Custom Query" and not user_query:
|
565 |
-
st.warning("Please enter a query")
|
566 |
-
else:
|
567 |
-
with st.spinner("Analyzing document..."):
|
568 |
-
try:
|
569 |
-
# Ensure OpenAI API key is set
|
570 |
-
os.environ['OPENAI_API_KEY'] = st.session_state.openai_api_key
|
571 |
-
|
572 |
-
# Combine predefined and user queries
|
573 |
-
if analysis_type != "Custom Query":
|
574 |
-
combined_query = f"""
|
575 |
-
Using the uploaded document as reference:
|
576 |
-
|
577 |
-
Primary Analysis Task: {analysis_configs[analysis_type]['query']}
|
578 |
-
Focus Areas: {', '.join(analysis_configs[analysis_type]['agents'])}
|
579 |
-
|
580 |
-
Please search the knowledge base and provide specific references from the document.
|
581 |
-
"""
|
582 |
-
else:
|
583 |
-
combined_query = f"""
|
584 |
-
Using the uploaded document as reference:
|
585 |
-
|
586 |
-
{user_query}
|
587 |
-
|
588 |
-
Please search the knowledge base and provide specific references from the document.
|
589 |
-
Focus Areas: {', '.join(analysis_configs[analysis_type]['agents'])}
|
590 |
-
"""
|
591 |
-
|
592 |
-
response = st.session_state.legal_team.run(combined_query)
|
593 |
-
|
594 |
-
# Display results in tabs
|
595 |
-
tabs = st.tabs(["Analysis", "Key Points", "Recommendations"])
|
596 |
-
|
597 |
-
with tabs[0]:
|
598 |
-
st.markdown("### Detailed Analysis")
|
599 |
-
if response.content:
|
600 |
-
st.markdown(response.content)
|
601 |
-
else:
|
602 |
-
for message in response.messages:
|
603 |
-
if message.role == 'assistant' and message.content:
|
604 |
-
st.markdown(message.content)
|
605 |
-
|
606 |
-
with tabs[1]:
|
607 |
-
st.markdown("### Key Points")
|
608 |
-
key_points_response = st.session_state.legal_team.run(
|
609 |
-
f"""Based on this previous analysis:
|
610 |
-
{response.content}
|
611 |
-
|
612 |
-
Please summarize the key points in bullet points.
|
613 |
-
Focus on insights from: {', '.join(analysis_configs[analysis_type]['agents'])}"""
|
614 |
-
)
|
615 |
-
if key_points_response.content:
|
616 |
-
st.markdown(key_points_response.content)
|
617 |
-
else:
|
618 |
-
for message in key_points_response.messages:
|
619 |
-
if message.role == 'assistant' and message.content:
|
620 |
-
st.markdown(message.content)
|
621 |
-
|
622 |
-
with tabs[2]:
|
623 |
-
st.markdown("### Recommendations")
|
624 |
-
recommendations_response = st.session_state.legal_team.run(
|
625 |
-
f"""Based on this previous analysis:
|
626 |
-
{response.content}
|
627 |
-
|
628 |
-
What are your key recommendations based on the analysis, the best course of action?
|
629 |
-
Provide specific recommendations from: {', '.join(analysis_configs[analysis_type]['agents'])}"""
|
630 |
-
)
|
631 |
-
if recommendations_response.content:
|
632 |
-
st.markdown(recommendations_response.content)
|
633 |
-
else:
|
634 |
-
for message in recommendations_response.messages:
|
635 |
-
if message.role == 'assistant' and message.content:
|
636 |
-
st.markdown(message.content)
|
637 |
-
|
638 |
-
except Exception as e:
|
639 |
-
st.error(f"Error during analysis: {str(e)}")
|
640 |
-
else:
|
641 |
-
st.info("Please upload a legal document to begin analysis")
|
642 |
-
|
643 |
-
if __name__ == "__main__":
|
644 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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