File size: 1,626 Bytes
ebfef12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9c9c4c
ebfef12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import google.generativeai as genai
import streamlit as st


def main():
    """main"""

    st.set_page_config(
        page_title="Gemini Chatbot",
        page_icon="🤖",
        layout="centered",
        initial_sidebar_state="collapsed",
    )

    st.header("The Gemini Chatbot 🤖", divider="rainbow")

    st.subheader(
        "Enjoy :red[talking] with :green[Google Gemini] :sunglasses:"
    )

    st.markdown("[Checkout the repository](https://github.com/ThivaV/gemini_chatbot)")

    gemini_key = st.text_input("Enter your Google Gemini API key 👇", type="password")

    genai.configure(api_key=gemini_key)

    model = genai.GenerativeModel("gemini-pro")
    chat = model.start_chat(history=[])

    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])

    if prompt := st.chat_input("Say something"):
        # user message
        with st.chat_message("user"):
            st.markdown(prompt)
            st.session_state.messages.append({"role": "user", "content": prompt})

        response = chat.send_message(prompt, stream=True)
        complete_response = ""
        with st.chat_message("assistant"):
            for chunk in response:
                complete_response += chunk.text
                st.write(chunk.text)

            st.session_state.messages.append(
                {"role": "assistant", "content": complete_response}
            )


if __name__ == "__main__":

    # initialize streamlit session state
    if "messages" not in st.session_state:
        st.session_state.messages = []

    main()