DevBM commited on
Commit
3b56429
1 Parent(s): fd825ab

updating feedback section

Browse files

added more feedback parameters.
made functions for all the work with feedback.

Files changed (1) hide show
  1. app.py +138 -67
app.py CHANGED
@@ -30,11 +30,15 @@ import uuid
30
  import time
31
  import asyncio
32
  import aiohttp
 
 
 
33
  # '-----------------'
34
  import smtplib
35
  from email.mime.multipart import MIMEMultipart
36
  from email.mime.text import MIMEText
37
  from email.mime.base import MIMEBase
 
38
  from email import encoders
39
  # '------------------'
40
  print("***************************************************************")
@@ -81,6 +85,7 @@ def get_state(session_id):
81
  def set_state(session_id, key, value):
82
  st.session_state.session_states[session_id][key] = value
83
 
 
84
  @st.cache_resource
85
  def load_model(modelname):
86
  model_name = modelname
@@ -114,6 +119,8 @@ nlp, s2v = load_nlp_models()
114
  similarity_model, spell = load_qa_models()
115
  context_model = similarity_model
116
  model, tokenizer = load_model(modelname)
 
 
117
  # Info Section
118
  def display_info():
119
  st.sidebar.title("Information")
@@ -154,7 +161,7 @@ def get_pdf_text(pdf_file):
154
  text += page.get_text()
155
  return text
156
 
157
- def save_feedback(question, answer, rating, options, context):
158
  feedback_file = 'question_feedback.json'
159
  if os.path.exists(feedback_file):
160
  with open(feedback_file, 'r') as f:
@@ -175,34 +182,117 @@ def save_feedback(question, answer, rating, options, context):
175
  json.dump(feedback_data, f)
176
 
177
  return feedback_file
 
178
  # -----------------------------------------------------------------------------------------
179
- def send_email_with_attachment(email_subject, email_body, recipient_emails, sender_email, sender_password, attachment_path):
180
- msg = MIMEMultipart()
181
- msg['From'] = sender_email
182
- msg['To'] = ", ".join(recipient_emails) # Join the list of recipients with commas
183
-
184
- msg['Subject'] = email_subject
185
-
186
- msg.attach(MIMEText(email_body, 'plain'))
187
-
188
- attachment = open(attachment_path, 'rb')
189
- part = MIMEBase('application', 'octet-stream')
190
- part.set_payload(attachment.read())
191
- encoders.encode_base64(part)
192
- part.add_header('Content-Disposition', f'attachment; filename={os.path.basename(attachment_path)}')
193
-
194
- msg.attach(part)
195
- attachment.close()
196
-
197
- with smtplib.SMTP('smtp.gmail.com', 587) as server:
198
- server.starttls()
199
- print(sender_email)
200
- print(sender_password)
201
- server.login(sender_email, sender_password)
202
- text = msg.as_string()
203
- server.sendmail(sender_email, recipient_emails, text)
 
 
 
 
 
204
  # ----------------------------------------------------------------------------------
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
  # Function to clean text
207
  def clean_text(text):
208
  text = re.sub(r"[^\x00-\x7F]", " ", text)
@@ -517,6 +607,8 @@ def main():
517
  st.title(":blue[Question Generator System]")
518
  session_id = get_session_id()
519
  state = initialize_state(session_id)
 
 
520
 
521
  with st.sidebar:
522
  show_info = st.toggle('Show Info',True)
@@ -609,20 +701,13 @@ def main():
609
 
610
  # q['context'] = st.text_area(f"Edit Context {i+1}:", value=q['context'], key=f"context_{i}")
611
  if enable_feedback_mode:
612
- q['question'] = st.text_input(f"Edit Question {i+1}:", value=q['question'], key=f"question_{i}")
613
- q['rating'] = st.select_slider(f"Rate this question (1-5)", options=[1, 2, 3, 4, 5], key=f"rating_{i}")
614
- if st.button(f"Submit Feedback for Question {i+1}", key=f"submit_{i}"):
615
- feedback_file=save_feedback(q['question'], q['answer'], q['rating'], q['options'], q['context'])
616
- st.success(f"Feedback submitted for Question {i+1}")
617
- pswd = st.secrets['EMAIL_PASSWORD']
618
- send_email_with_attachment(
619
- email_subject='feedback from QGen',
620
- email_body='Please find the attached feedback JSON file.',
621
- recipient_emails=['apjc01unique@gmail.com', 'channingfisher7@gmail.com'],
622
- sender_email='apjc01unique@gmail.com',
623
- sender_password=pswd,
624
- attachment_path=feedback_file)
625
- st.write("Feedback sent to admin")
626
  st.write("---")
627
 
628
 
@@ -648,33 +733,19 @@ def main():
648
 
649
  # View Feedback Statistics
650
  with st.expander("View Feedback Statistics"):
651
- feedback_file = 'question_feedback.json'
652
- if os.path.exists(feedback_file):
653
- with open(feedback_file, 'r') as f:
654
- feedback_data = json.load(f)
655
-
656
- st.subheader("Feedback Statistics")
657
-
658
- # Calculate average rating
659
- ratings = [feedback['rating'] for feedback in feedback_data]
660
- avg_rating = sum(ratings) / len(ratings) if ratings else 0
661
- st.write(f"Average Question Rating: {avg_rating:.2f}")
662
-
663
- # Show distribution of ratings
664
- rating_counts = {i: ratings.count(i) for i in range(1, 6)}
665
- st.bar_chart(rating_counts)
666
-
667
- # Show some highly rated questions
668
- st.subheader("Highly Rated Questions")
669
- sorted_feedback = sorted(feedback_data, key=lambda x: x['rating'], reverse=True)
670
- top_questions = sorted_feedback[:5]
671
- for feedback in top_questions:
672
- st.write(f"Question: {feedback['question']}")
673
- st.write(f"Answer: {feedback['answer']}")
674
- st.write(f"Rating: {feedback['rating']}")
675
- st.write("---")
676
- else:
677
- st.write("No feedback data available yet.")
678
  print("********************************************************************************")
679
 
680
  if __name__ == '__main__':
 
30
  import time
31
  import asyncio
32
  import aiohttp
33
+ from datetime import datetime
34
+ import base64
35
+ from io import BytesIO
36
  # '-----------------'
37
  import smtplib
38
  from email.mime.multipart import MIMEMultipart
39
  from email.mime.text import MIMEText
40
  from email.mime.base import MIMEBase
41
+ from email.mime.application import MIMEApplication
42
  from email import encoders
43
  # '------------------'
44
  print("***************************************************************")
 
85
  def set_state(session_id, key, value):
86
  st.session_state.session_states[session_id][key] = value
87
 
88
+
89
  @st.cache_resource
90
  def load_model(modelname):
91
  model_name = modelname
 
119
  similarity_model, spell = load_qa_models()
120
  context_model = similarity_model
121
  model, tokenizer = load_model(modelname)
122
+
123
+
124
  # Info Section
125
  def display_info():
126
  st.sidebar.title("Information")
 
161
  text += page.get_text()
162
  return text
163
 
164
+ def save_feedback_og(question, answer, rating, options, context):
165
  feedback_file = 'question_feedback.json'
166
  if os.path.exists(feedback_file):
167
  with open(feedback_file, 'r') as f:
 
182
  json.dump(feedback_data, f)
183
 
184
  return feedback_file
185
+
186
  # -----------------------------------------------------------------------------------------
187
+ def send_email_with_attachment(email_subject, email_body, recipient_emails, sender_email, sender_password, attachment):
188
+ smtp_server = "smtp.gmail.com" # Replace with your SMTP server
189
+ smtp_port = 587 # Replace with your SMTP port
190
+
191
+ # Create the email message
192
+ message = MIMEMultipart()
193
+ message['From'] = sender_email
194
+ message['To'] = ", ".join(recipient_emails)
195
+ message['Subject'] = email_subject
196
+ message.attach(MIMEText(email_body, 'plain'))
197
+
198
+ # Attach the feedback data if available
199
+ if attachment:
200
+ attachment_part = MIMEApplication(attachment.getvalue(), Name="feedback_data.json")
201
+ attachment_part['Content-Disposition'] = f'attachment; filename="feedback_data.json"'
202
+ message.attach(attachment_part)
203
+
204
+ # Send the email
205
+ try:
206
+ with smtplib.SMTP(smtp_server, smtp_port) as server:
207
+ server.starttls()
208
+ print(sender_email)
209
+ print(sender_password)
210
+ server.login(sender_email, sender_password)
211
+ text = message.as_string()
212
+ server.sendmail(sender_email, recipient_emails, text)
213
+ return True
214
+ except Exception as e:
215
+ st.error(f"Failed to send email: {str(e)}")
216
+ return False
217
  # ----------------------------------------------------------------------------------
218
 
219
+ def collect_feedback(i,question, answer, context, options):
220
+ st.write("Please provide feedback for this question:")
221
+ edited_question = st.text_input("Enter improved question",value=question,key=f'fdx1{i}')
222
+ clarity = st.slider("Clarity", 1, 5, 3, help="1 = Very unclear, 5 = Very clear",key=f'fdx2{i}')
223
+ difficulty = st.slider("Difficulty", 1, 5, 3, help="1 = Very easy, 5 = Very difficult",key=f'fdx3{i}')
224
+ relevance = st.slider("Relevance", 1, 5, 3, help="1 = Not relevant, 5 = Highly relevant",key=f'fdx4{i}')
225
+ option_quality = st.slider("Quality of Options", 1, 5, 3, help="1 = Poor options, 5 = Excellent options",key=f'fdx5{i}')
226
+ overall_rating = st.slider("Overall Rating", 1, 5, 3, help="1 = Poor, 5 = Excellent",key=f'fdx6{i}')
227
+ comments = st.text_input("Additional Comments", "",key=f'fdx7{i}')
228
+
229
+ if st.button("Submit Feedback",key=f'fdx8{i}'):
230
+ feedback = {
231
+ "question": question,
232
+ 'edited_question':edited_question,
233
+ "answer": answer,
234
+ "options": options,
235
+ "clarity": clarity,
236
+ "difficulty": difficulty,
237
+ "relevance": relevance,
238
+ "option_quality": option_quality,
239
+ "overall_rating": overall_rating,
240
+ "comments": comments
241
+ }
242
+ save_feedback(feedback)
243
+ st.success("Thank you for your feedback!")
244
+
245
+ def save_feedback(feedback):
246
+ st.session_state.feedback_data.append(feedback)
247
+
248
+ def analyze_feedback():
249
+ if not st.session_state.feedback_data:
250
+ st.warning("No feedback data available yet.")
251
+ return
252
+
253
+ df = pd.DataFrame(st.session_state.feedback_data)
254
+
255
+ st.write("Feedback Analysis")
256
+ st.write(f"Total feedback collected: {len(df)}")
257
+
258
+ metrics = ['clarity', 'difficulty', 'relevance', 'option_quality', 'overall_rating']
259
+
260
+ for metric in metrics:
261
+ fig, ax = plt.subplots()
262
+ df[metric].value_counts().sort_index().plot(kind='bar', ax=ax)
263
+ plt.title(f"Distribution of {metric.capitalize()} Ratings")
264
+ plt.xlabel("Rating")
265
+ plt.ylabel("Count")
266
+ st.pyplot(fig)
267
+
268
+ st.write("Average Ratings:")
269
+ st.write(df[metrics].mean())
270
+
271
+ # Word cloud of comments
272
+ comments = " ".join(df['comments'])
273
+ if len(comments) > 1:
274
+ wordcloud = WordCloud(width=800, height=400, background_color='white').generate(comments)
275
+ fig, ax = plt.subplots()
276
+ plt.imshow(wordcloud, interpolation='bilinear')
277
+ plt.axis("off")
278
+ st.pyplot(fig)
279
+
280
+
281
+ def export_feedback_data():
282
+ if not st.session_state.feedback_data:
283
+ st.warning("No feedback data available.")
284
+ return None
285
+
286
+ # Convert feedback data to JSON
287
+ json_data = json.dumps(st.session_state.feedback_data, indent=2)
288
+
289
+ # Create a BytesIO object
290
+ buffer = BytesIO()
291
+ buffer.write(json_data.encode())
292
+ buffer.seek(0)
293
+
294
+ return buffer
295
+
296
  # Function to clean text
297
  def clean_text(text):
298
  text = re.sub(r"[^\x00-\x7F]", " ", text)
 
607
  st.title(":blue[Question Generator System]")
608
  session_id = get_session_id()
609
  state = initialize_state(session_id)
610
+ if 'feedback_data' not in st.session_state:
611
+ st.session_state.feedback_data = []
612
 
613
  with st.sidebar:
614
  show_info = st.toggle('Show Info',True)
 
701
 
702
  # q['context'] = st.text_area(f"Edit Context {i+1}:", value=q['context'], key=f"context_{i}")
703
  if enable_feedback_mode:
704
+ collect_feedback(
705
+ i,
706
+ question = q['question'],
707
+ answer = q['answer'],
708
+ context = q['context'],
709
+ options = q['options'],
710
+ )
 
 
 
 
 
 
 
711
  st.write("---")
712
 
713
 
 
733
 
734
  # View Feedback Statistics
735
  with st.expander("View Feedback Statistics"):
736
+ analyze_feedback()
737
+ if st.button("Export Feedback"):
738
+ feedback_data = export_feedback_data()
739
+ pswd = st.secrets['EMAIL_PASSWORD']
740
+ send_email_with_attachment(
741
+ email_subject='feedback from QGen',
742
+ email_body='Please find the attached feedback JSON file.',
743
+ recipient_emails=['apjc01unique@gmail.com', 'channingfisher7@gmail.com'],
744
+ sender_email='apjc01unique@gmail.com',
745
+ sender_password=pswd,
746
+ attachment=feedback_data
747
+ )
748
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
749
  print("********************************************************************************")
750
 
751
  if __name__ == '__main__':