File size: 5,873 Bytes
ce3af46
 
fdc80c8
ce3af46
 
 
1a1332a
ce3af46
 
 
 
e6167f8
 
 
 
 
4433c64
e6167f8
 
 
 
 
 
 
ce3af46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca5264b
f67181d
f78495c
 
 
ce3af46
 
f78495c
ce3af46
 
 
f78495c
ce3af46
f78495c
ce3af46
 
 
 
f78495c
 
 
 
 
ce3af46
f78495c
ce3af46
f78495c
 
ce3af46
f78495c
ce3af46
 
 
 
 
 
 
 
1a1332a
 
ced5431
8848e89
ce3af46
 
8848e89
599d161
 
 
 
 
 
 
 
1a1332a
e6167f8
8848e89
1a1332a
 
e6167f8
1a1332a
8848e89
1a1332a
8848e89
1a1332a
8848e89
 
 
ce3af46
1a1332a
 
599d161
1a1332a
 
 
8848e89
e6167f8
ca5aacd
8848e89
1a1332a
8848e89
9665824
 
8848e89
9665824
ad53611
ce3af46
 
 
 
 
 
 
 
a523549
1893da3
fdc80c8
 
ce3af46
a523549
ce3af46
599d161
ce3af46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6c820d
 
ce3af46
 
e6167f8
fdc80c8
ce3af46
 
ced5431
ce3af46
 
e6167f8
 
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
import streamlit as st
from generator import generate_response_from_document
from retrieval import retrieve_documents_hybrid,find_query_dataset
from evaluation import calculate_metrics
from data_processing import load_recent_questions, save_recent_question
import time
import matplotlib.pyplot as plt

# Page Title
st.title("RAG7 - Real World RAG System")

st.markdown(
    """

    <style>

    .stTextArea textarea {

        background-color: white !important;

        font-size: 20px !important;

        color: black !important;

    }

    </style>

    """,
    unsafe_allow_html=True
)

# global retrieved_documents
# retrieved_documents = []

# global response 
# response = ""

# global time_taken_for_response
# time_taken_for_response = 'N/A'

# @st.cache_data
# def load_data():
#     load_data_from_faiss()

# data_status = load_data()

# Question Section
st.subheader("Hi, What do you want to know today?")
question = st.text_area("Enter your question:", placeholder="Type your question here...", height=100)
question = question.strip()

# # Submit Button
# if st.button("Submit"):
#     start_time = time.time()
#     retrieved_documents = retrieve_documents_hybrid(question, 10)  
#     response = generate_response_from_document(question, retrieved_documents)
#     end_time = time.time()
#     time_taken_for_response = end_time-start_time
# else:
#     response = ""

# # Response Section
# st.subheader("Response")
# st.text_area("Generated Response:", value=response, height=150, disabled=True)

# # Metrics Section
# st.subheader("Metrics")

# col1, col2 = st.columns([1, 3])  # Creating two columns for button and metrics display

# with col1:
#     if st.button("Calculate Metrics"):
#         metrics = calculate_metrics(question, response, retrieved_documents, time_taken_for_response)
#     else:
#         metrics = ""

# with col2:
#     st.text_area("Metrics:", value=metrics, height=100, disabled=True)

if "retrieved_documents" not in st.session_state:
    st.session_state.retrieved_documents = []
if "response" not in st.session_state:
    st.session_state.response = ""
if "time_taken_for_response" not in st.session_state:
    st.session_state.time_taken_for_response = "N/A"
if "metrics" not in st.session_state:
    st.session_state.metrics = {}
if "query_dataset" not in st.session_state:
    st.session_state.query_dataset = ''

recent_questions = load_recent_questions()

# for visualization

# response_time = [q["response_time"] for q in recent_data["questions"]]
# labels = [f"Q{i+1}" for i in range(len(response_time))]  # Labels for X-axis

# fig, ax = plt.subplots()
# ax.set_xlabel("Recent Questions")
# ax.set_ylabel("Time Taken for Response")
# ax.legend()
# st.sidebar.pyplot(fig)
if recent_questions and "questions" in recent_questions and recent_questions["questions"]:
    recent_qns = list(reversed(recent_questions["questions"]))
    st.sidebar.title("Analytics")

    # Extract response times and labels
    response_time = [q["response_time"] for q in recent_qns]
    labels = [f"Q{i+1}" for i in range(len(response_time))]

    # Plot graph
    fig, ax = plt.subplots()
    ax.plot(labels, response_time, marker="o", linestyle="-", color="skyblue")
    ax.set_xlabel("Recent Questions")
    ax.set_ylabel("Time Taken for Response (seconds)")
    ax.set_title("Response Time Analysis")

    # Display the plot in the sidebar
    st.sidebar.pyplot(fig)

    st.sidebar.markdown("---")

    # Display Recent Questions
    st.sidebar.title("Recent Questions")
    for q in recent_qns:  # Show latest first
        st.sidebar.write(f"🔹 {q['question']}")
else:
    st.sidebar.write("No recent questions")
 # Separator

# Streamlit Sidebar for Recent Questions


# Submit Button
# if st.button("Submit"):
#     start_time = time.time()
#     st.session_state.retrieved_documents = retrieve_documents_hybrid(question, 10)  
#     st.session_state.response = generate_response_from_document(question, st.session_state.retrieved_documents)
#     end_time = time.time()
#     st.session_state.time_taken_for_response = end_time - start_time

if st.button("Submit"):
    start_time = time.time()
    st.session_state.metrics = {}
    st.session_state.query_dataset =  find_query_dataset(question)
    st.session_state.retrieved_documents = retrieve_documents_hybrid(question, st.session_state.query_dataset, 10)  
    st.session_state.response = generate_response_from_document(question, st.session_state.retrieved_documents)
    end_time = time.time()
    st.session_state.time_taken_for_response = end_time - start_time
    save_recent_question(question, st.session_state.time_taken_for_response)

# Display stored response
st.subheader("Response")
st.text_area("Generated Response:", value=st.session_state.response, height=150, disabled=True)

col1, col2 = st.columns([1, 3])  # Creating two columns for button and metrics display

# # Calculate Metrics Button
# with col1:
#     if st.button("Calculate Metrics"):
#         metrics = calculate_metrics(question, st.session_state.response, st.session_state.retrieved_documents, st.session_state.time_taken_for_response)
#     else:
#         metrics = {}

# with col2:
#     #st.text_area("Metrics:", value=metrics, height=100, disabled=True)
#     st.json(metrics)


# Calculate Metrics Button
with col1:
    if st.button("Show Metrics"):     
        st.session_state.metrics = calculate_metrics(question, st.session_state.query_dataset, st.session_state.response, st.session_state.retrieved_documents, st.session_state.time_taken_for_response)
    else:
        metrics_ = {}

with col2:
    #st.text_area("Metrics:", value=metrics, height=100, disabled=True)
    st.json(st.session_state.metrics)