File size: 5,479 Bytes
58d1f0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226bf18
58d1f0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from functions import *

# set the title
st.sidebar.title(DASHBOARD_TITLE)
info_section = st.empty()

# add an explanation of what is NER and why it is important for medical tasks
st.sidebar.markdown(SIDEBAR_EXPLANATION)

# if the assistant has already answered, hide the input fields
if 'assistant_answer' in st.session_state and st.session_state['assistant_answer']:

    assistant_answer = st.session_state['assistant_answer']

    # write a title and the original text
    st.markdown("## Assistant response")
    st.text_area("Original text", value=assistant_answer['original_text'], height=200, disabled=True)

    # propose to ask another question
    if st.button("Submit another text"):
        del st.session_state['assistant_answer']
        st.rerun()

    # if the response is a text:
    if 'textual_response' in assistant_answer:
        st.markdown("---")
        with st.chat_message("Assistant"):
            st.write(assistant_answer['textual_response'])

    # otherwise, create visual elements for the response
    if isinstance(assistant_answer, dict):

        if 'topics' in assistant_answer:
            st.markdown("---")
            st.markdown("#### Main topics:")
            topic_bullets = [annotate(topic, value) for topic, value in assistant_answer['topics'].items()]
            for topic in topic_bullets:
                annotated_text(topic)

        if 'summary' in assistant_answer:
            st.markdown("---")
            st.markdown("#### Summary:")
            st.write(assistant_answer['summary'])

        if 'recommendation' in assistant_answer:
            st.markdown("---")
            st.markdown("#### Recommendation:")
            st.write(assistant_answer['recommendation'])

        # create 2 columns to display the rating average and the sentiment distribution
        if 'rating' in assistant_answer or 'sentiments' in assistant_answer:
            st.markdown("---")
        sentiment_column, average_column = st.columns([3,1])

        if 'sentiments' in assistant_answer:
            # display a chart with the sentiment distribution. Use streamlit's bar_chart function
            sentiment_column.markdown("#### Sentiment distribution:")
            sentiment_distribution = Counter(assistant_answer['sentiments'])
            sentiment_column.bar_chart(sentiment_distribution)

        if 'rating' in assistant_answer:
            # display the rating average
            average_column.markdown("#### Rating average:")
            rating_average = str((np.mean(assistant_answer['rating']).round(2)))
            average_column.markdown(f'#### {rating_average}')

else:

    # create a text field to input the text
    text = ''

    st.subheader("Patient feedback analysis")
    st.write('This assistant will help you get insights on the overall sentiment and the most common topics mentioned in patient feedbacks. To start, submit a text with patient feedbacks or a file containing the feedbacks.\nIf you are out of ideas, you can use the example text provided by clicking the button "Fill with an example".')
    if 'use_example_text' in st.session_state and st.session_state['use_example_text'] == True:
        text_field = st.text_area("Paste the text here", value=EXAMPLE_REVIEW)
    else:
        text_field = st.text_area("Paste the text here")

    # if the text field is empty, ask for a file
    if text_field == '' or text_field is None:
        st.write('OR')
        text_file = st.file_uploader("Upload a text file", type=['txt','csv'])

        # if a text file is uploaded, convert it to a string
        if text_file:
            text = text_file.read().decode('utf-8')

    # get the text field OR the text file
    if text == '':
        text = text_field

    # if the user is out of ideas, propose an example
    if not text:
        st.info('Out of ideas? Try with an example')
        example_button = st.button("Fill with an example")

        if example_button:
            st.session_state['use_example_text'] = True
            st.rerun()

    # create a button to submit the text
    user_input = st.button("Submit")  

    # create a variable to store the assistant answer
    assistant_answer = ''

    # if the user has submitted the text
    if user_input:

        token_amount = get_token_amount(text + SYSTEM_PROMPT)
        if token_amount > 8000:
            st.error("The text is too long for the model. Please reduce the number of characters.")
            st.stop()

        # make the request
        with st.spinner("Generating the response..."):
            
            messages = [{'role': 'system', 'content': SYSTEM_PROMPT},
                        {'role': 'user',   'content': text}]
            
            # query the model
            assistant_answer = make_request(messages)

            # try to convert the response to a dictionary
            try:
                assistant_answer = json.loads(assistant_answer)
                assistant_answer['original_text'] = text
            except:
                string_assistant_answer = dict()
                string_assistant_answer['original_text'] = text
                string_assistant_answer['textual_response'] = assistant_answer
                assistant_answer = string_assistant_answer

        # store the assistant answer in the session state
        st.session_state['assistant_answer'] = assistant_answer

        # if all went well, remove the st container
        st.rerun()