cb1716pics commited on
Commit
599d161
·
verified ·
1 Parent(s): 9665824

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +17 -18
  2. data_processing.py +7 -29
  3. evaluation.py +1 -0
  4. retrieval.py +0 -2
app.py CHANGED
@@ -71,25 +71,30 @@ import matplotlib.pyplot as plt
71
  # for visualization
72
  st.sidebar.title("Analytics")
73
 
74
- #context_relevance = [q["metrics"]["context_relevance"] for q in recent_data["questions"]]
75
- response_time = [q["metrics"]["response_time"] for q in recent_data["questions"]]
 
 
 
 
 
 
 
76
  labels = [f"Q{i+1}" for i in range(len(response_time))] # Labels for X-axis
77
 
78
  fig, ax = plt.subplots()
79
- #ax.plot(labels, context_relevance, marker="o", label="Context Relevance")
80
- #ax.plot(labels, response_time, marker="s", label="Response Time (sec)")
81
  ax.set_xlabel("Recent Questions")
82
- ax.set_ylabel("Time Taken for Response")
83
- ax.legend()
84
- st.sidebar.pyplot(fig)
85
 
86
  st.sidebar.markdown("---") # Separator
87
 
88
  # Streamlit Sidebar for Recent Questions
89
  st.sidebar.title("Recent Questions")
90
  for q in reversed(recent_data["questions"]): # Show latest first
91
- with st.expander(f"🔹 {q['question']}"):
92
- st.json(q["metrics"])
93
 
94
  # Submit Button
95
  # if st.button("Submit"):
@@ -105,13 +110,7 @@ if st.button("Submit"):
105
  st.session_state.response = generate_response_from_document(question, st.session_state.retrieved_documents)
106
  end_time = time.time()
107
  st.session_state.time_taken_for_response = end_time - start_time
108
-
109
- # Calculate metrics
110
- st.session_state.metrics = calculate_metrics(question, st.session_state.response, st.session_state.retrieved_documents, st.session_state.time_taken_for_response)
111
-
112
- # Save question & metrics
113
- save_recent_question(question, st.session_state.metrics)
114
-
115
 
116
  # Display stored response
117
  st.subheader("Response")
@@ -134,10 +133,10 @@ col1, col2 = st.columns([1, 3]) # Creating two columns for button and metrics d
134
  # Calculate Metrics Button
135
  with col1:
136
  if st.button("Show Metrics"):
137
- metrics_ = st.session_state.metrics
138
  else:
139
  metrics_ = {}
140
 
141
  with col2:
142
  #st.text_area("Metrics:", value=metrics, height=100, disabled=True)
143
- st.json(metrics_)
 
71
  # for visualization
72
  st.sidebar.title("Analytics")
73
 
74
+ # response_time = [q["response_time"] for q in recent_data["questions"]]
75
+ # labels = [f"Q{i+1}" for i in range(len(response_time))] # Labels for X-axis
76
+
77
+ # fig, ax = plt.subplots()
78
+ # ax.set_xlabel("Recent Questions")
79
+ # ax.set_ylabel("Time Taken for Response")
80
+ # ax.legend()
81
+ # st.sidebar.pyplot(fig)
82
+ response_time = [q["response_time"] for q in recent_data["questions"]]
83
  labels = [f"Q{i+1}" for i in range(len(response_time))] # Labels for X-axis
84
 
85
  fig, ax = plt.subplots()
86
+ ax.plot(labels, response_time, color="skyblue")
 
87
  ax.set_xlabel("Recent Questions")
88
+ ax.set_ylabel("Time Taken for Response (seconds)")
89
+ ax.set_title("Response Time Analysis")
90
+
91
 
92
  st.sidebar.markdown("---") # Separator
93
 
94
  # Streamlit Sidebar for Recent Questions
95
  st.sidebar.title("Recent Questions")
96
  for q in reversed(recent_data["questions"]): # Show latest first
97
+ st.sidebar.write(f"🔹 {q}")
 
98
 
99
  # Submit Button
100
  # if st.button("Submit"):
 
110
  st.session_state.response = generate_response_from_document(question, st.session_state.retrieved_documents)
111
  end_time = time.time()
112
  st.session_state.time_taken_for_response = end_time - start_time
113
+ save_recent_question(question, st.session_state.time_taken_for_response)
 
 
 
 
 
 
114
 
115
  # Display stored response
116
  st.subheader("Response")
 
133
  # Calculate Metrics Button
134
  with col1:
135
  if st.button("Show Metrics"):
136
+ st.session_state.metrics = calculate_metrics(question, st.session_state.response, st.session_state.retrieved_documents, st.session_state.time_taken_for_response)
137
  else:
138
  metrics_ = {}
139
 
140
  with col2:
141
  #st.text_area("Metrics:", value=metrics, height=100, disabled=True)
142
+ st.json(st.session_state.metrics )
data_processing.py CHANGED
@@ -93,13 +93,13 @@ def load_ragbench():
93
  ragbench[dataset] = load_dataset("rungalileo/ragbench", dataset)
94
  return ragbench
95
 
96
- def load_query_dataset(query_dataset):
97
  global query_dataset_data
98
- if query_dataset_data[query_dataset]:
99
- return query_dataset_data[query_dataset]
100
  else:
101
- query_dataset_data[query_dataset] = load_dataset("rungalileo/ragbench", query_dataset)
102
- return query_dataset_data[query_dataset]
103
 
104
  def load_faiss(query_dataset):
105
  global index
@@ -136,40 +136,18 @@ def load_recent_questions():
136
  return json.load(file)
137
  return {"questions": []} # Default structure if file doesn't exist
138
 
139
- def save_recent_question(question, metrics):
140
  data = load_recent_questions()
141
 
142
  # Append new question & metrics
143
  data["questions"].append({
144
  "question": question,
145
- "metrics": metrics
146
  })
147
 
148
  # Keep only the last 5 questions
149
  data["questions"] = data["questions"][-5:]
150
 
151
  # Write back to file
152
- with open(RECENT_QUESTIONS_FILE, "w") as file:
153
- json.dump(data, file, indent=4)
154
-
155
- # Function to save/update a question in the recent list
156
- def save_recent_question(question, metrics):
157
- data = load_recent_questions()
158
-
159
- # Check if the question already exists
160
- existing_questions = {q["question"]: q for q in data["questions"]}
161
-
162
- if question in existing_questions:
163
- # Update metrics & move to the latest position
164
- existing_questions[question]["metrics"] = metrics
165
- data["questions"].remove(existing_questions[question]) # Remove old entry
166
-
167
- # Add the updated/new question at the latest position
168
- data["questions"].append({"question": question, "metrics": metrics})
169
-
170
- # Keep only the last 10 questions
171
- data["questions"] = data["questions"][-5:]
172
-
173
- # Write back to the file
174
  with open(RECENT_QUESTIONS_FILE, "w") as file:
175
  json.dump(data, file, indent=4)
 
93
  ragbench[dataset] = load_dataset("rungalileo/ragbench", dataset)
94
  return ragbench
95
 
96
+ def load_query_dataset(q_dataset):
97
  global query_dataset_data
98
+ if query_dataset_data[q_dataset]:
99
+ return query_dataset_data[q_dataset]
100
  else:
101
+ query_dataset_data[q_dataset] = load_dataset("rungalileo/ragbench", q_dataset)
102
+ return query_dataset_data[q_dataset]
103
 
104
  def load_faiss(query_dataset):
105
  global index
 
136
  return json.load(file)
137
  return {"questions": []} # Default structure if file doesn't exist
138
 
139
+ def save_recent_question(question, response_time):
140
  data = load_recent_questions()
141
 
142
  # Append new question & metrics
143
  data["questions"].append({
144
  "question": question,
145
+ "response_time": response_time
146
  })
147
 
148
  # Keep only the last 5 questions
149
  data["questions"] = data["questions"][-5:]
150
 
151
  # Write back to file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  with open(RECENT_QUESTIONS_FILE, "w") as file:
153
  json.dump(data, file, indent=4)
evaluation.py CHANGED
@@ -102,6 +102,7 @@ def calculate_metrics(question, response, docs, time_taken):
102
 
103
  # Predicted metrics
104
  predicted_metrics = {
 
105
  "ground_truth": ground_truth_answer,
106
  "context_relevance": context_relevance(question, docs),
107
  "context_utilization": context_utilization(response, docs),
 
102
 
103
  # Predicted metrics
104
  predicted_metrics = {
105
+ "RAG_model_response": response,
106
  "ground_truth": ground_truth_answer,
107
  "context_relevance": context_relevance(question, docs),
108
  "context_utilization": context_utilization(response, docs),
retrieval.py CHANGED
@@ -9,8 +9,6 @@ from sentence_transformers import CrossEncoder
9
  reranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
10
 
11
  retrieved_docs = None
12
- query_dataset = 'hotpotqa'
13
-
14
 
15
  def retrieve_documents_hybrid(query, top_k=5):
16
  global query_dataset
 
9
  reranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
10
 
11
  retrieved_docs = None
 
 
12
 
13
  def retrieve_documents_hybrid(query, top_k=5):
14
  global query_dataset