File size: 14,397 Bytes
85e15b0
 
 
 
 
 
2a65ba9
85e15b0
 
 
 
2a65ba9
 
386ca41
2a65ba9
 
13dc359
593722f
754c466
2a65ba9
6f7f729
 
2a65ba9
6f7f729
 
 
 
 
b29de20
6f7f729
2a65ba9
85e15b0
2a65ba9
13dc359
 
 
 
 
 
b29de20
2a65ba9
13dc359
 
 
 
 
2a65ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85e15b0
 
2a65ba9
 
 
 
 
 
85e15b0
 
 
 
 
 
4815e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85e15b0
 
 
 
2a65ba9
85e15b0
2a65ba9
abd97d7
2a65ba9
13dc359
85e15b0
 
10f45b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a65ba9
 
 
 
 
 
 
 
13dc359
 
2a65ba9
b29de20
 
2a65ba9
 
b29de20
2a65ba9
13dc359
 
 
 
 
2a65ba9
b29de20
 
 
2a65ba9
13dc359
 
 
 
2a65ba9
13dc359
 
 
 
 
 
2a65ba9
b29de20
 
 
 
 
2a65ba9
13dc359
 
 
 
b29de20
 
2a65ba9
b29de20
13dc359
 
2a65ba9
b29de20
 
13dc359
10f45b6
 
 
 
 
75a2b67
10f45b6
 
 
 
 
75a2b67
10f45b6
 
2a65ba9
10f45b6
 
4815e8f
2a65ba9
4815e8f
 
2a65ba9
4815e8f
 
 
 
85e15b0
2a65ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85e15b0
2a65ba9
 
 
 
 
 
 
85e15b0
2a65ba9
85e15b0
 
 
 
 
 
 
2a65ba9
85e15b0
 
 
 
 
 
 
 
 
 
4815e8f
 
 
 
 
 
 
85e15b0
 
 
 
5008838
 
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import os
import streamlit as st
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.chains import RetrievalQA
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import PromptTemplate
from langchain_community.llms import HuggingFaceEndpoint
import time
import translators as ts
from huggingface_hub import hf_hub_download

# Set page layout to wide
st.set_page_config(layout="wide")

# ================== CONFIGURATION ================== #
HF_TOKEN = os.getenv("HF_TOKEN")  # From Spaces secrets
VECTORSTORE_REPO_ID = "vashu2425/bhagavad-geeta-faiss-vectordb"
MODEL_REPO_ID = "mistralai/Mistral-7B-Instruct-v0.3"

    
CUSTOM_PROMPT_TEMPLATE = """
Use The Pieces Of Information Provided In The Context To Answer User's Question.
If You Don't Know The Answer, Just Say "I Don't Have Information",except this do not say anything. 
Don't Try To Make Up An Answer. Don't Provide Anything Out Of The Given Context.

Context: {context}
Question: {question}

Start The Answer Directly., Please. The Answer Should Contain All 3 Contexts.
Consider Yourself As God Krishna And Answer The Question Result Should Not Start With "Answer"
"""  # Keep your template here

# ---------- Session Management Functions ---------- #
def initialize_session_states():
    session_defaults = {
        "messages": [],
        "selected_question": None,
        "show_predefined": True,
        "last_response": None,
        "translation_done": False,
        "last_prompt": None  # Add this line
    }
    for key, val in session_defaults.items():
        if key not in st.session_state:
            st.session_state[key] = val

def render_chat_messages():
    for message in st.session_state.messages:
        with st.chat_message(message["role"], avatar="🐿" if message["role"] == "user" else "🪈"):
            content = message["content"]
            if "hindi-text" in content:
                st.markdown(content, unsafe_allow_html=True)
            else:
                st.markdown(content)

def render_predefined_questions():
    predefined_questions = [
        "Meaning of Dharma?",
        "What is the purpose of life?",
        "How to find inner peace?",
        "How can I be a better person?",
        "What is the meaning of life?",
        "How can I be a better friend?"
    ]
    st.markdown("### Or, try one of these:")
    buttons = st.columns(len(predefined_questions))
    for idx, question in enumerate(predefined_questions):
        if buttons[idx].button(question, key=f"predefined_{idx}"):
            st.session_state.selected_question = question
            st.session_state.show_predefined = False

# ---------- Core Functionality Functions ---------- #
def translate_text(text, dest_language="hi"):
    try:
        # Use the updated translation method
        return ts.translate_text(
            text, 
            to_language=dest_language,
            translator='google'
        )
    except Exception as e:
        st.error(f"Translation failed: {str(e)}")
        return text

@st.cache_resource
def get_vectorstore():
    try:
        embedding_model = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
        os.makedirs("vectorstore/db_faiss", exist_ok=True)
        
        faiss_files = ["index.faiss", "index.pkl"]
        for filename in faiss_files:
            if not os.path.exists(f"vectorstore/db_faiss/{filename}"):
                hf_hub_download(
                    repo_id=VECTORSTORE_REPO_ID,
                    filename=filename,
                    local_dir="vectorstore/db_faiss",
                    token=HF_TOKEN,
                    repo_type="dataset"
                )
        
        return FAISS.load_local("vectorstore/db_faiss", embedding_model, allow_dangerous_deserialization=True)
    except Exception as e:
        st.error(f"Vectorstore initialization failed: {str(e)}")
        st.stop()

def set_custom_prompt(custom_prompt_template):
    return PromptTemplate(template=custom_prompt_template, input_variables=["context", "question"])

def load_llm(huggingface_repo_id, hf_token):
    return HuggingFaceEndpoint(
        repo_id=huggingface_repo_id,
        temperature=0.4,
        huggingfacehub_api_token=hf_token,
        model_kwargs={"max_length": 512}
    )

def handle_translation():
    if "last_response" in st.session_state and st.session_state.last_response:
        try:
            if not st.session_state.get("translation_done", False):
                translated_text = translate_text(st.session_state.last_response, "hi")

                # Update the last assistant message
                for i in range(len(st.session_state.messages) - 1, -1, -1):
                    if st.session_state.messages[i]["role"] == "assistant":
                        st.session_state.messages[i]["content"] = f'<div class="hindi-text">{translated_text}</div>'
                        break

                # Mark translation as done
                st.session_state.translation_done = True
                st.rerun()  # Forces a UI refresh

        except Exception as e:
            st.error(f"Translation error: {str(e)}")
    

def format_source_docs(source_documents):
    formatted_docs = []
    for idx, doc in enumerate(source_documents, start=1):
        content = doc.page_content.replace('\t', ' ').replace('\n', ' ').strip()
        formatted_doc = f"**Source {idx}** (Page {doc.metadata['page']}):\n\n{content[:500]}..." 
        formatted_docs.append(formatted_doc)
    return "\n\n".join(formatted_docs)

def handle_user_input(prompt, qa_chain):
    if prompt:
        # Check if this prompt has already been processed
        if st.session_state.get("last_prompt") == prompt:
            return
        
        # Store the current prompt to prevent reprocessing
        st.session_state.last_prompt = prompt
        
        with st.chat_message("user", avatar="🐿"):
            st.markdown(prompt)
        st.session_state.messages.append({"role": "user", "content": prompt})

        try:
            # Add temporary assistant message
            with st.chat_message("assistant", avatar="🪈"):
                response_placeholder = st.empty()
            
            # Process query and generate response
            response = qa_chain.invoke({"query": prompt})
            result = response["result"]
            source_documents = response["source_documents"]

            # Build response incrementally
            accumulated_text = ""
            for char in result:
                accumulated_text += char
                response_placeholder.markdown(f'<div class="english-text">{accumulated_text}</div>', unsafe_allow_html=True)
                time.sleep(0.01)
            
            # Update session state with final response
            st.session_state.messages.append({
                "role": "assistant",
                "content": f'<div class="english-text">{accumulated_text}</div>',
                "original": accumulated_text
            })
            
            st.session_state.last_response = accumulated_text
            st.session_state.show_predefined = False
            st.session_state.translation_done = False

            if "don't have information" not in result.lower():
                with st.expander("Source Documents"):
                    st.markdown(format_source_docs(source_documents))

        except Exception as e:
            st.error(f"Error: {str(e)}")
            # Remove temporary assistant message on error
            if st.session_state.messages and st.session_state.messages[-1]["role"] == "assistant":
                st.session_state.messages.pop()

# def handle_translation():
#     if "last_response" in st.session_state and st.session_state.last_response:
#         try:
#             if not st.session_state.get("translation_done", False):
#                 translated_text = translate_text(st.session_state.last_response, "hi")
                
#                 # Update messages
#                 for msg in reversed(st.session_state.messages):
#                     if msg["role"] == "assistant":
#                         msg["content"] = f'<div class="hindi-text">{translated_text}</div>'
#                         break
                
#                 st.session_state.translation_done = True
#                 st.rerun()  # Corrected rerun method
                
#         except Exception as e:
#             st.error(f"Translation error: {str(e)}")

def render_chat_messages():
    for message in st.session_state.messages:
        with st.chat_message(message["role"], avatar="🐿" if message["role"] == "user" else "🪈"):
            content = message.get("original", message["content"])  # Show original if available
            if "hindi-text" in message["content"]:
                st.markdown(message["content"], unsafe_allow_html=True)
            else:
                st.markdown(content)

def main():
    st.markdown( """
        <style>
            @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Devanagari:wght@400;700&display=swap');
            .hindi-text {
                font-family: 'Noto Sans Devanagari', sans-serif;
                font-size: 16px;
                line-height: 1.8;
                direction: ltr;
                text-align: left;
            }
            
            .english-text {
                font-family: Arial, sans-serif;
                font-size: 16px;
                line-height: 1.6;
            }
            
             .translate-btn {
             background-color: #4CAF50 !important;
             color: white !important;
             border-radius: 20px;  /* Reduced from 25px */
             padding: 6px 20px;    /* Reduced from 8px 25px */
             margin: 6px 0;        /* Reduced from 10px 0 */
             border: none;
             transition: all 0.3s ease;
             font-size: 14px;      /* Added font-size control */
             min-width: 120px;     /* Added for better proportions */
             }

             .translate-btn:hover {
                 background-color: #45a049 !important;
                 transform: scale(1.03); /* Reduced from 1.05 */
             }
            
             .top-left-button {
                position: auto;
                top: 50px;
                left: 20px;
                z-index: 100;
                padding: 10px 20px;
                background-color: #e0162e;
                color: white !important;
                text-decoration: none !important;
                border-radius: 50px;
                margin-top: 10px;
                font-size: 16px;
                text-align: center;
            }
            .top-left-button:hover {
                background-color: #f7525a;
            }
        
            /* Fullscreen styles */
            body {
                margin: 0;
                padding: 0;
                width: 100vw;
                height: 100vh;
                display: flex;
                justify-content: center;
                align-items: center;
                background-color: #1e1e30; /* Change the background color to #1e1e30 */
            }
        
            [data-testid="stAppViewContainer"] > .main {
                background-size: cover;
                background-position: center center;
                background-repeat: no-repeat;
                background-attachment: local;
            }
        
            /* Header background */
            [data-testid="stHeader"] {
                background: #1e1e30;
            }
        
            /* Apply background color to the whole Streamlit app */
            .stApp {
                width: 100%;
                max-width: 100vw;
                display: flex;
                justify-content: center;
                align-items: flex-start;
                padding: 20px;
                background-color: #1e1e30; /* This will apply the background color to the entire app */
            }

             .custom-paragraph {
                font-size: 20px !important;
                line-height: 0.2;
                color: #666666;
            } 
        
            /* Apply background color to stBottomBlockContainer */
            [data-testid="stBottomBlockContainer"] {
                background-color: #1e1e30; /* Set the same color for bottom block */
            }
        
            /* Hover effect for textarea (optional) */
            .stTextArea>div>textarea:hover {
                box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.3); /* Change shadow on hover */
        </style>
        <a href="https://iskconmangaluru.com/wp-content/uploads/2021/04/English-Bhagavad-gita-His-Divine-Grace-AC-Bhaktivedanta-Swami-Prabhupada.pdf" target="_blank" class="top-left-button">
            Source Bhagavad Gita PDF
        </a>
        """,
        unsafe_allow_html=True
    )
    
    st.title("Ask Krishna! 🦚")
    st.markdown('<p class="hindi-text" style="color:#666666; font-size:20px;">शांति स्वीकृति से शुरू होती है</p>', 
                unsafe_allow_html=True)

    initialize_session_states()
    render_chat_messages()

    if st.session_state.show_predefined:
        render_predefined_questions()

    prompt = st.chat_input("What's your curiosity?") or st.session_state.selected_question
    st.session_state.selected_question = None

    try:
        vectorstore = get_vectorstore()
        qa_chain = RetrievalQA.from_chain_type(
            llm=load_llm(MODEL_REPO_ID, HF_TOKEN),
            chain_type="stuff",
            retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
            return_source_documents=True,
            chain_type_kwargs={"prompt": set_custom_prompt(CUSTOM_PROMPT_TEMPLATE)}
        )

        if prompt:
            handle_user_input(prompt, qa_chain)

        if st.session_state.get("last_response"):
            col1, col2 = st.columns([1, 3])
            with col1:
                if st.button("🌐 Translate to Hindi", key="translate_btn"):
                    handle_translation()
            with col2:
                if st.session_state.get("translation_done"):
                    st.success("Translation to Hindi completed!")

    except Exception as e:
        st.error(f"Initialization error: {str(e)}")

if __name__ == "__main__":
    main()