Wilame Lima
Update app.py
226bf18
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()