File size: 3,823 Bytes
7fafac4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import json
from io import BytesIO
import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import os

def save_feedback_og(feedback):

    feedback_file = 'feedback_data.json'
    if os.path.exists(feedback_file):
        with open(feedback_file, 'r') as f:
            feedback_data = json.load(f)
    else:
        feedback_data = []
    # tpl = {
    #     'question' : question,
    #     'answer' : answer,
    #     'context' : context,
    #     'options' : options,
    #     'rating' : rating,
    # }
    # feedback_data[question] = rating
    feedback_data.append(feedback)
    
    print(feedback_data)
    with open(feedback_file, 'w') as f:
        json.dump(feedback_data, f)
    st.session_state.feedback_data.append(feedback)
    return feedback_file

def collect_feedback(i,question, answer, context, options):
    st.write("Please provide feedback for this question:")
    edited_question = st.text_input("Enter improved question",value=question,key=f'fdx1{i}')
    clarity = st.slider("Clarity", 1, 5, 3, help="1 = Very unclear, 5 = Very clear",key=f'fdx2{i}')
    difficulty = st.slider("Difficulty", 1, 5, 3, help="1 = Very easy, 5 = Very difficult",key=f'fdx3{i}')
    relevance = st.slider("Relevance", 1, 5, 3, help="1 = Not relevant, 5 = Highly relevant",key=f'fdx4{i}')
    option_quality = st.slider("Quality of Options", 1, 5, 3, help="1 = Poor options, 5 = Excellent options",key=f'fdx5{i}')
    overall_rating = st.slider("Overall Rating", 1, 5, 3, help="1 = Poor, 5 = Excellent",key=f'fdx6{i}')
    comments = st.text_input("Additional Comments", "",key=f'fdx7{i}')

    if st.button("Submit Feedback",key=f'fdx8{i}'):
        feedback = {
            "context": context,
            "question": question,
            'edited_question':edited_question,
            "answer": answer,
            "options": options,
            "clarity": clarity,
            "difficulty": difficulty,
            "relevance": relevance,
            "option_quality": option_quality,
            "overall_rating": overall_rating,
            "comments": comments
        }
        # save_feedback(feedback)
        save_feedback_og(feedback)
            
        st.success("Thank you for your feedback!")

def analyze_feedback():
    if not st.session_state.feedback_data:
        st.warning("No feedback data available yet.")
        return

    df = pd.DataFrame(st.session_state.feedback_data)
    
    st.write("Feedback Analysis")
    st.write(f"Total feedback collected: {len(df)}")
    
    metrics = ['clarity', 'difficulty', 'relevance', 'option_quality', 'overall_rating']
    
    for metric in metrics:
        fig, ax = plt.subplots()
        df[metric].value_counts().sort_index().plot(kind='bar', ax=ax)
        plt.title(f"Distribution of {metric.capitalize()} Ratings")
        plt.xlabel("Rating")
        plt.ylabel("Count")
        st.pyplot(fig)

    st.write("Average Ratings:")
    st.write(df[metrics].mean())

    # Word cloud of comments
    comments = " ".join(df['comments'])
    if len(comments) > 1:
        wordcloud = WordCloud(width=800, height=400, background_color='white').generate(comments)
        fig, ax = plt.subplots()
        plt.imshow(wordcloud, interpolation='bilinear')
        plt.axis("off")
        st.pyplot(fig)


def export_feedback_data():
    if not st.session_state.feedback_data:
        st.warning("No feedback data available.")
        return None

    # Convert feedback data to JSON
    json_data = json.dumps(st.session_state.feedback_data, indent=2)
    
    # Create a BytesIO object
    buffer = BytesIO()
    buffer.write(json_data.encode())
    buffer.seek(0)
    
    return buffer