Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,36 @@ from phi.embedder.openai import OpenAIEmbedder
|
|
8 |
import tempfile
|
9 |
import os
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
#initializing the session state variables
|
12 |
def init_session_state():
|
13 |
"""Initialize session state variables"""
|
@@ -32,7 +62,13 @@ def init_qdrant():
|
|
32 |
raise ValueError("Qdrant URL not provided")
|
33 |
|
34 |
return Qdrant(
|
35 |
-
collection="
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
url=st.session_state.qdrant_url,
|
37 |
api_key=st.session_state.qdrant_api_key,
|
38 |
https=True,
|
@@ -63,21 +99,7 @@ def process_document(uploaded_file, vector_db: Qdrant):
|
|
63 |
# Creating knowledge base with explicit Qdrant configuration
|
64 |
knowledge_base = PDFKnowledgeBase(
|
65 |
path=temp_dir,
|
66 |
-
vector_db=vector_db,
|
67 |
-
reader=PDFReader(chunk=True),
|
68 |
-
embedder=embedder,
|
69 |
-
recreate_vector_db=True
|
70 |
-
)
|
71 |
-
knowledge_base.load()
|
72 |
-
return knowledge_base
|
73 |
-
except Exception as e:
|
74 |
-
raise Exception(f"Error processing document: {str(e)}")
|
75 |
-
|
76 |
-
def main():
|
77 |
-
st.set_page_config(page_title="Legal Document Analyzer", layout="wide")
|
78 |
-
init_session_state()
|
79 |
-
|
80 |
-
st.title("AI Legal Agent Team π¨ββοΈ")import streamlit as st
|
81 |
from phi.agent import Agent
|
82 |
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
83 |
from phi.vectordb.qdrant import Qdrant
|
@@ -114,6 +136,20 @@ def init_qdrant():
|
|
114 |
collection="legal_knowledge",
|
115 |
url=st.session_state.qdrant_url,
|
116 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
with st.sidebar:
|
119 |
st.header("π API Configuration")
|
@@ -138,6 +174,22 @@ from phi.embedder.openai import OpenAIEmbedder
|
|
138 |
import tempfile
|
139 |
import os
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
#initializing the session state variables
|
142 |
def init_session_state():
|
143 |
"""Initialize session state variables"""
|
@@ -164,32 +216,7 @@ def init_qdrant():
|
|
164 |
return Qdrant(
|
165 |
collection="legal_knowledge",
|
166 |
url=st.session_state.qdrant_url,
|
167 |
-
main()
|
168 |
-
type="password",
|
169 |
-
value=st.session_state.qdrant_api_key if st.session_state.qdrant_api_key else "",
|
170 |
-
help="Enter your Qdrant API key"
|
171 |
-
)
|
172 |
-
if qdrant_key:
|
173 |
-
st.session_state.qdrant_api_key = qdrant_key
|
174 |
-
|
175 |
-
qdrant_url = st.text_input(
|
176 |
-
"Qdrant URL",import streamlit as st
|
177 |
-
from phi.agent import Agent
|
178 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
179 |
-
from phi.vectordb.qdrant import Qdrant
|
180 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
181 |
-
from phi.model.openai import OpenAIChat
|
182 |
-
from phi.embedder.openai import OpenAIEmbedder
|
183 |
-
import tempfile
|
184 |
-
import os
|
185 |
-
|
186 |
-
#initializing the session state variables
|
187 |
-
def init_session_state():
|
188 |
-
"""Initialize session state variables"""
|
189 |
-
if 'openai_api_key' not in st.session_state:
|
190 |
-
st.session_state.openai_api_key = None
|
191 |
-
if 'qdrant_api_key' not in st.session_state:
|
192 |
-
st.session_state.qdrant_api_key = None
|
193 |
if 'qdrant_url' not in st.session_state:
|
194 |
st.session_state.qdrant_url = None
|
195 |
if 'vector_db' not in st.session_state:
|
@@ -210,6 +237,15 @@ def init_qdrant():
|
|
210 |
collection="legal_knowledge",
|
211 |
url=st.session_state.qdrant_url,
|
212 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
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",
|
214 |
help="Enter your Qdrant instance URL"
|
215 |
)
|
@@ -217,7 +253,13 @@ def init_qdrant():
|
|
217 |
st.session_state.qdrant_url = qdrant_url
|
218 |
|
219 |
if all([st.session_state.qdrant_api_key, st.session_state.qdrant_url]):
|
220 |
-
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
from phi.agent import Agent
|
222 |
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
223 |
from phi.vectordb.qdrant import Qdrant
|
@@ -254,12 +296,6 @@ def init_qdrant():
|
|
254 |
collection="legal_knowledge",
|
255 |
url=st.session_state.qdrant_url,
|
256 |
main()
|
257 |
-
if not st.session_state.vector_db:
|
258 |
-
st.session_state.vector_db = init_qdrant()
|
259 |
-
st.success("Successfully connected to Qdrant!")
|
260 |
-
except Exception as e:
|
261 |
-
st.error(f"Failed to connect to Qdrant: {str(e)}")
|
262 |
-
|
263 |
st.divider()
|
264 |
|
265 |
if all([st.session_state.openai_api_key, st.session_state.vector_db]):
|
@@ -279,43 +315,7 @@ def init_qdrant():
|
|
279 |
model=OpenAIChat(model="gpt-4o"),
|
280 |
tools=[DuckDuckGo()],
|
281 |
knowledge=st.session_state.knowledge_base,
|
282 |
-
|
283 |
-
from phi.agent import Agent
|
284 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
285 |
-
from phi.vectordb.qdrant import Qdrant
|
286 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
287 |
-
from phi.model.openai import OpenAIChat
|
288 |
-
from phi.embedder.openai import OpenAIEmbedder
|
289 |
-
import tempfile
|
290 |
-
import os
|
291 |
-
|
292 |
-
#initializing the session state variables
|
293 |
-
def init_session_state():
|
294 |
-
"""Initialize session state variables"""
|
295 |
-
if 'openai_api_key' not in st.session_state:
|
296 |
-
st.session_state.openai_api_key = None
|
297 |
-
if 'qdrant_api_key' not in st.session_state:
|
298 |
-
st.session_state.qdrant_api_key = None
|
299 |
-
if 'qdrant_url' not in st.session_state:
|
300 |
-
st.session_state.qdrant_url = None
|
301 |
-
if 'vector_db' not in st.session_state:
|
302 |
-
st.session_state.vector_db = None
|
303 |
-
if 'legal_team' not in st.session_state:
|
304 |
-
st.session_state.legal_team = None
|
305 |
-
if 'knowledge_base' not in st.session_state:
|
306 |
-
st.session_state.knowledge_base = None
|
307 |
-
|
308 |
-
def init_qdrant():
|
309 |
-
"""Initialize Qdrant vector database"""
|
310 |
-
if not st.session_state.qdrant_api_key:
|
311 |
-
raise ValueError("Qdrant API key not provided")
|
312 |
-
if not st.session_state.qdrant_url:
|
313 |
-
raise ValueError("Qdrant URL not provided")
|
314 |
-
|
315 |
-
return Qdrant(
|
316 |
-
collection="legal_knowledge",
|
317 |
-
url=st.session_state.qdrant_url,
|
318 |
-
main() wledge=True,
|
319 |
instructions=[
|
320 |
"Find and cite relevant legal cases and precedents",
|
321 |
"Provide detailed research summaries with sources",
|
@@ -333,43 +333,7 @@ def init_qdrant():
|
|
333 |
knowledge=knowledge_base,
|
334 |
search_knowledge=True,
|
335 |
instructions=[
|
336 |
-
"
|
337 |
-
from phi.agent import Agent
|
338 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
339 |
-
from phi.vectordb.qdrant import Qdrant
|
340 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
341 |
-
from phi.model.openai import OpenAIChat
|
342 |
-
from phi.embedder.openai import OpenAIEmbedder
|
343 |
-
import tempfile
|
344 |
-
import os
|
345 |
-
|
346 |
-
#initializing the session state variables
|
347 |
-
def init_session_state():
|
348 |
-
"""Initialize session state variables"""
|
349 |
-
if 'openai_api_key' not in st.session_state:
|
350 |
-
st.session_state.openai_api_key = None
|
351 |
-
if 'qdrant_api_key' not in st.session_state:
|
352 |
-
st.session_state.qdrant_api_key = None
|
353 |
-
if 'qdrant_url' not in st.session_state:
|
354 |
-
st.session_state.qdrant_url = None
|
355 |
-
if 'vector_db' not in st.session_state:
|
356 |
-
st.session_state.vector_db = None
|
357 |
-
if 'legal_team' not in st.session_state:
|
358 |
-
st.session_state.legal_team = None
|
359 |
-
if 'knowledge_base' not in st.session_state:
|
360 |
-
st.session_state.knowledge_base = None
|
361 |
-
|
362 |
-
def init_qdrant():
|
363 |
-
"""Initialize Qdrant vector database"""
|
364 |
-
if not st.session_state.qdrant_api_key:
|
365 |
-
raise ValueError("Qdrant API key not provided")
|
366 |
-
if not st.session_state.qdrant_url:
|
367 |
-
raise ValueError("Qdrant URL not provided")
|
368 |
-
|
369 |
-
return Qdrant(
|
370 |
-
collection="legal_knowledge",
|
371 |
-
url=st.session_state.qdrant_url,
|
372 |
-
main() view contracts thoroughly",
|
373 |
"Identify key terms and potential issues",
|
374 |
"Reference specific clauses from the document"
|
375 |
],
|
@@ -388,7 +352,45 @@ def init_qdrant():
|
|
388 |
"Consider both risks and opportunities"
|
389 |
],
|
390 |
markdown=True
|
391 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
from phi.agent import Agent
|
393 |
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
394 |
from phi.vectordb.qdrant import Qdrant
|
@@ -425,29 +427,15 @@ def init_qdrant():
|
|
425 |
collection="legal_knowledge",
|
426 |
url=st.session_state.qdrant_url,
|
427 |
main()
|
|
|
428 |
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
search_knowledge=True,
|
437 |
-
instructions=[
|
438 |
-
"Coordinate analysis between team members",
|
439 |
-
"Provide comprehensive responses",
|
440 |
-
"Ensure all recommendations are properly sourced",
|
441 |
-
"Reference specific parts of the uploaded document",
|
442 |
-
"Always search the knowledge base before delegating tasks"
|
443 |
-
],
|
444 |
-
show_tool_calls=True,
|
445 |
-
markdown=True
|
446 |
-
)
|
447 |
-
|
448 |
-
st.success("β
Document processed and team initialized!")
|
449 |
-
|
450 |
-
except Exceptioimport streamlit as st
|
451 |
from phi.agent import Agent
|
452 |
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
453 |
from phi.vectordb.qdrant import Qdrant
|
@@ -483,31 +471,7 @@ def init_qdrant():
|
|
483 |
return Qdrant(
|
484 |
collection="legal_knowledge",
|
485 |
url=st.session_state.qdrant_url,
|
486 |
-
main()
|
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": "π",
|
@@ -542,43 +506,7 @@ def init_qdrant():
|
|
542 |
},
|
543 |
"Custom Query": {
|
544 |
"query": None,
|
545 |
-
"agents": ["Legal
|
546 |
-
from phi.agent import Agent
|
547 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
548 |
-
from phi.vectordb.qdrant import Qdrant
|
549 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
550 |
-
from phi.model.openai import OpenAIChat
|
551 |
-
from phi.embedder.openai import OpenAIEmbedder
|
552 |
-
import tempfile
|
553 |
-
import os
|
554 |
-
|
555 |
-
#initializing the session state variables
|
556 |
-
def init_session_state():
|
557 |
-
"""Initialize session state variables"""
|
558 |
-
if 'openai_api_key' not in st.session_state:
|
559 |
-
st.session_state.openai_api_key = None
|
560 |
-
if 'qdrant_api_key' not in st.session_state:
|
561 |
-
st.session_state.qdrant_api_key = None
|
562 |
-
if 'qdrant_url' not in st.session_state:
|
563 |
-
st.session_state.qdrant_url = None
|
564 |
-
if 'vector_db' not in st.session_state:
|
565 |
-
st.session_state.vector_db = None
|
566 |
-
if 'legal_team' not in st.session_state:
|
567 |
-
st.session_state.legal_team = None
|
568 |
-
if 'knowledge_base' not in st.session_state:
|
569 |
-
st.session_state.knowledge_base = None
|
570 |
-
|
571 |
-
def init_qdrant():
|
572 |
-
"""Initialize Qdrant vector database"""
|
573 |
-
if not st.session_state.qdrant_api_key:
|
574 |
-
raise ValueError("Qdrant API key not provided")
|
575 |
-
if not st.session_state.qdrant_url:
|
576 |
-
raise ValueError("Qdrant URL not provided")
|
577 |
-
|
578 |
-
return Qdrant(
|
579 |
-
collection="legal_knowledge",
|
580 |
-
url=st.session_state.qdrant_url,
|
581 |
-
main() esearcher", "Contract Analyst", "Legal Strategist"],
|
582 |
"description": "Custom analysis using all available agents"
|
583 |
}
|
584 |
}
|
@@ -600,43 +528,7 @@ def init_qdrant():
|
|
600 |
if analysis_type == "Custom Query" and not user_query:
|
601 |
st.warning("Please enter a query")
|
602 |
else:
|
603 |
-
with st.spinner("
|
604 |
-
from phi.agent import Agent
|
605 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
606 |
-
from phi.vectordb.qdrant import Qdrant
|
607 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
608 |
-
from phi.model.openai import OpenAIChat
|
609 |
-
from phi.embedder.openai import OpenAIEmbedder
|
610 |
-
import tempfile
|
611 |
-
import os
|
612 |
-
|
613 |
-
#initializing the session state variables
|
614 |
-
def init_session_state():
|
615 |
-
"""Initialize session state variables"""
|
616 |
-
if 'openai_api_key' not in st.session_state:
|
617 |
-
st.session_state.openai_api_key = None
|
618 |
-
if 'qdrant_api_key' not in st.session_state:
|
619 |
-
st.session_state.qdrant_api_key = None
|
620 |
-
if 'qdrant_url' not in st.session_state:
|
621 |
-
st.session_state.qdrant_url = None
|
622 |
-
if 'vector_db' not in st.session_state:
|
623 |
-
st.session_state.vector_db = None
|
624 |
-
if 'legal_team' not in st.session_state:
|
625 |
-
st.session_state.legal_team = None
|
626 |
-
if 'knowledge_base' not in st.session_state:
|
627 |
-
st.session_state.knowledge_base = None
|
628 |
-
|
629 |
-
def init_qdrant():
|
630 |
-
"""Initialize Qdrant vector database"""
|
631 |
-
if not st.session_state.qdrant_api_key:
|
632 |
-
raise ValueError("Qdrant API key not provided")
|
633 |
-
if not st.session_state.qdrant_url:
|
634 |
-
raise ValueError("Qdrant URL not provided")
|
635 |
-
|
636 |
-
return Qdrant(
|
637 |
-
collection="legal_knowledge",
|
638 |
-
url=st.session_state.qdrant_url,
|
639 |
-
main() nalyzing document..."):
|
640 |
try:
|
641 |
# Ensure OpenAI API key is set
|
642 |
os.environ['OPENAI_API_KEY'] = st.session_state.openai_api_key
|
@@ -644,43 +536,7 @@ def init_qdrant():
|
|
644 |
# Combine predefined and user queries
|
645 |
if analysis_type != "Custom Query":
|
646 |
combined_query = f"""
|
647 |
-
Using
|
648 |
-
from phi.agent import Agent
|
649 |
-
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
650 |
-
from phi.vectordb.qdrant import Qdrant
|
651 |
-
from phi.tools.duckduckgo import DuckDuckGo
|
652 |
-
from phi.model.openai import OpenAIChat
|
653 |
-
from phi.embedder.openai import OpenAIEmbedder
|
654 |
-
import tempfile
|
655 |
-
import os
|
656 |
-
|
657 |
-
#initializing the session state variables
|
658 |
-
def init_session_state():
|
659 |
-
"""Initialize session state variables"""
|
660 |
-
if 'openai_api_key' not in st.session_state:
|
661 |
-
st.session_state.openai_api_key = None
|
662 |
-
if 'qdrant_api_key' not in st.session_state:
|
663 |
-
st.session_state.qdrant_api_key = None
|
664 |
-
if 'qdrant_url' not in st.session_state:
|
665 |
-
st.session_state.qdrant_url = None
|
666 |
-
if 'vector_db' not in st.session_state:
|
667 |
-
st.session_state.vector_db = None
|
668 |
-
if 'legal_team' not in st.session_state:
|
669 |
-
st.session_state.legal_team = None
|
670 |
-
if 'knowledge_base' not in st.session_state:
|
671 |
-
st.session_state.knowledge_base = None
|
672 |
-
|
673 |
-
def init_qdrant():
|
674 |
-
"""Initialize Qdrant vector database"""
|
675 |
-
if not st.session_state.qdrant_api_key:
|
676 |
-
raise ValueError("Qdrant API key not provided")
|
677 |
-
if not st.session_state.qdrant_url:
|
678 |
-
raise ValueError("Qdrant URL not provided")
|
679 |
-
|
680 |
-
return Qdrant(
|
681 |
-
collection="legal_knowledge",
|
682 |
-
url=st.session_state.qdrant_url,
|
683 |
-
main() he uploaded document as reference:
|
684 |
|
685 |
Primary Analysis Task: {analysis_configs[analysis_type]['query']}
|
686 |
Focus Areas: {', '.join(analysis_configs[analysis_type]['agents'])}
|
|
|
8 |
import tempfile
|
9 |
import os
|
10 |
|
11 |
+
#initializing the session state variables
|
12 |
+
def init_session_state():
|
13 |
+
"""Initialize session state variables"""
|
14 |
+
if 'openai_api_key' not in st.session_state:
|
15 |
+
st.session_state.openai_api_key = None
|
16 |
+
if 'qdrant_api_key' not in st.session_state:
|
17 |
+
st.session_state.qdrant_api_key = None
|
18 |
+
if 'qdrant_url' not in st.session_state:
|
19 |
+
st.session_state.qdrant_url = None
|
20 |
+
if 'vector_db' not in st.session_state:
|
21 |
+
st.session_state.vector_db = None
|
22 |
+
if 'legal_team' not in st.session_state:
|
23 |
+
st.session_state.legal_team = None
|
24 |
+
if 'knowledge_base' not in st.session_state:
|
25 |
+
st.session_state.knowledge_base = None
|
26 |
+
|
27 |
+
def init_qdrant():
|
28 |
+
"""Initialize Qdrant vector database"""
|
29 |
+
if not st.session_state.qdrant_api_key:
|
30 |
+
raise ValueError("Qdrant API key not provided")
|
31 |
+
if not st.session_state.qdrant_url:import streamlit as st
|
32 |
+
from phi.agent import Agent
|
33 |
+
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
34 |
+
from phi.vectordb.qdrant import Qdrant
|
35 |
+
from phi.tools.duckduckgo import DuckDuckGo
|
36 |
+
from phi.model.openai import OpenAIChat
|
37 |
+
from phi.embedder.openai import OpenAIEmbedder
|
38 |
+
import tempfile
|
39 |
+
import os
|
40 |
+
|
41 |
#initializing the session state variables
|
42 |
def init_session_state():
|
43 |
"""Initialize session state variables"""
|
|
|
62 |
raise ValueError("Qdrant URL not provided")
|
63 |
|
64 |
return Qdrant(
|
65 |
+
collection="legal_knowledge",
|
66 |
+
url=st.session_state.qdrant_url,
|
67 |
+
main()
|
68 |
+
raise ValueError("Qdrant URL not provided")
|
69 |
+
|
70 |
+
return Qdrant(
|
71 |
+
collection="legal_knowledge",
|
72 |
url=st.session_state.qdrant_url,
|
73 |
api_key=st.session_state.qdrant_api_key,
|
74 |
https=True,
|
|
|
99 |
# Creating knowledge base with explicit Qdrant configuration
|
100 |
knowledge_base = PDFKnowledgeBase(
|
101 |
path=temp_dir,
|
102 |
+
vector_db=vector_db, import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
from phi.agent import Agent
|
104 |
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
105 |
from phi.vectordb.qdrant import Qdrant
|
|
|
136 |
collection="legal_knowledge",
|
137 |
url=st.session_state.qdrant_url,
|
138 |
main()
|
139 |
+
reader=PDFReader(chunk=True),
|
140 |
+
embedder=embedder,
|
141 |
+
recreate_vector_db=True
|
142 |
+
)
|
143 |
+
knowledge_base.load()
|
144 |
+
return knowledge_base
|
145 |
+
except Exception as e:
|
146 |
+
raise Exception(f"Error processing document: {str(e)}")
|
147 |
+
|
148 |
+
def main():
|
149 |
+
st.set_page_config(page_title="Legal Document Analyzer", layout="wide")
|
150 |
+
init_session_state()
|
151 |
+
|
152 |
+
st.title("AI Legal Agent Team π¨ββοΈ")
|
153 |
|
154 |
with st.sidebar:
|
155 |
st.header("π API Configuration")
|
|
|
174 |
import tempfile
|
175 |
import os
|
176 |
|
177 |
+
#initializing the session state variables
|
178 |
+
def init_session_state():
|
179 |
+
"""Initialize session state variables"""
|
180 |
+
if 'openai_api_key' not in st.session_state:
|
181 |
+
st.session_state.openai_api_key = None
|
182 |
+
if 'qdrant_api_key' not in st.session_state:
|
183 |
+
st.session_state.qdrant_apiimport streamlit as st
|
184 |
+
from phi.agent import Agent
|
185 |
+
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
186 |
+
from phi.vectordb.qdrant import Qdrant
|
187 |
+
from phi.tools.duckduckgo import DuckDuckGo
|
188 |
+
from phi.model.openai import OpenAIChat
|
189 |
+
from phi.embedder.openai import OpenAIEmbedder
|
190 |
+
import tempfile
|
191 |
+
import os
|
192 |
+
|
193 |
#initializing the session state variables
|
194 |
def init_session_state():
|
195 |
"""Initialize session state variables"""
|
|
|
216 |
return Qdrant(
|
217 |
collection="legal_knowledge",
|
218 |
url=st.session_state.qdrant_url,
|
219 |
+
main() _key = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
if 'qdrant_url' not in st.session_state:
|
221 |
st.session_state.qdrant_url = None
|
222 |
if 'vector_db' not in st.session_state:
|
|
|
237 |
collection="legal_knowledge",
|
238 |
url=st.session_state.qdrant_url,
|
239 |
main()
|
240 |
+
type="password",
|
241 |
+
value=st.session_state.qdrant_api_key if st.session_state.qdrant_api_key else "",
|
242 |
+
help="Enter your Qdrant API key"
|
243 |
+
)
|
244 |
+
if qdrant_key:
|
245 |
+
st.session_state.qdrant_api_key = qdrant_key
|
246 |
+
|
247 |
+
qdrant_url = st.text_input(
|
248 |
+
"Qdrant URL",
|
249 |
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",
|
250 |
help="Enter your Qdrant instance URL"
|
251 |
)
|
|
|
253 |
st.session_state.qdrant_url = qdrant_url
|
254 |
|
255 |
if all([st.session_state.qdrant_api_key, st.session_state.qdrant_url]):
|
256 |
+
try:
|
257 |
+
if not st.session_state.vector_db:
|
258 |
+
st.session_state.vector_db = init_qdrant()
|
259 |
+
st.success("Successfully connected to Qdrant!")
|
260 |
+
except Exception as e:
|
261 |
+
st.error(f"Failed to connect to Qdrant: {str(e)}")
|
262 |
+
import streamlit as st
|
263 |
from phi.agent import Agent
|
264 |
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
265 |
from phi.vectordb.qdrant import Qdrant
|
|
|
296 |
collection="legal_knowledge",
|
297 |
url=st.session_state.qdrant_url,
|
298 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
299 |
st.divider()
|
300 |
|
301 |
if all([st.session_state.openai_api_key, st.session_state.vector_db]):
|
|
|
315 |
model=OpenAIChat(model="gpt-4o"),
|
316 |
tools=[DuckDuckGo()],
|
317 |
knowledge=st.session_state.knowledge_base,
|
318 |
+
search_knowledge=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
319 |
instructions=[
|
320 |
"Find and cite relevant legal cases and precedents",
|
321 |
"Provide detailed research summaries with sources",
|
|
|
333 |
knowledge=knowledge_base,
|
334 |
search_knowledge=True,
|
335 |
instructions=[
|
336 |
+
"Review contracts thoroughly",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
"Identify key terms and potential issues",
|
338 |
"Reference specific clauses from the document"
|
339 |
],
|
|
|
352 |
"Consider both risks and opportunities"
|
353 |
],
|
354 |
markdown=True
|
355 |
+
)
|
356 |
+
|
357 |
+
# Legal Agent Team
|
358 |
+
st.session_state.legal_team = Agent(
|
359 |
+
name="Legal Team Lead",
|
360 |
+
role="Legal team coordinator",
|
361 |
+
model=OpenAIChat(model="gpt-4o"),
|
362 |
+
team=[legal_researcher, contract_analyst, legal_strategist],
|
363 |
+
knowledge=st.session_state.knowledge_base,
|
364 |
+
search_knowledge=True,
|
365 |
+
instructions=[
|
366 |
+
"Coordinate analysis between team members",
|
367 |
+
"Provide comprehensive responses",
|
368 |
+
"Ensure all recommendations are properly sourced",
|
369 |
+
"Reference specific parts of the uploaded document",
|
370 |
+
"Always search the knowledge base before delegating tasks"
|
371 |
+
],
|
372 |
+
show_tool_calls=True,
|
373 |
+
markdown=True
|
374 |
+
)
|
375 |
+
|
376 |
+
st.success("β
Document processed and team initialized!")
|
377 |
+
|
378 |
+
except Exception as e:
|
379 |
+
st.error(f"Error processing document: {str(e)}")
|
380 |
+
|
381 |
+
st.divider()
|
382 |
+
st.header("π Analysis Options")
|
383 |
+
analysis_type = st.selectbox(
|
384 |
+
"Select Analysis Type",
|
385 |
+
[
|
386 |
+
"Contract Review",
|
387 |
+
"Legal Research",
|
388 |
+
"Risk Assessment",
|
389 |
+
"Compliance Check",
|
390 |
+
"Custom Query"
|
391 |
+
]
|
392 |
+
)
|
393 |
+
else:import streamlit as st
|
394 |
from phi.agent import Agent
|
395 |
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
396 |
from phi.vectordb.qdrant import Qdrant
|
|
|
427 |
collection="legal_knowledge",
|
428 |
url=st.session_state.qdrant_url,
|
429 |
main()
|
430 |
+
st.warning("Please configure all API credentials to proceed")
|
431 |
|
432 |
+
# Main content area
|
433 |
+
if not all([st.session_state.openai_api_key, st.session_state.vector_db]):
|
434 |
+
st.info("π Please configure your API credentials in the sidebar to begin")
|
435 |
+
elif not uploaded_file:
|
436 |
+
st.info("π Please upload a legal document to begin analysis")
|
437 |
+
elif st.session_state.legal_team:
|
438 |
+
# Create a dictionary for import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
439 |
from phi.agent import Agent
|
440 |
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
|
441 |
from phi.vectordb.qdrant import Qdrant
|
|
|
471 |
return Qdrant(
|
472 |
collection="legal_knowledge",
|
473 |
url=st.session_state.qdrant_url,
|
474 |
+
main() analysis type icons
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
475 |
analysis_icons = {
|
476 |
"Contract Review": "π",
|
477 |
"Legal Research": "π",
|
|
|
506 |
},
|
507 |
"Custom Query": {
|
508 |
"query": None,
|
509 |
+
"agents": ["Legal Researcher", "Contract Analyst", "Legal Strategist"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
510 |
"description": "Custom analysis using all available agents"
|
511 |
}
|
512 |
}
|
|
|
528 |
if analysis_type == "Custom Query" and not user_query:
|
529 |
st.warning("Please enter a query")
|
530 |
else:
|
531 |
+
with st.spinner("Analyzing document..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
532 |
try:
|
533 |
# Ensure OpenAI API key is set
|
534 |
os.environ['OPENAI_API_KEY'] = st.session_state.openai_api_key
|
|
|
536 |
# Combine predefined and user queries
|
537 |
if analysis_type != "Custom Query":
|
538 |
combined_query = f"""
|
539 |
+
Using the uploaded document as reference:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
540 |
|
541 |
Primary Analysis Task: {analysis_configs[analysis_type]['query']}
|
542 |
Focus Areas: {', '.join(analysis_configs[analysis_type]['agents'])}
|