File size: 6,142 Bytes
5759094
7cd4c4f
5759094
 
 
9270b84
7cd4c4f
2b27d13
 
5759094
 
 
 
2b27d13
5759094
 
 
 
 
 
 
 
 
 
 
 
 
ff18760
4cac5ca
5759094
 
 
 
 
 
 
 
 
 
 
fa8e86c
 
 
5759094
fa8e86c
5759094
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1340ba9
fa8e86c
5759094
fa8e86c
 
 
 
 
 
5759094
fa8e86c
 
7cd4c4f
fa8e86c
 
 
 
 
7cd4c4f
fa8e86c
 
 
 
 
 
 
7cd4c4f
fa8e86c
 
 
 
 
 
cc8c280
7cd4c4f
fa8e86c
7cd4c4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5759094
 
 
2b27d13
ff18760
5759094
 
 
 
 
 
2b27d13
5759094
 
 
 
 
 
 
 
2b27d13
5759094
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import streamlit as st
import g4f
from g4f.client import Client
import sqlite3
import google.generativeai as genai
# import pyttsx3
import pyperclip


def local_css(file_name):
    with open(file_name) as f:
        st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)


local_css("style.css")

# Create a connection to the database
conn = sqlite3.connect('chat_history.db')
c = conn.cursor()

# Create table if not exists
try:
    c.execute('''CREATE TABLE IF NOT EXISTS chat_history
                 (conversation_id INTEGER, role TEXT, content TEXT)''')
    conn.commit()
except Exception as e:
    st.error(f"An error occurred: {e}")


# Streamlit app
def main():
    try:
        if "chat_history" not in st.session_state:
            st.session_state.chat_history = []

        if "conversation_id" not in st.session_state:
            st.session_state.conversation_id = 1

        models = {
            "๐Ÿš€ Airoboros 70B": "airoboros-70b",
            "๐Ÿ‘‘ Gemini 1.0": "gemini-1.0-pro",
            "๐Ÿงจ Gemini 1.0 Pro ": "gemini-1.0-pro-001",
            "โšก Gemini 1.0 pro latest": "gemini-1.0-pro-latest",
            "๐Ÿ”ฎ Gemini Pro": "gemini-pro",
            "๐ŸŽ‰ Gemini pro vision": "gemini-pro-vision"
        }

        columns = st.columns(3)  # Split the layout into three columns
        with columns[0]:
            st.header("DarkGPT")

        with columns[2]:
            selected_model_display_name = st.selectbox("Select Model", list(models.keys()), index=0)

        with columns[1]:
            selected_model = models[selected_model_display_name]

        # Sidebar (left side) - New chat button
        if st.sidebar.button("โœจ New Chat", key="new_chat_button"):
            st.session_state.chat_history.clear()
            st.session_state.conversation_id += 1

        # Sidebar (left side) - Display saved chat
        st.sidebar.write("Chat History")
        c.execute("SELECT DISTINCT conversation_id FROM chat_history")
        conversations = c.fetchall()
        for conv_id in reversed(conversations):
            c.execute("SELECT content FROM chat_history WHERE conversation_id=? AND role='bot' LIMIT 1",
                      (conv_id[0],))
            first_bot_response = c.fetchone()
            if first_bot_response:
                if st.sidebar.button(" ".join(first_bot_response[0].split()[0:5])):
                    display_conversation(conv_id[0])

        # Sidebar (left side) - Clear Chat History button
        if st.sidebar.button("Clear Chat History โœ–๏ธ"):
            st.session_state.chat_history.clear()
            c.execute("DELETE FROM chat_history")
            conn.commit()

        # Main content area (center)
        st.markdown("---")

        user_input = st.chat_input("Ask Anything ...")

        if user_input:
            if selected_model == "airoboros-70b":
                try:
                    client = Client()
                    response = client.chat.completions.create(
                        model=models[selected_model_display_name],
                        messages=[{"role": "user", "content": user_input}],
                    )
                    bot_response = response.choices[0].message.content

                    st.session_state.chat_history.append({"role": "user", "content": user_input})
                    st.session_state.chat_history.append({"role": "bot", "content": bot_response})

                    # Store chat in the database
                    for chat in st.session_state.chat_history:
                        c.execute("INSERT INTO chat_history VALUES (?, ?, ?)",
                                  (st.session_state.conversation_id, chat["role"], chat["content"]))
                    conn.commit()

                    # Display chat history
                    for index, chat in enumerate(st.session_state.chat_history):
                        with st.chat_message(chat["role"]):
                            if chat["role"] == "user":
                                st.markdown(chat["content"])
                            elif chat["role"] == "bot":
                                st.markdown(chat["content"])


                except Exception as e:
                    st.error(f"An error occurred: {e}")

            else:
                try:
                        GOOGLE_API_KEY = "Your_Gemini_Api_key"
                        genai.configure(api_key=GOOGLE_API_KEY)
                        model = genai.GenerativeModel(selected_model)
                        prompt = user_input
                        response = model.generate_content(prompt)
                        bot_response = response.candidates[0].content.parts[0].text

                        st.session_state.chat_history.append({"role": "user", "content": user_input})
                        st.session_state.chat_history.append({"role": "bot", "content": bot_response})

                        # Store chat in the database
                        for chat in st.session_state.chat_history:
                            c.execute("INSERT INTO chat_history VALUES (?, ?, ?)",
                                      (st.session_state.conversation_id, chat["role"], chat["content"]))
                        conn.commit()

                        for index, chat in enumerate(st.session_state.chat_history):
                            with st.chat_message(chat["role"]):
                                if chat["role"] == "user":
                                    st.markdown(chat["content"])
                                elif chat["role"] == "bot":
                                    st.markdown(chat["content"])

                except Exception as e:
                    st.error(f"An error occurred: {e}")





    except Exception as e:
        st.error(f"An error occurred: {e}")


def display_conversation(conversation_id):
    c.execute("SELECT * FROM chat_history WHERE conversation_id=?", (conversation_id,))
    chats = c.fetchall()
    st.markdown(f"### Conversation")
    for chat in chats:
        st.markdown(f"{chat[1]}")
        st.markdown(f"{chat[2]}")


if __name__ == "__main__":
    main()