Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,10 @@ import streamlit as st
|
|
| 2 |
from utils.vector_store import VectorStore
|
| 3 |
from utils.document_processor import DocumentProcessor
|
| 4 |
from utils.case_manager import CaseManager
|
| 5 |
-
from utils.
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Initialize components
|
| 10 |
-
case_manager = CaseManager()
|
| 11 |
-
doc_processor = DocumentProcessor()
|
| 12 |
-
vector_store = VectorStore()
|
| 13 |
-
prompt_generator = LegalPromptGenerator() # Add this
|
| 14 |
-
chat_interface = ChatInterface(case_manager, vector_store, doc_processor) # Pass components here
|
| 15 |
-
|
| 16 |
|
| 17 |
# Page configuration
|
| 18 |
st.set_page_config(
|
|
@@ -22,9 +15,40 @@ st.set_page_config(
|
|
| 22 |
initial_sidebar_state="expanded"
|
| 23 |
)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def main():
|
| 27 |
-
# Header
|
| 28 |
st.markdown(
|
| 29 |
"""
|
| 30 |
<style>
|
|
@@ -47,6 +71,13 @@ def main():
|
|
| 47 |
font-size: 14px;
|
| 48 |
color: #A9A9A9;
|
| 49 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
</style>
|
| 51 |
<h1 class="main-header">SuoMoto.AI</h1>
|
| 52 |
<p class="main-tagline">Empowering Legal Intelligence: Automate, Analyze, Act.</p>
|
|
@@ -54,29 +85,41 @@ def main():
|
|
| 54 |
unsafe_allow_html=True
|
| 55 |
)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Sidebar navigation
|
| 58 |
st.sidebar.title("SuoMoto.AI ⚖️")
|
| 59 |
st.sidebar.markdown(
|
| 60 |
"""
|
| 61 |
-
Streamline legal workflows with advanced AI-powered tools for document
|
|
|
|
| 62 |
"""
|
| 63 |
)
|
|
|
|
| 64 |
tab = st.sidebar.radio(
|
| 65 |
"Navigation",
|
| 66 |
-
["📁 Case Manager", "📄 Document
|
| 67 |
)
|
| 68 |
|
| 69 |
# Tab 1: Case Manager
|
| 70 |
if tab == "📁 Case Manager":
|
| 71 |
st.title("📁 Manage Cases")
|
| 72 |
-
st.markdown("
|
| 73 |
|
| 74 |
# Case creation form
|
| 75 |
with st.expander("➕ Create New Case", expanded=True):
|
| 76 |
with st.form("create_case_form"):
|
| 77 |
title = st.text_input("Case Title")
|
| 78 |
description = st.text_area("Description", placeholder="Enter case details...")
|
| 79 |
-
case_type = st.selectbox(
|
|
|
|
|
|
|
|
|
|
| 80 |
create_case = st.form_submit_button("Create Case")
|
| 81 |
|
| 82 |
if create_case and title:
|
|
@@ -104,139 +147,81 @@ def main():
|
|
| 104 |
|
| 105 |
# Upload new document
|
| 106 |
uploaded_file = st.file_uploader(
|
| 107 |
-
f"Upload Document to Case: {case['title']}",
|
|
|
|
| 108 |
)
|
| 109 |
if uploaded_file:
|
| 110 |
with st.spinner("Processing document..."):
|
| 111 |
text, chunks, metadata = doc_processor.process_and_tag_document(uploaded_file)
|
| 112 |
-
doc_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
case_manager.add_document(case["id"], doc_data)
|
| 114 |
st.success(f"Document '{uploaded_file.name}' added to case!")
|
| 115 |
|
| 116 |
-
# Tab 2: Document
|
| 117 |
-
elif tab == "📄 Document
|
| 118 |
-
st.title("📄
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
-
if search_query:
|
| 124 |
-
results = case_manager.search(search_query)
|
| 125 |
-
st.subheader("Search Results")
|
| 126 |
-
if results:
|
| 127 |
-
for result in results:
|
| 128 |
-
with st.expander(f"**{result['title']}**"):
|
| 129 |
-
st.write(f"**Type**: {result.get('metadata', {}).get('type', 'Unknown')}")
|
| 130 |
-
st.write(f"**Jurisdiction**: {result.get('metadata', {}).get('jurisdiction', 'Unknown')}")
|
| 131 |
-
st.write(f"**Uploaded On**: {result.get('metadata', {}).get('uploaded_at', 'Unknown')}")
|
| 132 |
-
st.write(f"**Preview**: {result.get('text', '')[:300]}...")
|
| 133 |
-
else:
|
| 134 |
-
st.warning("No results found for your search.")
|
| 135 |
-
else:
|
| 136 |
-
st.subheader("📚 All Documents")
|
| 137 |
-
cases = case_manager.get_all_cases()
|
| 138 |
-
if cases:
|
| 139 |
-
for case in cases:
|
| 140 |
-
st.markdown(f"### Case: {case['title']}")
|
| 141 |
-
for doc in case_manager.list_documents(case["id"]):
|
| 142 |
-
with st.expander(f"Document: {doc.get('title', 'Unknown Title')}"):
|
| 143 |
-
st.markdown(
|
| 144 |
-
f"""
|
| 145 |
-
**Type**: {doc.get('metadata', {}).get('type', 'Unknown')}
|
| 146 |
-
**Jurisdiction**: {doc.get('metadata', {}).get('jurisdiction', 'Unknown')}
|
| 147 |
-
**Uploaded On**: {doc.get('metadata', {}).get('uploaded_at', 'Unknown')}
|
| 148 |
-
"""
|
| 149 |
-
)
|
| 150 |
-
st.markdown(f"**Content Preview**: {doc.get('text', '')[:500]}...")
|
| 151 |
-
else:
|
| 152 |
-
st.info("No cases available.")
|
| 153 |
-
|
| 154 |
-
# Tab 3: Chat
|
| 155 |
-
elif tab == "🤖 Chat":
|
| 156 |
-
st.markdown(
|
| 157 |
-
"""
|
| 158 |
-
<style>
|
| 159 |
-
.chat-container {
|
| 160 |
-
background-color: #f9f9f9;
|
| 161 |
-
border-radius: 10px;
|
| 162 |
-
padding: 20px;
|
| 163 |
-
max-height: 600px;
|
| 164 |
-
overflow-y: auto;
|
| 165 |
-
}
|
| 166 |
-
.user-message {
|
| 167 |
-
text-align: right;
|
| 168 |
-
color: #ffffff;
|
| 169 |
-
background-color: #2B547E;
|
| 170 |
-
padding: 10px;
|
| 171 |
-
border-radius: 10px;
|
| 172 |
-
margin-bottom: 10px;
|
| 173 |
-
max-width: 70%;
|
| 174 |
-
margin-left: auto;
|
| 175 |
-
}
|
| 176 |
-
.assistant-message {
|
| 177 |
-
text-align: left;
|
| 178 |
-
color: #000000;
|
| 179 |
-
background-color: #e3e3e3;
|
| 180 |
-
padding: 10px;
|
| 181 |
-
border-radius: 10px;
|
| 182 |
-
margin-bottom: 10px;
|
| 183 |
-
max-width: 70%;
|
| 184 |
-
margin-right: auto;
|
| 185 |
-
}
|
| 186 |
-
</style>
|
| 187 |
-
""",
|
| 188 |
-
unsafe_allow_html=True,
|
| 189 |
-
)
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
)
|
| 198 |
|
| 199 |
-
if
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
role_class = "user-message" if message["role"] == "user" else "assistant-message"
|
| 207 |
-
st.markdown(f'<div class="{role_class}">{message["content"]}</div>', unsafe_allow_html=True)
|
| 208 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 209 |
|
| 210 |
-
#
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
st.session_state["chat_history"] = chat_history
|
| 215 |
-
|
| 216 |
-
with st.spinner("Generating response..."):
|
| 217 |
-
context = selected_document.get('text', '')
|
| 218 |
-
if not context:
|
| 219 |
-
st.error("The selected document has no text content.")
|
| 220 |
-
else:
|
| 221 |
-
response = vector_store.chat_with_context(query, context)
|
| 222 |
-
chat_history.append({"role": "assistant", "content": response})
|
| 223 |
-
st.session_state["chat_history"] = chat_history
|
| 224 |
-
|
| 225 |
-
# Tab 4: Generate Templates
|
| 226 |
-
elif tab == "📝 Generate Templates":
|
| 227 |
-
st.title("📝 Legal Document Templates")
|
| 228 |
-
st.markdown("Generate standard legal documents using predefined templates.")
|
| 229 |
-
render_template_generator()
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
st.markdown(
|
| 232 |
"""
|
| 233 |
<div class="footer">
|
| 234 |
-
|
| 235 |
</div>
|
| 236 |
""",
|
| 237 |
unsafe_allow_html=True
|
| 238 |
)
|
| 239 |
|
| 240 |
-
|
| 241 |
if __name__ == "__main__":
|
| 242 |
-
main()
|
|
|
|
| 2 |
from utils.vector_store import VectorStore
|
| 3 |
from utils.document_processor import DocumentProcessor
|
| 4 |
from utils.case_manager import CaseManager
|
| 5 |
+
from utils.legal_notebook_interface import LegalNotebookInterface
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import os
|
| 8 |
+
import nltk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Page configuration
|
| 11 |
st.set_page_config(
|
|
|
|
| 15 |
initial_sidebar_state="expanded"
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# Initialize NLTK data directory
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def initialize_nltk():
|
| 21 |
+
nltk_data_dir = os.path.join(os.getcwd(), "data", "nltk_data")
|
| 22 |
+
os.makedirs(nltk_data_dir, exist_ok=True)
|
| 23 |
+
nltk.data.path.append(nltk_data_dir)
|
| 24 |
+
|
| 25 |
+
resources = ['punkt', 'stopwords']
|
| 26 |
+
for resource in resources:
|
| 27 |
+
try:
|
| 28 |
+
nltk.download(resource, download_dir=nltk_data_dir, quiet=True)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Error downloading NLTK resource {resource}: {str(e)}")
|
| 31 |
+
return True
|
| 32 |
+
|
| 33 |
+
# Initialize components
|
| 34 |
+
@st.cache_resource
|
| 35 |
+
def init_components():
|
| 36 |
+
# Initialize NLTK first
|
| 37 |
+
initialize_nltk()
|
| 38 |
+
|
| 39 |
+
# Set up data directory
|
| 40 |
+
data_dir = os.path.join(os.getcwd(), "data")
|
| 41 |
+
os.makedirs(data_dir, exist_ok=True)
|
| 42 |
+
|
| 43 |
+
# Initialize components
|
| 44 |
+
case_manager = CaseManager(base_path=os.path.join(data_dir, "cases"))
|
| 45 |
+
vector_store = VectorStore(storage_path=os.path.join(data_dir, "vectors"))
|
| 46 |
+
doc_processor = DocumentProcessor(base_path=data_dir)
|
| 47 |
+
|
| 48 |
+
return case_manager, vector_store, doc_processor
|
| 49 |
|
| 50 |
def main():
|
| 51 |
+
# Header
|
| 52 |
st.markdown(
|
| 53 |
"""
|
| 54 |
<style>
|
|
|
|
| 71 |
font-size: 14px;
|
| 72 |
color: #A9A9A9;
|
| 73 |
}
|
| 74 |
+
.stButton>button {
|
| 75 |
+
background-color: #2B547E;
|
| 76 |
+
color: white;
|
| 77 |
+
}
|
| 78 |
+
.stTextInput>div>div>input {
|
| 79 |
+
background-color: #f0f2f6;
|
| 80 |
+
}
|
| 81 |
</style>
|
| 82 |
<h1 class="main-header">SuoMoto.AI</h1>
|
| 83 |
<p class="main-tagline">Empowering Legal Intelligence: Automate, Analyze, Act.</p>
|
|
|
|
| 85 |
unsafe_allow_html=True
|
| 86 |
)
|
| 87 |
|
| 88 |
+
# Initialize components with proper error handling
|
| 89 |
+
try:
|
| 90 |
+
case_manager, vector_store, doc_processor = init_components()
|
| 91 |
+
except Exception as e:
|
| 92 |
+
st.error(f"Error initializing components: {str(e)}")
|
| 93 |
+
st.stop()
|
| 94 |
+
|
| 95 |
# Sidebar navigation
|
| 96 |
st.sidebar.title("SuoMoto.AI ⚖️")
|
| 97 |
st.sidebar.markdown(
|
| 98 |
"""
|
| 99 |
+
Streamline legal workflows with advanced AI-powered tools for document
|
| 100 |
+
analysis and management.
|
| 101 |
"""
|
| 102 |
)
|
| 103 |
+
|
| 104 |
tab = st.sidebar.radio(
|
| 105 |
"Navigation",
|
| 106 |
+
["📁 Case Manager", "📄 Document Analysis", "🤖 Legal Assistant"]
|
| 107 |
)
|
| 108 |
|
| 109 |
# Tab 1: Case Manager
|
| 110 |
if tab == "📁 Case Manager":
|
| 111 |
st.title("📁 Manage Cases")
|
| 112 |
+
st.markdown("Create and manage legal cases with linked documents.")
|
| 113 |
|
| 114 |
# Case creation form
|
| 115 |
with st.expander("➕ Create New Case", expanded=True):
|
| 116 |
with st.form("create_case_form"):
|
| 117 |
title = st.text_input("Case Title")
|
| 118 |
description = st.text_area("Description", placeholder="Enter case details...")
|
| 119 |
+
case_type = st.selectbox(
|
| 120 |
+
"Case Type",
|
| 121 |
+
["Judgment", "Contract", "MOU", "Will", "Other"]
|
| 122 |
+
)
|
| 123 |
create_case = st.form_submit_button("Create Case")
|
| 124 |
|
| 125 |
if create_case and title:
|
|
|
|
| 147 |
|
| 148 |
# Upload new document
|
| 149 |
uploaded_file = st.file_uploader(
|
| 150 |
+
f"Upload Document to Case: {case['title']}",
|
| 151 |
+
key=f"upload_{case['id']}"
|
| 152 |
)
|
| 153 |
if uploaded_file:
|
| 154 |
with st.spinner("Processing document..."):
|
| 155 |
text, chunks, metadata = doc_processor.process_and_tag_document(uploaded_file)
|
| 156 |
+
doc_data = {
|
| 157 |
+
"title": uploaded_file.name,
|
| 158 |
+
"text": text,
|
| 159 |
+
"metadata": metadata
|
| 160 |
+
}
|
| 161 |
case_manager.add_document(case["id"], doc_data)
|
| 162 |
st.success(f"Document '{uploaded_file.name}' added to case!")
|
| 163 |
|
| 164 |
+
# Tab 2: Document Analysis
|
| 165 |
+
elif tab == "📄 Document Analysis":
|
| 166 |
+
st.title("📄 Document Analysis")
|
| 167 |
+
# Initialize notebook interface
|
| 168 |
+
notebook = LegalNotebookInterface(case_manager, vector_store, doc_processor)
|
| 169 |
+
# Render the notebook interface
|
| 170 |
+
notebook.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
# Tab 3: Legal Assistant
|
| 173 |
+
elif tab == "🤖 Legal Assistant":
|
| 174 |
+
st.title("🤖 AI Legal Assistant")
|
| 175 |
+
st.markdown("Chat with your AI legal assistant about cases and documents.")
|
| 176 |
|
| 177 |
+
# Select case and documents
|
| 178 |
+
cases = case_manager.get_all_cases()
|
| 179 |
+
if not cases:
|
| 180 |
+
st.warning("Please create a case and add documents first.")
|
| 181 |
+
return
|
| 182 |
+
|
| 183 |
+
selected_case = st.selectbox(
|
| 184 |
+
"Select Case",
|
| 185 |
+
cases,
|
| 186 |
+
format_func=lambda x: x['title']
|
| 187 |
)
|
| 188 |
|
| 189 |
+
if selected_case:
|
| 190 |
+
documents = case_manager.list_documents(selected_case['id'])
|
| 191 |
+
if not documents:
|
| 192 |
+
st.warning("Please add documents to the case first.")
|
| 193 |
+
return
|
| 194 |
|
| 195 |
+
# Chat interface
|
| 196 |
+
if "messages" not in st.session_state:
|
| 197 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
# Display chat history
|
| 200 |
+
for message in st.session_state.messages:
|
| 201 |
+
with st.chat_message(message["role"]):
|
| 202 |
+
st.markdown(message["content"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
# Chat input
|
| 205 |
+
if prompt := st.chat_input("Ask about your legal documents..."):
|
| 206 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 207 |
+
with st.chat_message("user"):
|
| 208 |
+
st.markdown(prompt)
|
| 209 |
+
|
| 210 |
+
with st.chat_message("assistant"):
|
| 211 |
+
response = f"Analysis of your query: {prompt}\n\n"
|
| 212 |
+
response += "Based on the documents in this case..."
|
| 213 |
+
st.markdown(response)
|
| 214 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 215 |
+
|
| 216 |
+
# Footer
|
| 217 |
st.markdown(
|
| 218 |
"""
|
| 219 |
<div class="footer">
|
| 220 |
+
Powered by SuoMoto.AI
|
| 221 |
</div>
|
| 222 |
""",
|
| 223 |
unsafe_allow_html=True
|
| 224 |
)
|
| 225 |
|
|
|
|
| 226 |
if __name__ == "__main__":
|
| 227 |
+
main()
|