Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
from pathlib import Path
|
| 5 |
from components.template_generator import render_template_generator
|
|
|
|
|
|
|
| 6 |
from utils.document_processor import DocumentProcessor
|
| 7 |
import anthropic
|
| 8 |
|
|
@@ -16,31 +17,28 @@ st.set_page_config(
|
|
| 16 |
|
| 17 |
# Initialize Anthropic client
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
api_key = os.environ.get('ANTHROPIC_API_KEY')
|
| 21 |
if not api_key:
|
| 22 |
-
st.error("Please set the ANTHROPIC_API_KEY in your
|
| 23 |
st.stop()
|
| 24 |
-
|
| 25 |
client = anthropic.Anthropic(api_key=api_key)
|
| 26 |
except Exception as e:
|
| 27 |
-
st.error(f"Error initializing Anthropic client:
|
| 28 |
st.stop()
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
if
|
| 32 |
st.session_state.processed_docs = {}
|
| 33 |
-
if
|
| 34 |
st.session_state.chat_history = []
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
base_path.mkdir(parents=True, exist_ok=True)
|
| 40 |
-
return base_path
|
| 41 |
|
| 42 |
def analyze_document(text: str) -> str:
|
| 43 |
-
"""Analyze document using Claude"""
|
| 44 |
try:
|
| 45 |
message = client.messages.create(
|
| 46 |
model="claude-3-sonnet-20240229",
|
|
@@ -63,8 +61,9 @@ def analyze_document(text: str) -> str:
|
|
| 63 |
st.error(f"Error during analysis: {str(e)}")
|
| 64 |
return ""
|
| 65 |
|
|
|
|
| 66 |
def chat_with_docs(query: str, context: str) -> str:
|
| 67 |
-
"""Chat with documents using Claude"""
|
| 68 |
try:
|
| 69 |
message = client.messages.create(
|
| 70 |
model="claude-3-sonnet-20240229",
|
|
@@ -73,9 +72,9 @@ def chat_with_docs(query: str, context: str) -> str:
|
|
| 73 |
messages=[{
|
| 74 |
"role": "user",
|
| 75 |
"content": f"""Based on the following context, please answer the question.
|
| 76 |
-
|
| 77 |
Context: {context}
|
| 78 |
-
|
| 79 |
Question: {query}"""
|
| 80 |
}]
|
| 81 |
)
|
|
@@ -84,104 +83,97 @@ def chat_with_docs(query: str, context: str) -> str:
|
|
| 84 |
st.error(f"Error generating response: {str(e)}")
|
| 85 |
return ""
|
| 86 |
|
|
|
|
| 87 |
def render_sidebar():
|
| 88 |
-
"""Render sidebar with navigation options"""
|
| 89 |
with st.sidebar:
|
| 90 |
st.title("Legal AI Assistant")
|
| 91 |
-
|
| 92 |
# Navigation
|
| 93 |
selected_option = st.radio(
|
| 94 |
"Navigation",
|
| 95 |
-
["
|
| 96 |
)
|
| 97 |
-
|
| 98 |
return selected_option
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
def main():
|
| 101 |
-
# Initialize folders
|
| 102 |
-
base_path = initialize_case_folders()
|
| 103 |
-
|
| 104 |
-
# Initialize components
|
| 105 |
-
doc_processor = DocumentProcessor()
|
| 106 |
-
|
| 107 |
# Render sidebar and get selected option
|
| 108 |
selected_option = render_sidebar()
|
| 109 |
-
|
| 110 |
-
if selected_option == "
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# File upload
|
| 114 |
-
uploaded_file = st.file_uploader(
|
| 115 |
-
"Upload a legal document",
|
| 116 |
-
type=['pdf', 'docx', 'txt']
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
if uploaded_file:
|
| 120 |
-
with st.spinner("Processing document..."):
|
| 121 |
-
# Process document
|
| 122 |
-
text, chunks = doc_processor.process_document(uploaded_file)
|
| 123 |
-
|
| 124 |
-
if text:
|
| 125 |
-
st.success("Document processed successfully!")
|
| 126 |
-
|
| 127 |
-
# Store in session state
|
| 128 |
-
st.session_state.processed_docs[uploaded_file.name] = {
|
| 129 |
-
'text': text,
|
| 130 |
-
'chunks': chunks
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
# Show document content
|
| 134 |
-
with st.expander("Document Content", expanded=False):
|
| 135 |
-
st.text(text[:1000] + "..." if len(text) > 1000 else text)
|
| 136 |
-
|
| 137 |
-
# Analysis button
|
| 138 |
-
if st.button("Analyze Document"):
|
| 139 |
-
with st.spinner("Analyzing..."):
|
| 140 |
-
analysis = analyze_document(text)
|
| 141 |
-
if analysis:
|
| 142 |
-
st.markdown("### Analysis Results")
|
| 143 |
-
st.markdown(analysis)
|
| 144 |
-
|
| 145 |
-
# Download button
|
| 146 |
-
st.download_button(
|
| 147 |
-
"Download Analysis",
|
| 148 |
-
analysis,
|
| 149 |
-
file_name=f"analysis_{uploaded_file.name}.txt",
|
| 150 |
-
mime="text/plain"
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
elif selected_option == "Document Generation":
|
| 154 |
# Render template generator
|
| 155 |
render_template_generator()
|
| 156 |
-
|
| 157 |
-
else: # Chat with Documents
|
| 158 |
st.header("Chat with Documents")
|
| 159 |
-
|
| 160 |
if not st.session_state.processed_docs:
|
| 161 |
-
st.warning("Please upload some documents in the
|
| 162 |
return
|
| 163 |
-
|
| 164 |
# Chat interface
|
| 165 |
query = st.text_input("Ask a question about your documents:")
|
| 166 |
if query:
|
| 167 |
# Combine all document texts as context
|
| 168 |
context = "\n\n".join([doc['text'] for doc in st.session_state.processed_docs.values()])
|
| 169 |
-
|
| 170 |
with st.spinner("Thinking..."):
|
| 171 |
response = chat_with_docs(query, context)
|
| 172 |
-
|
| 173 |
# Add to chat history
|
| 174 |
st.session_state.chat_history.append({
|
| 175 |
"query": query,
|
| 176 |
"response": response
|
| 177 |
})
|
| 178 |
-
|
| 179 |
# Display chat history
|
| 180 |
for chat in st.session_state.chat_history:
|
| 181 |
with st.container():
|
| 182 |
-
st.markdown("**You:**
|
| 183 |
-
st.markdown("**Assistant:**
|
| 184 |
st.markdown("---")
|
| 185 |
|
|
|
|
| 186 |
if __name__ == "__main__":
|
| 187 |
-
main()
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
from pathlib import Path
|
| 4 |
from components.template_generator import render_template_generator
|
| 5 |
+
from components.document_viewer import DocumentViewer
|
| 6 |
+
from utils.case_manager import CaseManager
|
| 7 |
from utils.document_processor import DocumentProcessor
|
| 8 |
import anthropic
|
| 9 |
|
|
|
|
| 17 |
|
| 18 |
# Initialize Anthropic client
|
| 19 |
try:
|
| 20 |
+
api_key = os.getenv("ANTHROPIC_API_KEY")
|
|
|
|
| 21 |
if not api_key:
|
| 22 |
+
st.error("Please set the ANTHROPIC_API_KEY in your environment variables.")
|
| 23 |
st.stop()
|
| 24 |
+
|
| 25 |
client = anthropic.Anthropic(api_key=api_key)
|
| 26 |
except Exception as e:
|
| 27 |
+
st.error(f"Error initializing Anthropic client: {str(e)}")
|
| 28 |
st.stop()
|
| 29 |
|
| 30 |
+
# Initialize session state
|
| 31 |
+
if "processed_docs" not in st.session_state:
|
| 32 |
st.session_state.processed_docs = {}
|
| 33 |
+
if "chat_history" not in st.session_state:
|
| 34 |
st.session_state.chat_history = []
|
| 35 |
|
| 36 |
+
# Initialize CaseManager
|
| 37 |
+
case_manager = CaseManager()
|
| 38 |
+
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def analyze_document(text: str) -> str:
|
| 41 |
+
"""Analyze document using Claude."""
|
| 42 |
try:
|
| 43 |
message = client.messages.create(
|
| 44 |
model="claude-3-sonnet-20240229",
|
|
|
|
| 61 |
st.error(f"Error during analysis: {str(e)}")
|
| 62 |
return ""
|
| 63 |
|
| 64 |
+
|
| 65 |
def chat_with_docs(query: str, context: str) -> str:
|
| 66 |
+
"""Chat with documents using Claude."""
|
| 67 |
try:
|
| 68 |
message = client.messages.create(
|
| 69 |
model="claude-3-sonnet-20240229",
|
|
|
|
| 72 |
messages=[{
|
| 73 |
"role": "user",
|
| 74 |
"content": f"""Based on the following context, please answer the question.
|
| 75 |
+
|
| 76 |
Context: {context}
|
| 77 |
+
|
| 78 |
Question: {query}"""
|
| 79 |
}]
|
| 80 |
)
|
|
|
|
| 83 |
st.error(f"Error generating response: {str(e)}")
|
| 84 |
return ""
|
| 85 |
|
| 86 |
+
|
| 87 |
def render_sidebar():
|
| 88 |
+
"""Render sidebar with navigation options."""
|
| 89 |
with st.sidebar:
|
| 90 |
st.title("Legal AI Assistant")
|
| 91 |
+
|
| 92 |
# Navigation
|
| 93 |
selected_option = st.radio(
|
| 94 |
"Navigation",
|
| 95 |
+
["๐ Manage Cases", "๐ Document Generation", "๐ค Chat with Documents"]
|
| 96 |
)
|
|
|
|
| 97 |
return selected_option
|
| 98 |
|
| 99 |
+
|
| 100 |
+
def manage_cases():
|
| 101 |
+
"""Case management interface."""
|
| 102 |
+
st.header("Manage Cases")
|
| 103 |
+
st.write("Create, view, and manage cases and their associated documents.")
|
| 104 |
+
|
| 105 |
+
# Case creation form
|
| 106 |
+
with st.expander("Create New Case"):
|
| 107 |
+
with st.form("create_case_form"):
|
| 108 |
+
title = st.text_input("Case Title")
|
| 109 |
+
description = st.text_area("Description")
|
| 110 |
+
case_type = st.selectbox("Case Type", ["Contract", "Litigation", "Employment", "Other"])
|
| 111 |
+
create_case = st.form_submit_button("Create Case")
|
| 112 |
+
|
| 113 |
+
if create_case and title:
|
| 114 |
+
case_id = case_manager.create_case(title, description, case_type)
|
| 115 |
+
st.success(f"Case '{title}' created successfully!")
|
| 116 |
+
|
| 117 |
+
# List all cases
|
| 118 |
+
cases = case_manager.get_all_cases()
|
| 119 |
+
if cases:
|
| 120 |
+
for case in cases:
|
| 121 |
+
with st.expander(f"Case: {case['title']}"):
|
| 122 |
+
st.write(f"**Description**: {case['description']}")
|
| 123 |
+
st.write(f"**Type**: {case['case_type']}")
|
| 124 |
+
st.write(f"**Created At**: {case['created_at']}")
|
| 125 |
+
|
| 126 |
+
# List documents in case
|
| 127 |
+
documents = case_manager.list_documents(case["id"])
|
| 128 |
+
for doc in documents:
|
| 129 |
+
st.markdown(f"- **{doc['title']}** (Added: {doc['added_at']})")
|
| 130 |
+
|
| 131 |
+
# Upload new documents
|
| 132 |
+
uploaded_file = st.file_uploader("Upload Document", key=f"upload_{case['id']}")
|
| 133 |
+
if uploaded_file:
|
| 134 |
+
doc_data = {"title": uploaded_file.name, "content": uploaded_file.read().decode("utf-8")}
|
| 135 |
+
case_manager.add_document(case["id"], doc_data)
|
| 136 |
+
st.success(f"Document '{uploaded_file.name}' added to case.")
|
| 137 |
+
|
| 138 |
+
|
| 139 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
# Render sidebar and get selected option
|
| 141 |
selected_option = render_sidebar()
|
| 142 |
+
|
| 143 |
+
if selected_option == "๐ Manage Cases":
|
| 144 |
+
manage_cases()
|
| 145 |
+
elif selected_option == "๐ Document Generation":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
# Render template generator
|
| 147 |
render_template_generator()
|
| 148 |
+
elif selected_option == "๐ค Chat with Documents":
|
|
|
|
| 149 |
st.header("Chat with Documents")
|
| 150 |
+
|
| 151 |
if not st.session_state.processed_docs:
|
| 152 |
+
st.warning("Please upload and process some documents in the Manage Cases section first.")
|
| 153 |
return
|
| 154 |
+
|
| 155 |
# Chat interface
|
| 156 |
query = st.text_input("Ask a question about your documents:")
|
| 157 |
if query:
|
| 158 |
# Combine all document texts as context
|
| 159 |
context = "\n\n".join([doc['text'] for doc in st.session_state.processed_docs.values()])
|
| 160 |
+
|
| 161 |
with st.spinner("Thinking..."):
|
| 162 |
response = chat_with_docs(query, context)
|
| 163 |
+
|
| 164 |
# Add to chat history
|
| 165 |
st.session_state.chat_history.append({
|
| 166 |
"query": query,
|
| 167 |
"response": response
|
| 168 |
})
|
| 169 |
+
|
| 170 |
# Display chat history
|
| 171 |
for chat in st.session_state.chat_history:
|
| 172 |
with st.container():
|
| 173 |
+
st.markdown(f"**You:** {chat['query']}")
|
| 174 |
+
st.markdown(f"**Assistant:** {chat['response']}")
|
| 175 |
st.markdown("---")
|
| 176 |
|
| 177 |
+
|
| 178 |
if __name__ == "__main__":
|
| 179 |
+
main()
|