File size: 7,040 Bytes
8e9f909
 
 
 
 
bb89e39
13b4a25
8e9f909
 
1447c64
8e9f909
 
 
5e08d43
8e9f909
fff83e9
8e9f909
 
 
 
 
1447c64
b21f324
1447c64
 
 
 
 
 
 
 
 
 
 
b21f324
 
 
1447c64
 
b21f324
 
1447c64
b21f324
1447c64
 
 
d3e8323
8e9f909
d3e8323
8e9f909
b21f324
1447c64
 
b21f324
 
 
 
 
 
 
 
 
 
d3e8323
 
 
 
 
1a35a99
b21f324
1447c64
 
 
 
8e9f909
 
 
b21f324
8e9f909
 
deb1607
021aa72
d3e8323
1a35a99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3e8323
 
1447c64
 
d3e8323
1447c64
1a35a99
 
 
d3e8323
 
1a35a99
13b4a25
 
 
 
b21f324
 
694fa5c
b21f324
1a35a99
13b4a25
 
 
1a35a99
13b4a25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fff83e9
13b4a25
 
 
 
 
 
 
 
2ffb05f
1a35a99
d3e8323
1a35a99
001d826
 
1a35a99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import streamlit as st
from utils.vector_store import VectorStore
from utils.document_processor import DocumentProcessor
from utils.case_manager import CaseManager
from utils.legal_notebook_interface import LegalNotebookInterface
from utils.case_manager_interface import CaseManagerInterface
from utils.admin_interface import AdminInterface
from datetime import datetime
import os
from pathlib import Path
import nltk
import spacy

# Ensure this is the first Streamlit command
st.set_page_config(
    page_title="Synaptyx.AI - Legal Agent : Empowering Legal Intelligence",
    page_icon="βš–οΈ",
    layout="wide",
    initial_sidebar_state="expanded"
)

def init_huggingface_directories():
    """Initialize directory structure for Hugging Face Spaces."""
    if os.environ.get('SPACE_ID'):
        base_dir = Path("/data")
    else:
        base_dir = Path(os.getcwd()) / "data"
    
    directories = {
        'data': base_dir,
        'cases': base_dir / "cases",
        'vectors': base_dir / "vectors",
        'nltk_data': base_dir / "nltk_data",
        'temp': base_dir / "temp",
        'logs': base_dir / "logs",
        'prompts': base_dir / "prompts",
        'ontology': base_dir / "ontology"
    }
    
    # Create directories
    for dir_name, dir_path in directories.items():
        dir_path.mkdir(parents=True, exist_ok=True)
        st.sidebar.write(f"βœ“ {dir_name} directory ready")
        
    return directories

@st.cache_resource(show_spinner=False)
def init_components():
    """Initialize all components silently."""
    try:
        # Set up directories
        directories = init_huggingface_directories()

        # Initialize NLTK
        nltk.data.path.append(str(directories['nltk_data']))
        resources = ['punkt', 'averaged_perceptron_tagger', 'maxent_ne_chunker', 'words', 'stopwords']
        for resource in resources:
            try:
                nltk.download(resource, download_dir=str(directories['nltk_data']), quiet=True)
            except Exception:
                pass

        # Initialize spaCy
        try:
            nlp = spacy.load("en_core_web_sm")
        except OSError:
            os.system("python -m spacy download en_core_web_sm > /dev/null 2>&1")
            nlp = spacy.load("en_core_web_sm")

        # Initialize components with proper paths
        case_manager = CaseManager(base_path=str(directories['cases']))
        vector_store = VectorStore(storage_path=str(directories['vectors']))
        doc_processor = DocumentProcessor(base_path=str(directories['data']))
        
        return case_manager, vector_store, doc_processor

    except Exception as e:
        st.error(f"Failed to initialize components: {str(e)}")
        raise

def main():
    try:
        # Custom styles
        st.markdown("""
            <style>
            .main-header {
                font-size: 40px;
                font-weight: bold;
                color: #2B547E;
                margin-bottom: 0px;
            }
            .main-tagline {
                font-size: 18px;
                color: #6A7B8C;
                margin-top: 0px;
                font-style: italic;
            }
            .stButton button {
                width: 100%;
            }
            .status-message {
                padding: 1rem;
                border-radius: 0.5rem;
                margin: 1rem 0;
            }
            .status-success {
                background-color: #d1e7dd;
                color: #0f5132;
            }
            .case-card {
                padding: 1rem;
                border-radius: 0.5rem;
                border: 1px solid #e0e0e0;
                margin-bottom: 1rem;
            }
            </style>
            """, unsafe_allow_html=True)

        # Initialize components
        case_manager, vector_store, doc_processor = init_components()

        # Session state for user authentication
        if 'user_authenticated' not in st.session_state:
            st.session_state.user_authenticated = False
            st.session_state.current_user = None
        
        # Header
        st.markdown("<h1 class='main-header'>Synaptyx.AI - Legal AI agent based solution</h1>", unsafe_allow_html=True)
        st.markdown("<p class='main-tagline'>Empowering Legal Intelligence: Automate, Analyze, Act.</p>", unsafe_allow_html=True)

        # Sidebar navigation with admin access
        if st.session_state.user_authenticated:
            is_admin = st.session_state.current_user == 'admin'
            
            navigation_options = ["πŸ“ Case Manager", "πŸ“„ Document Analysis", "πŸ€– Legal Assistant"]
            if is_admin:
                navigation_options.append("βš™οΈ Admin")
            
            tab = st.sidebar.radio("Navigation", navigation_options)
            
            # Add logout button
            if st.sidebar.button("Logout"):
                st.session_state.user_authenticated = False
                st.session_state.current_user = None
                st.rerun()

            if tab == "πŸ“ Case Manager":
                case_manager_interface = CaseManagerInterface(case_manager, vector_store, doc_processor)
                case_manager_interface.render()

            elif tab == "πŸ“„ Document Analysis":
                notebook = LegalNotebookInterface(case_manager, vector_store, doc_processor)
                notebook.render()

            elif tab == "πŸ€– Legal Assistant":
                notebook = LegalNotebookInterface(case_manager, vector_store, doc_processor)
                notebook.render()

            elif tab == "βš™οΈ Admin" and is_admin:
                admin_interface = AdminInterface(case_manager, vector_store, doc_processor)
                admin_interface.render()

        else:
            # Login form
            st.sidebar.title("Login")
            with st.sidebar.form("login_form"):
                username = st.text_input("Username")
                password = st.text_input("Password", type="password")
                submit = st.form_submit_button("Login")

                if submit:
                    if username == "admin" and password == "demo":
                        st.session_state.user_authenticated = True
                        st.session_state.current_user = "admin"
                        st.rerun()
                    # Here you can add more user authentication logic
                    else:
                        st.error("Invalid credentials")

            # Show welcome message for non-authenticated users
            st.markdown("""
            ## Welcome to Synaptyx.AI Legal Agent
            Please login to access the legal analysis tools.
            
            ### Available Features:
            - πŸ“ Case Management
            - πŸ“„ Document Analysis
            - πŸ€– Legal Assistant
            - βš™οΈ Admin Panel (for administrators)
            """)

    except Exception as e:
        st.error("An unexpected error occurred")
        st.exception(e)

if __name__ == "__main__":
    main()