Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,10 @@
|
|
| 1 |
# app.py
|
| 2 |
import streamlit as st
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
from components.chat_interface import ChatInterface
|
| 6 |
from components.template_generator import render_template_generator
|
| 7 |
-
from utils.case_manager import CaseManager
|
| 8 |
from utils.document_processor import DocumentProcessor
|
| 9 |
-
|
| 10 |
-
import os
|
| 11 |
|
| 12 |
# Page config
|
| 13 |
st.set_page_config(
|
|
@@ -17,86 +14,174 @@ st.set_page_config(
|
|
| 17 |
initial_sidebar_state="expanded"
|
| 18 |
)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def main():
|
|
|
|
|
|
|
|
|
|
| 25 |
# Initialize components
|
| 26 |
-
case_manager = CaseManager()
|
| 27 |
doc_processor = DocumentProcessor()
|
| 28 |
|
| 29 |
-
# Render sidebar and get selected
|
| 30 |
-
|
| 31 |
|
| 32 |
-
if
|
| 33 |
-
|
| 34 |
-
vector_store = VectorStore(selected_case)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
type=['pdf', 'docx', 'txt']
|
| 52 |
-
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
[chunk["text"] for chunk in chunks],
|
| 63 |
-
[chunk["metadata"] for chunk in chunks]
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
# Add to case
|
| 67 |
-
doc_data = {
|
| 68 |
-
"title": file.name,
|
| 69 |
-
"content": text,
|
| 70 |
-
"chunks": chunks
|
| 71 |
-
}
|
| 72 |
-
case_manager.add_document(selected_case, doc_data)
|
| 73 |
-
|
| 74 |
-
st.success(f"✅ {file.name} processed successfully!")
|
| 75 |
-
|
| 76 |
-
# Document viewer
|
| 77 |
-
doc_viewer = DocumentViewer(case_manager)
|
| 78 |
-
doc_viewer.render(selected_case)
|
| 79 |
-
|
| 80 |
-
with tab2:
|
| 81 |
-
# Chat interface
|
| 82 |
-
chat = ChatInterface(vector_store)
|
| 83 |
-
chat.render()
|
| 84 |
-
|
| 85 |
-
with tab3:
|
| 86 |
-
# Document generation
|
| 87 |
-
render_template_generator()
|
| 88 |
-
|
| 89 |
-
else:
|
| 90 |
-
# No case selected
|
| 91 |
-
st.markdown("## Welcome to Legal AI Assistant")
|
| 92 |
-
st.markdown("""
|
| 93 |
-
This tool helps you:
|
| 94 |
-
- Manage legal cases and documents
|
| 95 |
-
- Chat with your documents
|
| 96 |
-
- Generate legal documents from templates
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
main()
|
|
|
|
| 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 |
|
| 9 |
# Page config
|
| 10 |
st.set_page_config(
|
|
|
|
| 14 |
initial_sidebar_state="expanded"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Initialize Anthropic client
|
| 18 |
+
try:
|
| 19 |
+
# First try to get from HF secrets
|
| 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 Space's secrets")
|
| 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: Please ensure Anthropic_API_KEY is set in HF secrets")
|
| 28 |
+
st.stop()
|
| 29 |
+
|
| 30 |
+
# Session state initialization
|
| 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 |
+
def initialize_case_folders():
|
| 37 |
+
"""Initialize folder structure for cases"""
|
| 38 |
+
base_path = Path("data/cases")
|
| 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",
|
| 47 |
+
max_tokens=2000,
|
| 48 |
+
temperature=0.7,
|
| 49 |
+
messages=[{
|
| 50 |
+
"role": "user",
|
| 51 |
+
"content": f"""Analyze this legal document and provide:
|
| 52 |
+
1. Key points and findings
|
| 53 |
+
2. Legal implications
|
| 54 |
+
3. Important dates and deadlines
|
| 55 |
+
4. Potential risks
|
| 56 |
+
5. Recommended actions
|
| 57 |
+
|
| 58 |
+
Document text: {text}"""
|
| 59 |
+
}]
|
| 60 |
+
)
|
| 61 |
+
return message.content[0].text
|
| 62 |
+
except Exception as e:
|
| 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",
|
| 71 |
+
max_tokens=2000,
|
| 72 |
+
temperature=0.7,
|
| 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 |
+
)
|
| 82 |
+
return message.content[0].text
|
| 83 |
+
except Exception as e:
|
| 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 |
+
["Document Analysis", "Document Generation", "Chat with Documents"]
|
| 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 == "Document Analysis":
|
| 111 |
+
st.header("Document Analysis")
|
|
|
|
| 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 Document Analysis section first.")
|
| 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:** " + chat["query"])
|
| 183 |
+
st.markdown("**Assistant:** " + chat["response"])
|
| 184 |
+
st.markdown("---")
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
main()
|