report-week-7 / utils.py
gvarnayev's picture
add base for week 7
701866f
from typing import Dict
import hmac
import streamlit as st
def process_olivia_data(transcription: dict) -> str:
phrases = transcription['phrases']
conversation = ''
for phrase in phrases:
conversation += 'Agent: ' if phrase['channel'] == 0 else 'Customer: '
parsed_phrase = ''.join(word['text'] for word in phrase['words'])
conversation += parsed_phrase
conversation += '\n\n\n'
return {'text': conversation}
def process_tethr_data(data: Dict) -> Dict:
extracted_words = []
participants = data.get("participants", [])
for participant in participants:
participant_type = 'Agent' if 'Agent' in participant.get("refType", "") else 'Customer'
utterances = participant.get("utterances", [])
for utterance in utterances:
words = utterance.get("words") # by words, not utterances since messed up
for word in words:
word_obj = {
'participant': participant_type,
'word': word.get('content', ''),
'start': word.get('startMs', None),
'end': word.get('endMs', None)
}
extracted_words.append(word_obj)
extracted_words.sort(key=lambda x: x['start']) # Sort by start time
conversation = ''
participant = None
for word in extracted_words:
if word['participant'] != participant:
if participant:
conversation += '\n\n'
participant = word['participant']
conversation += f'{participant}: '
conversation += f"{word['word']} "
return {'id': data['callId'], 'text': conversation}
def check_password():
"""Returns `True` if the user had the correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if hmac.compare_digest(st.session_state["password"], st.secrets["PASSWORD"]):
st.session_state["password_correct"] = True
del st.session_state["password"] # Don't store the password.
else:
st.session_state["password_correct"] = False
# Return True if the passward is validated.
if st.session_state.get("password_correct", False):
return True
# Show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
if "password_correct" in st.session_state:
st.error("Incorrect password")
return False