File size: 3,454 Bytes
ec2398c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from CallCenter import get_included_files, convertMp3ToWav, diarize_wav_file
import os

from segment_wave_files import segment_wave_files
from transcript_analysis import transcript_analysis
from transcribe_files import transcribe_segments

location, wave_files = get_included_files()


def main():
    # --- Streamlit App ---
    st.title("Call Center Analysis")

    # --- Selectable Elements ---

    def get_file_sel():
        if selected_file is None:
            return
        st.session_state.selected_file = location+selected_file  # Store the selected option in session state
        st.write(f"{selected_file} is selected") # Display feedback
        st.session_state.wave_file = st.session_state.selected_file

    selected_file = st.selectbox("Select an element:", wave_files)
    get_file_sel()

    # --- File Upload ---
    uploaded_file = st.file_uploader("Upload a file", type=['mp3', 'wav'])
    if uploaded_file:
        st.session_state.uploaded_file = uploaded_file.name
        if st.session_state.uploaded_file is not None:
            if os.path.splitext(uploaded_file.name)[1].lower()==".mp3":
                bytes_data = uploaded_file.read()  # read the content of the file in binary
                if not os.path.exists("/tmp"):
                    os.makedirs("/tmp")

                with open(os.path.join("/tmp", uploaded_file.name), "wb") as f:
                    f.write(bytes_data)  # write this content elsewhere
                wav_file = convertMp3ToWav(f"/tmp/{uploaded_file.name}")
                st.session_state.wave_file = wav_file  # Store the selected option in session state
            else:
                bytes_data = uploaded_file.read()  # read the content of the file in binary
                with open(os.path.join("/tmp", uploaded_file.name), "wb") as f:
                    f.write(bytes_data)  # write this content elsewhere
                st.session_state.wave_file = os.path.join("/tmp", uploaded_file.name)  # Store the selected option in session state

            st.write(f"You uploaded: {uploaded_file.name}")  # Display feedback
        uploaded_file = None

    analysis = ""
    # --- Start Button ---
    if st.button("Start"):
        if st.session_state.wave_file is not None:
            with st.spinner("Please wait..."):
                try:
                    speakers = diarize_wav_file(st.session_state.wave_file )

                    speakers = segment_wave_files(speakers, st.session_state.wave_file )

                    transcripts = transcribe_segments(speakers)
                    analysis = transcript_analysis(transcripts)
                except Exception as e:
                    st.error(f"Error processing file: {st.session_state.wave_file}")
        else:
            st.warning("Please upload a file.")

    # --- Reset Button ---
    if st.button("Reset"):
        st.experimental_rerun()

    # --- Large Text Box (Display Only) ---
    if analysis != "":
        analysis = analysis[9:]
        index=analysis.lower().find("sentiment")
        summary=analysis[0:index].lstrip("\n")

        sentiment = analysis[index:]
        index=sentiment.lower().find(":")
        sentiment = sentiment[index+1:].lstrip("\n")

        height = 34*15
        st.text_area("SUMMARY:", value=summary, disabled=True, height=height)
        st.text_area("SENTIMENT:", value=sentiment, disabled=True, height=height)


main()