File size: 9,243 Bytes
6c01b98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7fdbad
6c01b98
 
 
 
 
 
 
 
b7fdbad
6c01b98
b7fdbad
6c01b98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import streamlit as st
import requests
import time

# --- 1. PAGE CONFIG ---
st.set_page_config(
    page_title="RAG Insight Assistant",
    page_icon="🧠",
    layout="wide",
    initial_sidebar_state="expanded"
)

# --- 2. THE OFFICIAL UNIFIED UI (CSS) ---
st.markdown("""

    <style>

    /* Global Reset & Pure Black Background */

    .stApp, .stMain, .stBottom, [data-testid="stMainBlockContainer"], [data-testid="stBottomBlockContainer"] {

        background-color: #000000 !important;

        color: #FFFFFF !important;

    }

    

    /* Remove all default borders and shadows */

    .st-emotion-cache-4rsbii, .st-emotion-cache-liupih, .st-emotion-cache-1p2n2i4, .st-emotion-cache-128upt6 {

        background-color: #000000 !important;

        border: none !important;

        box-shadow: none !important;

    }



    [data-testid="stHeader"] {

        background: rgba(0,0,0,0) !important;

    }



    .block-container {

        max-width: 1000px !important;

        padding-top: 2rem !important;

        margin: auto;

    }



    /* THE UNIFIED AI CARD - STITCHING LOGIC */

    .hero-unit {

        background-color: #050505;

        border: 2px solid #58A6FF; /* Unified Glow Border */

        border-bottom: none; /* Open bottom to merge */

        border-radius: 24px 24px 0 0;

        padding: 5rem 2rem 1.5rem 2rem;

        text-align: center;

        margin: 2rem auto 0 auto;

        max-width: 850px !important;

        box-shadow: 0 -10px 40px rgba(59, 130, 246, 0.1);

    }



    /* Target the Chat Input to physically merge with the div above */

    [data-testid="stChatInput"] {

        position: relative !important;

        background-color: #050505 !important;

        border: 2px solid #58A6FF !important;

        border-top: none !important; /* Open top to merge */

        border-radius: 0 0 24px 24px !important;

        padding: 0 3rem 4rem 3rem !important;

        margin: 0 auto !important; /* Center match */

        max-width: 850px !important; /* Width match */

        bottom: auto !important;

        z-index: 10;

        box-shadow: 0 20px 40px rgba(0, 0, 0, 0.9);

    }



    /* Target all internal wrappers from your HTML snippet */

    [data-testid="stChatInput"] > div,

    [data-testid="stChatInput"] div[class*="st-emotion-cache"] {

        background-color: transparent !important;

        border: none !important;

    }



    /* Remove the 'invisible gap' between Streamlit elements */

    [data-testid="stVerticalBlock"] > div:has(div.hero-unit) + div {

        margin-top: -1px !important;

    }



    /* THE TEXTAREA - ABSOLUTE BLACK */

    [data-testid="stChatInputTextArea"] {

        background-color: #000000 !important;

        color: #FFFFFF !important;

        -webkit-text-fill-color: #FFFFFF !important;

        caret-color: #FFFFFF !important;

        border: 1px solid #374151 !important;

        border-radius: 12px !important;

    }



    /* THE SUBMIT BUTTON */

    [data-testid="stChatInputSubmitButton"] {

        background-color: #58A6FF !important;

        border-radius: 50% !important;

    }

    

    [data-testid="stChatInputSubmitButton"] svg {

        color: #000000 !important;

    }



    /* TYPOGRAPHY */

    .main-header {

        font-size: 3.5rem;

        font-weight: 900;

        color: #58A6FF;

        margin-bottom: 0.5rem;

    }

    

    .sub-header {

        font-size: 1.2rem;

        color: #8B949E;

        margin-bottom: 0;

    }



    /* CHAT MESSAGES */

    [data-testid="stChatMessage"] {

        background-color: #161B22 !important;

        border: 1px solid #30363D !important;

        margin-top: 1rem;

    }

    

    [data-testid="stChatMessage"] * {

        color: #FFFFFF !important;

    }



    /* SIDEBAR - PURE BLACK */

    [data-testid="stSidebar"] {

        background-color: #000000 !important;

        border-right: 1px solid #30363D;

    }

    

    [data-testid="stSidebar"] * {

        color: #FFFFFF !important;

    }

    

    [data-testid="stSidebarCollapse"] svg {

        color: #FFFFFF !important;

        fill: #FFFFFF !important;

    }



    /* BUTTONS */

    .stButton>button {

        background-color: #21262D;

        color: #FFFFFF;

        border: 1px solid #30363D;

        border-radius: 8px;

        font-weight: 600;

        height: 3em;

    }

    

    .stButton>button:hover {

        border-color: #58A6FF;

    }



    .stChatInputContainer {

        position: static !important;

        background-color: transparent !important;

        padding: 0 !important;

    }



    /* MOBILE RESPONSIVE */

    @media (max-width: 768px) {

        .main-header { font-size: 2rem; }

        .sub-header { font-size: 1rem; }

        .hero-unit, [data-testid="stChatInput"] { max-width: 95% !important; }

    }

    </style>

    """, unsafe_allow_html=True)

# --- 3. MAIN UI STRUCTURE ---
st.markdown("""

    <div class="hero-unit">

        <h1 class="main-header">🧠 RAG Insight</h1>

        <p class="sub-header">Professional Enterprise AI Document Intelligence</p>

    </div>

    """, unsafe_allow_html=True)

# Sidebar for Controls and Knowledge Base
with st.sidebar:
    st.image("https://img.icons8.com/fluency/96/artificial-intelligence.png", width=80)
    st.title("Knowledge Hub")
    
    st.markdown("""

    <div class="info-card">

        <strong>Engine:</strong> Cohere Command-R<br>

        <strong>Vector DB:</strong> Qdrant<br>

        <strong>RAG Protocol:</strong> Enabled

    </div>

    """, unsafe_allow_html=True)

    # Document Topic Section
    st.subheader("πŸ“„ Current Context")
    st.markdown("""

    <div style="background-color: #21262D; padding: 10px; border-radius: 8px; border-left: 4px solid #58A6FF; margin-bottom: 15px;">

        <span style="font-size: 0.9rem; color: #E0E0E0;">

            <strong>Topic:</strong> employee handbook / Document Analysis<br>

            <strong>Source:</strong> sample.pdf

        </span>

    </div>

    """, unsafe_allow_html=True)

    st.subheader("πŸ“ Document Management")
    
    # Pre-requisite Note
    st.warning("⚠️ **Note:** Please 'Upload Docs' before asking questions to process the document embeddings.")

    if st.button("Upload document"):
        with st.spinner("Processing document embeddings..."):
            try:
                # Adding a small delay for UI feel
                time.sleep(1) 
                response = requests.post("http://localhost:8000/ingest")
                if response.status_code == 200:
                    st.success("βœ… System ready! Document indexed.")
                else:
                    st.error(f"❌ Initialization failed: {response.text}")
            except Exception as e:
                st.error(f"⚠️ Connection error: {e}")

    st.divider()
    
    with st.expander("πŸ” How it works"):
        st.write("""

        1. **Context Ingestion:** Documents are parsed and converted into high-dimensional vectors.

        2. **Neural Search:** Your questions are compared against the document database to find relevant matches.

        3. **Guided Synthesis:** The AI generates an answer restricted solely to the retrieved context for maximum accuracy.

        """)

# Chat Interface Logic
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat history with custom styling
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# User Interaction
if prompt := st.chat_input("Ask a question about your documents..."):
    # Store user message
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    # Generate Response
    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        with st.spinner("Analyzing knowledge base..."):
            try:
                response = requests.post(
                    "http://localhost:8000/query",
                    json={"question": prompt}
                )
                if response.status_code == 200:
                    answer = response.json().get("answer", "I couldn't find a relevant answer in the provided documents.")
                    
                    # Simulate typing effect for a better feel
                    full_response = ""
                    for chunk in answer.split():
                        full_response += chunk + " "
                        time.sleep(0.05)
                        message_placeholder.markdown(full_response + "β–Œ")
                    
                    message_placeholder.markdown(full_response)
                    st.session_state.messages.append({"role": "assistant", "content": full_response})
                else:
                    st.error("The backend service is currently unreachable.")
            except Exception as e:
                st.error(f"An unexpected error occurred: {e}")

# Footer
st.markdown("---")
st.caption("Powered by Cohere and Qdrant β€’ Developed for RAG Insights")