File size: 3,481 Bytes
0628cbb
226da61
0628cbb
 
 
3cf3e00
8dfb503
 
 
 
 
 
3cf3e00
 
1ab74f5
3cf3e00
1ab74f5
 
8dfb503
3cf3e00
8bfb7cc
 
3cf3e00
 
 
 
 
8dfb503
17d5dd8
9ac9057
538e101
17d5dd8
3cf3e00
 
 
 
 
 
 
 
 
17d5dd8
8bfb7cc
3cf3e00
8bfb7cc
 
3cf3e00
8bfb7cc
3cf3e00
8dfb503
8bfb7cc
5d2245e
226da61
d2770d0
3cf3e00
226da61
9ac9057
 
8bfb7cc
8dfb503
4a84d58
3cf3e00
 
 
 
 
 
 
668b17d
 
3cf3e00
 
 
668b17d
3cf3e00
ca9cc5e
3cf3e00
538e101
8bfb7cc
 
0628cbb
17a737c
 
 
 
 
 
 
3cf3e00
 
 
069e614
 
 
 
 
 
 
 
 
 
17a737c
3cf3e00
17a737c
 
 
e36ffb0
17a737c
 
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
import streamlit as st
import time

st.set_page_config(page_title="Indian Spiritual RAG")

# Initialize session state variables
if 'initialized' not in st.session_state:
    st.session_state.initialized = False
if 'last_query' not in st.session_state:
    st.session_state.last_query = ""
if 'submit_clicked' not in st.session_state:
    st.session_state.submit_clicked = False
if 'query_input' not in st.session_state:
    st.session_state.query_input = ""

# Import other modules
from rag_engine import process_query, load_model
from utils import setup_all_auth

# Custom CSS for styling
st.markdown("""
<style>
.main-title {
    font-size: 2.5rem;
    color: #FF5722;
    text-align: center;
    margin-bottom: 1rem;
}
.stButton>button {
    background-color: #4CAF50 !important;
    color: white !important;
    border-radius: 8px !important;
    font-weight: bold !important;
}
div[data-baseweb="input"] {
    border: 3px solid #4CAF50 !important;
    border-radius: 8px !important;
    transition: border-color 0.3s ease-in-out;
}
div[data-baseweb="input"]:focus-within {
    border: 3px solid #FF5722 !important;
}
</style>
<div class="main-title">Indian Spiritual Texts Q&A</div>
""", unsafe_allow_html=True)

# Initialization logic
if not st.session_state.initialized:
    init_message = st.empty()
    init_message.info("Hang in there! We are setting the system up for you. 😊")
    try:
        setup_all_auth()
        load_model()
        st.session_state.initialized = True
        time.sleep(0.5)
        init_message.success("System initialized successfully!")
        time.sleep(1)
        init_message.empty()
    except Exception as e:
        init_message.error(f"Error initializing: {str(e)}")

# Function to handle form submission
def handle_form_submit():
    st.session_state.last_query = st.session_state.query_input
    st.session_state.submit_clicked = True
    st.session_state.query_input = ""  # ✅ Clears input field on submission

# Form for user input
with st.form(key="query_form"):
    query = st.text_input(
        "Ask your question:", 
        key="query_input", 
        value=st.session_state.query_input
    )
    submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)

# Display last question
if st.session_state.last_query:
    st.markdown("### Current Question:")
    st.info(st.session_state.last_query)

# Sliders for customization
col1, col2 = st.columns(2)
with col1:
    top_k = st.slider("Number of sources:", 3, 10, 5)
with col2:
    word_limit = st.slider("Word limit:", 50, 500, 200)

# Process query when submitted
if st.session_state.submit_clicked and st.session_state.last_query:
    st.session_state.submit_clicked = False
    with st.spinner("Processing your question..."):
        try:
            result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
            st.subheader("Answer:")
            st.write(result["answer_with_rag"])
            st.subheader("Sources:")
            for citation in result["citations"].split("\n"):
                st.write(citation)
        except Exception as e:
            st.error(f"Error processing query: {str(e)}")

# Footer
st.markdown("---")
st.markdown("""
### About this app
This application uses a Retrieval-Augmented Generation (RAG) system to answer questions about Indian spiritual texts.
It searches through a database of texts to find relevant passages and generates answers based on those passages.
""")