Skip to content

Main

about_section(container, database, export)

Function to create the about section.

Parameters:

  • container (streamlit.container) –

    Parent Container or where the content will be displayed.

  • database (database.Repository.ChatRepositoryImp) –

    Chat Repository object

  • export (export.MarkdownToPdf.Export) –

    Export object

Source code in src/project/Main.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def about_section(container: st.container, database: ChatRepositoryImp, export: Export):
    """Function to create the about section.

    Args:
        container (st.container): Parent Container or where the content will be displayed.
        database (ChatRepositoryImp): Chat Repository object
        export (Export): Export object
    """
    with st.expander("About", expanded=True) as e:
        st.title('Chatbot')
        st.write('Welcome to Tutor Talk')
        st.button('➕ New Chat', key='new-chat', on_click=database.create_new_session, use_container_width=True,
                  disabled=len(database.get_current().get_messages()) == 0
                  )
        st.button('Export to PDF', key='export-pdf', use_container_width=True,
                  on_click=export.export_to_pdf,
                  args=[convert_to_markdown(copy.deepcopy(database.get_current())), container,
                        copy.deepcopy(database.get_current()).get_session_name() + '.pdf'],
                  disabled=len(database.get_current().get_messages()) == 0
                  )

acknowledgements_sec()

Acknowledgements section.

Source code in src/project/Main.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def acknowledgements_sec():
    """Acknowledgements section.
    """
    with st.expander('Acknowledgements', expanded=False):
        st.link_button('Source Code', url=Links.GITHUB.value, use_container_width=True)
        with st.container(border=True):
            st.image('src/project/img/male.png', width=80)
            st.subheader('Ayaan', divider=True)
            st.link_button('Github', url=Links.AYAAN.value, use_container_width=True)
        with st.container():
            st.caption('This project is made using:')
            st.link_button('Streamlit', url=Links.STREAMLIT.value, use_container_width=True)
            st.link_button('Python', url=Links.PYTHON.value, use_container_width=True)
            st.link_button('Gemini Ai', url=Links.GEMINI.value, use_container_width=True)
            st.link_button('MdPdf', url=Links.MD_PDF.value, use_container_width=True)

his_section(database, gemini)

Function to create the history section.

Parameters:

  • database (database.Repository.ChatRepositoryImp) –

    Chat Repository object.

  • gemini (gemini.Gemini) –

    Gemini object.

Source code in src/project/Main.py
70
71
72
73
74
75
76
77
78
79
80
81
def his_section(database: ChatRepositoryImp, gemini: Gemini):
    """Function to create the history section.

    Args:
        database (ChatRepositoryImp): Chat Repository object.
        gemini (Gemini): Gemini object.
    """
    with st.expander('History', expanded=True):
        st.subheader('All history')
        for session in reversed(get_value_from_state(State.SESSION_LIST_STATE.value)):
            st.button(session.get_session_name(), key=session.get_session_id,
                      on_click=populate_messages, args=[session.get_session_id(), database, gemini, ])

message_container(message)

Function to create a message container.

Parameters:

  • message (database.Session.Message) –

    Message object

Source code in src/project/Main.py
39
40
41
42
43
44
45
46
47
48
49
50
def message_container(message: Message):
    """Function to create a message container.

    Args:
        message (Message): Message object
    """
    if message.get_role() == Role.MODEL:
        with st.chat_message('AI'):
            st.markdown(message.get_content(), unsafe_allow_html=True)
    else:
        with st.chat_message('User'):
            st.markdown(message.get_content(), unsafe_allow_html=True)

populate_messages(session_id, database, gemini)

Function to populate the messages.

Parameters:

  • session_id (int) –

    Current session id

  • database (database.Repository.ChatRepositoryImp) –

    Database object

  • gemini (gemini.Gemini) –

    Gemini object

Source code in src/project/Main.py
58
59
60
61
62
63
64
65
66
67
def populate_messages(session_id: int, database: ChatRepositoryImp, gemini: Gemini):
    """Function to populate the messages.

    Args:
        session_id (int): Current session id
        database (ChatRepositoryImp): Database object
        gemini (Gemini): Gemini object
    """
    database.get_current_session(session_id)
    gemini.start_new_chat(database.get_current().get_messages())

progress_message_in_other_thread(input_message, database, gemini)

Function to process the message in another thread.

Parameters:

  • input_message (str) –

    Input from user.

  • database (database.Repository.ChatRepositoryImp) –

    Chat Repository object.

  • gemini (gemini.Gemini) –

    Gemini object.

Source code in src/project/Main.py
101
102
103
104
105
106
107
108
109
110
def progress_message_in_other_thread(input_message: str, database: ChatRepositoryImp, gemini: Gemini):
    """ Function to process the message in another thread.

    Args:
        input_message (str): Input from user.
        database (ChatRepositoryImp): Chat Repository object.
        gemini (Gemini): Gemini object.
    """
    response = gemini.send_message(input_message, Role.USER)
    database.add_message(response.get_content(), Role.MODEL)

thinking(input_message, _database, _gemini)

Function to process the message in an async manner.

Parameters:

  • input_message (str) –

    Input from user.

  • _database (database.Repository.ChatRepositoryImp) –

    Chat Repository object.

  • _gemini (gemini.Gemini) –

    Gemini object.

Source code in src/project/Main.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
@st.cache_data()
def thinking(input_message: str, _database: ChatRepositoryImp, _gemini: Gemini):
    """Function to process the message in an async manner.

    Args:
        input_message (str): Input from user.
        _database (ChatRepositoryImp): Chat Repository object.
        _gemini (Gemini): Gemini object.
    """

    thread = threading.Thread(target=progress_message_in_other_thread, args=(input_message, _database, _gemini))
    add_script_run_ctx(thread)
    thread.start()
    thread.join()
    st.rerun()