vamseelatha2002 commited on
Commit
d2b1c5d
·
verified ·
1 Parent(s): 4433c64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +185 -173
app.py CHANGED
@@ -1,173 +1,185 @@
1
- import streamlit as st
2
- from generator import generate_response_from_document
3
- from retrieval import retrieve_documents_hybrid,find_query_dataset
4
- from evaluation import calculate_metrics
5
- from data_processing import load_recent_questions, save_recent_question
6
- import time
7
- import matplotlib.pyplot as plt
8
-
9
- # Page Title
10
- st.title("RAG7 - Real World RAG System")
11
-
12
- st.markdown(
13
- """
14
- <style>
15
- .stTextArea textarea {
16
- background-color: white !important;
17
- font-size: 20px !important;
18
- color: black !important;
19
- }
20
- </style>
21
- """,
22
- unsafe_allow_html=True
23
- )
24
-
25
- # global retrieved_documents
26
- # retrieved_documents = []
27
-
28
- # global response
29
- # response = ""
30
-
31
- # global time_taken_for_response
32
- # time_taken_for_response = 'N/A'
33
-
34
- # @st.cache_data
35
- # def load_data():
36
- # load_data_from_faiss()
37
-
38
- # data_status = load_data()
39
-
40
- # Question Section
41
- st.subheader("Hi, What do you want to know today?")
42
- question = st.text_area("Enter your question:", placeholder="Type your question here...", height=100)
43
- question = question.strip()
44
-
45
- # # Submit Button
46
- # if st.button("Submit"):
47
- # start_time = time.time()
48
- # retrieved_documents = retrieve_documents_hybrid(question, 10)
49
- # response = generate_response_from_document(question, retrieved_documents)
50
- # end_time = time.time()
51
- # time_taken_for_response = end_time-start_time
52
- # else:
53
- # response = ""
54
-
55
- # # Response Section
56
- # st.subheader("Response")
57
- # st.text_area("Generated Response:", value=response, height=150, disabled=True)
58
-
59
- # # Metrics Section
60
- # st.subheader("Metrics")
61
-
62
- # col1, col2 = st.columns([1, 3]) # Creating two columns for button and metrics display
63
-
64
- # with col1:
65
- # if st.button("Calculate Metrics"):
66
- # metrics = calculate_metrics(question, response, retrieved_documents, time_taken_for_response)
67
- # else:
68
- # metrics = ""
69
-
70
- # with col2:
71
- # st.text_area("Metrics:", value=metrics, height=100, disabled=True)
72
-
73
- if "retrieved_documents" not in st.session_state:
74
- st.session_state.retrieved_documents = []
75
- if "response" not in st.session_state:
76
- st.session_state.response = ""
77
- if "time_taken_for_response" not in st.session_state:
78
- st.session_state.time_taken_for_response = "N/A"
79
- if "metrics" not in st.session_state:
80
- st.session_state.metrics = {}
81
- if "query_dataset" not in st.session_state:
82
- st.session_state.query_dataset = ''
83
-
84
- recent_questions = load_recent_questions()
85
-
86
- # for visualization
87
-
88
- # response_time = [q["response_time"] for q in recent_data["questions"]]
89
- # labels = [f"Q{i+1}" for i in range(len(response_time))] # Labels for X-axis
90
-
91
- # fig, ax = plt.subplots()
92
- # ax.set_xlabel("Recent Questions")
93
- # ax.set_ylabel("Time Taken for Response")
94
- # ax.legend()
95
- # st.sidebar.pyplot(fig)
96
- if recent_questions and "questions" in recent_questions and recent_questions["questions"]:
97
- recent_qns = list(reversed(recent_questions["questions"]))
98
- st.sidebar.title("Analytics")
99
-
100
- # Extract response times and labels
101
- response_time = [q["response_time"] for q in recent_qns]
102
- labels = [f"Q{i+1}" for i in range(len(response_time))]
103
-
104
- # Plot graph
105
- fig, ax = plt.subplots()
106
- ax.plot(labels, response_time, marker="o", linestyle="-", color="skyblue")
107
- ax.set_xlabel("Recent Questions")
108
- ax.set_ylabel("Time Taken for Response (seconds)")
109
- ax.set_title("Response Time Analysis")
110
-
111
- # Display the plot in the sidebar
112
- st.sidebar.pyplot(fig)
113
-
114
- st.sidebar.markdown("---")
115
-
116
- # Display Recent Questions
117
- st.sidebar.title("Recent Questions")
118
- for q in recent_qns: # Show latest first
119
- st.sidebar.write(f"🔹 {q['question']}")
120
- else:
121
- st.sidebar.write("No recent questions")
122
- # Separator
123
-
124
- # Streamlit Sidebar for Recent Questions
125
-
126
-
127
- # Submit Button
128
- # if st.button("Submit"):
129
- # start_time = time.time()
130
- # st.session_state.retrieved_documents = retrieve_documents_hybrid(question, 10)
131
- # st.session_state.response = generate_response_from_document(question, st.session_state.retrieved_documents)
132
- # end_time = time.time()
133
- # st.session_state.time_taken_for_response = end_time - start_time
134
-
135
- if st.button("Submit"):
136
- start_time = time.time()
137
- st.session_state.metrics = {}
138
- st.session_state.query_dataset = find_query_dataset(question)
139
- st.session_state.retrieved_documents = retrieve_documents_hybrid(question, st.session_state.query_dataset, 10)
140
- st.session_state.response = generate_response_from_document(question, st.session_state.retrieved_documents)
141
- end_time = time.time()
142
- st.session_state.time_taken_for_response = end_time - start_time
143
- save_recent_question(question, st.session_state.time_taken_for_response)
144
-
145
- # Display stored response
146
- st.subheader("Response")
147
- st.text_area("Generated Response:", value=st.session_state.response, height=150, disabled=True)
148
-
149
- col1, col2 = st.columns([1, 3]) # Creating two columns for button and metrics display
150
-
151
- # # Calculate Metrics Button
152
- # with col1:
153
- # if st.button("Calculate Metrics"):
154
- # metrics = calculate_metrics(question, st.session_state.response, st.session_state.retrieved_documents, st.session_state.time_taken_for_response)
155
- # else:
156
- # metrics = {}
157
-
158
- # with col2:
159
- # #st.text_area("Metrics:", value=metrics, height=100, disabled=True)
160
- # st.json(metrics)
161
-
162
-
163
- # Calculate Metrics Button
164
- with col1:
165
- if st.button("Show Metrics"):
166
- 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)
167
- else:
168
- metrics_ = {}
169
-
170
- with col2:
171
- #st.text_area("Metrics:", value=metrics, height=100, disabled=True)
172
- st.json(st.session_state.metrics)
173
-
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from generator import generate_response_from_document
3
+ from retrieval import retrieve_documents_hybrid,find_query_dataset
4
+ from evaluation import calculate_metrics
5
+ from data_processing import load_recent_questions, save_recent_question
6
+ import time
7
+ import matplotlib.pyplot as plt
8
+
9
+ # Page Title
10
+ st.title("RAG7 - Real World RAG System")
11
+
12
+ st.markdown(
13
+ """
14
+ <style>
15
+ .stTextArea textarea {
16
+ background-color: white !important;
17
+ font-size: 20px !important;
18
+ color: black !important;
19
+ }
20
+ </style>
21
+ """,
22
+ unsafe_allow_html=True
23
+ )
24
+
25
+ # global retrieved_documents
26
+ # retrieved_documents = []
27
+
28
+ # global response
29
+ # response = ""
30
+
31
+ # global time_taken_for_response
32
+ # time_taken_for_response = 'N/A'
33
+
34
+ # @st.cache_data
35
+ # def load_data():
36
+ # load_data_from_faiss()
37
+
38
+ # data_status = load_data()
39
+
40
+ # Question Section
41
+ st.subheader("Hi, What do you want to know today?")
42
+ question = st.text_area("Enter your question:", placeholder="Type your question here...", height=100)
43
+ question = question.strip()
44
+
45
+ # # Submit Button
46
+ # if st.button("Submit"):
47
+ # start_time = time.time()
48
+ # retrieved_documents = retrieve_documents_hybrid(question, 10)
49
+ # response = generate_response_from_document(question, retrieved_documents)
50
+ # end_time = time.time()
51
+ # time_taken_for_response = end_time-start_time
52
+ # else:
53
+ # response = ""
54
+
55
+ # # Response Section
56
+ # st.subheader("Response")
57
+ # st.text_area("Generated Response:", value=response, height=150, disabled=True)
58
+
59
+ # # Metrics Section
60
+ # st.subheader("Metrics")
61
+
62
+ # col1, col2 = st.columns([1, 3]) # Creating two columns for button and metrics display
63
+
64
+ # with col1:
65
+ # if st.button("Calculate Metrics"):
66
+ # metrics = calculate_metrics(question, response, retrieved_documents, time_taken_for_response)
67
+ # else:
68
+ # metrics = ""
69
+
70
+ # with col2:
71
+ # st.text_area("Metrics:", value=metrics, height=100, disabled=True)
72
+
73
+ if "retrieved_documents" not in st.session_state:
74
+ st.session_state.retrieved_documents = []
75
+ if "response" not in st.session_state:
76
+ st.session_state.response = ""
77
+ if "time_taken_for_response" not in st.session_state:
78
+ st.session_state.time_taken_for_response = "N/A"
79
+ if "metrics" not in st.session_state:
80
+ st.session_state.metrics = {}
81
+ if "query_dataset" not in st.session_state:
82
+ st.session_state.query_dataset = ''
83
+
84
+ recent_questions = load_recent_questions()
85
+
86
+ # for visualization
87
+
88
+ # response_time = [q["response_time"] for q in recent_data["questions"]]
89
+ # labels = [f"Q{i+1}" for i in range(len(response_time))] # Labels for X-axis
90
+
91
+ # fig, ax = plt.subplots()
92
+ # ax.set_xlabel("Recent Questions")
93
+ # ax.set_ylabel("Time Taken for Response")
94
+ # ax.legend()
95
+ # st.sidebar.pyplot(fig)
96
+ if recent_questions and "questions" in recent_questions and recent_questions["questions"]:
97
+ recent_qns = list(reversed(recent_questions["questions"]))
98
+ st.sidebar.title("Analytics")
99
+
100
+ # Extract response times and labels
101
+ response_time = [q["response_time"] for q in recent_qns]
102
+ labels = [f"Q{i+1}" for i in range(len(response_time))]
103
+
104
+ # Plot graph
105
+ fig, ax = plt.subplots()
106
+ ax.plot(labels, response_time, marker="o", linestyle="-", color="skyblue")
107
+ ax.set_xlabel("Recent Questions")
108
+ ax.set_ylabel("Time Taken for Response (seconds)")
109
+ ax.set_title("Response Time Analysis")
110
+
111
+ # Display the plot in the sidebar
112
+ st.sidebar.pyplot(fig)
113
+
114
+ st.sidebar.markdown("---")
115
+
116
+ # Display Recent Questions
117
+ st.sidebar.title("Recent Questions")
118
+ for q in recent_qns: # Show latest first
119
+ st.sidebar.write(f"🔹 {q['question']}")
120
+ else:
121
+ st.sidebar.write("No recent questions")
122
+ # Separator
123
+
124
+ # Streamlit Sidebar for Recent Questions
125
+
126
+
127
+ # Submit Button
128
+ # if st.button("Submit"):
129
+ # start_time = time.time()
130
+ # st.session_state.retrieved_documents = retrieve_documents_hybrid(question, 10)
131
+ # st.session_state.response = generate_response_from_document(question, st.session_state.retrieved_documents)
132
+ # end_time = time.time()
133
+ # st.session_state.time_taken_for_response = end_time - start_time
134
+
135
+ if st.button("Submit"):
136
+ start_time = time.time()
137
+ st.session_state.metrics = {}
138
+ st.session_state.query_dataset = find_query_dataset(question)
139
+ st.session_state.retrieved_documents = retrieve_documents_hybrid(question, st.session_state.query_dataset, 10)
140
+ st.session_state.response = generate_response_from_document(question, st.session_state.retrieved_documents)
141
+ end_time = time.time()
142
+ st.session_state.time_taken_for_response = end_time - start_time
143
+ save_recent_question(question, st.session_state.time_taken_for_response)
144
+
145
+ # Display stored response
146
+ st.subheader("Response")
147
+ st.text_area("Generated Response:", value=st.session_state.response, height=150, disabled=True)
148
+
149
+ col1, col2 = st.columns([1, 3]) # Creating two columns for button and metrics display
150
+
151
+ # # Calculate Metrics Button
152
+ # with col1:
153
+ # if st.button("Calculate Metrics"):
154
+ # metrics = calculate_metrics(question, st.session_state.response, st.session_state.retrieved_documents, st.session_state.time_taken_for_response)
155
+ # else:
156
+ # metrics = {}
157
+
158
+ # with col2:
159
+ # #st.text_area("Metrics:", value=metrics, height=100, disabled=True)
160
+ # st.json(metrics)
161
+
162
+
163
+ # Calculate Metrics Button
164
+ with col1:
165
+ if st.button("Show Metrics"):
166
+ 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)
167
+ else:
168
+ metrics_ = {}
169
+
170
+ with col2:
171
+ #st.text_area("Metrics:", value=metrics, height=100, disabled=True)
172
+ st.json(st.session_state.metrics)
173
+
174
+ # Calculate RMSE Button
175
+ with col1:
176
+ if st.button("Show RMSE"):
177
+ st.session_state.rmse = compute_rmse( st.session_state.metrics.values(),st.session_state.response)
178
+ else:
179
+ metrics_ = {}
180
+
181
+ with col2:
182
+ #st.text_area("RMSE:", value=rmse, height=100, disabled=True)
183
+ st.json(st.session_state.rmse)
184
+
185
+