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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from g4f.client import Client
|
| 3 |
+
import sqlite3
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
# import pyttsx3
|
| 6 |
+
import pyperclip
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
st.set_page_config(page_title="DarkGPT",
|
| 10 |
+
page_icon="🤖",
|
| 11 |
+
layout="wide",
|
| 12 |
+
initial_sidebar_state="expanded"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def local_css(file_name):
|
| 17 |
+
with open(file_name) as f:
|
| 18 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
local_css("style.css")
|
| 22 |
+
|
| 23 |
+
# Create a connection to the database
|
| 24 |
+
conn = sqlite3.connect('chat_history.db')
|
| 25 |
+
c = conn.cursor()
|
| 26 |
+
|
| 27 |
+
# Create table if not exists
|
| 28 |
+
try:
|
| 29 |
+
c.execute('''CREATE TABLE IF NOT EXISTS chat_history
|
| 30 |
+
(conversation_id INTEGER, role TEXT, content TEXT)''')
|
| 31 |
+
conn.commit()
|
| 32 |
+
except Exception as e:
|
| 33 |
+
st.error(f"An error occurred: {e}")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Streamlit app
|
| 38 |
+
def main():
|
| 39 |
+
try:
|
| 40 |
+
if "chat_history" not in st.session_state:
|
| 41 |
+
st.session_state.chat_history = []
|
| 42 |
+
|
| 43 |
+
if "conversation_id" not in st.session_state:
|
| 44 |
+
st.session_state.conversation_id = 1
|
| 45 |
+
|
| 46 |
+
models = {
|
| 47 |
+
"🚀 Airoboros 70B": "airoboros-70b",
|
| 48 |
+
"👑 Gemini 1.0": "gemini-1.0-pro",
|
| 49 |
+
"🧨 Gemini 1.0 Pro ": "gemini-1.0-pro-001",
|
| 50 |
+
"⚡ Gemini 1.0 pro latest": "gemini-1.0-pro-latest",
|
| 51 |
+
"🔮 Gemini Pro": "gemini-pro",
|
| 52 |
+
"🎉 Gemini pro vision": "gemini-pro-vision"
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
columns = st.columns(3) # Split the layout into three columns
|
| 56 |
+
with columns[0]:
|
| 57 |
+
st.header("DarkGPT")
|
| 58 |
+
|
| 59 |
+
with columns[2]:
|
| 60 |
+
|
| 61 |
+
selected_model_display_name = st.selectbox("Select Model", list(models.keys()), index=0)
|
| 62 |
+
selected_model = models[selected_model_display_name]
|
| 63 |
+
|
| 64 |
+
with columns[1]:
|
| 65 |
+
pass
|
| 66 |
+
# if st.button("summarize"):
|
| 67 |
+
# st.switch_page('pages/summarize.py')
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Sidebar (left side) - New chat button
|
| 71 |
+
if st.sidebar.button("✨ New Chat", key="new_chat_button"):
|
| 72 |
+
st.session_state.chat_history.clear()
|
| 73 |
+
st.session_state.conversation_id += 1
|
| 74 |
+
|
| 75 |
+
# Sidebar (left side) - Display saved chat
|
| 76 |
+
st.sidebar.write("Chat History")
|
| 77 |
+
c.execute("SELECT DISTINCT conversation_id FROM chat_history")
|
| 78 |
+
conversations = c.fetchall()
|
| 79 |
+
for conv_id in reversed(conversations):
|
| 80 |
+
c.execute("SELECT content FROM chat_history WHERE conversation_id=? AND role='bot' LIMIT 1",
|
| 81 |
+
(conv_id[0],))
|
| 82 |
+
first_bot_response = c.fetchone()
|
| 83 |
+
if first_bot_response:
|
| 84 |
+
if st.sidebar.button(" ".join(first_bot_response[0].split()[0:5])):
|
| 85 |
+
display_conversation(conv_id[0])
|
| 86 |
+
|
| 87 |
+
# Sidebar (left side) - Clear Chat History button
|
| 88 |
+
if st.sidebar.button("Clear Chat History ✖️"):
|
| 89 |
+
st.session_state.chat_history.clear()
|
| 90 |
+
c.execute("DELETE FROM chat_history")
|
| 91 |
+
conn.commit()
|
| 92 |
+
|
| 93 |
+
# Main content area (center)
|
| 94 |
+
st.markdown("---")
|
| 95 |
+
|
| 96 |
+
user_input = st.chat_input("Ask Anything ...")
|
| 97 |
+
|
| 98 |
+
if user_input:
|
| 99 |
+
if selected_model == "airoboros-70b":
|
| 100 |
+
try:
|
| 101 |
+
client = Client()
|
| 102 |
+
response = client.chat.completions.create(
|
| 103 |
+
model=models[selected_model_display_name],
|
| 104 |
+
messages=[{"role": "user", "content": user_input}],
|
| 105 |
+
)
|
| 106 |
+
bot_response = response.choices[0].message.content
|
| 107 |
+
|
| 108 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
| 109 |
+
st.session_state.chat_history.append({"role": "bot", "content": bot_response})
|
| 110 |
+
|
| 111 |
+
# Store chat in the database
|
| 112 |
+
for chat in st.session_state.chat_history:
|
| 113 |
+
c.execute("INSERT INTO chat_history VALUES (?, ?, ?)",
|
| 114 |
+
(st.session_state.conversation_id, chat["role"], chat["content"]))
|
| 115 |
+
conn.commit()
|
| 116 |
+
|
| 117 |
+
# Display chat history
|
| 118 |
+
for index, chat in enumerate(st.session_state.chat_history):
|
| 119 |
+
with st.chat_message(chat["role"]):
|
| 120 |
+
if chat["role"] == "user":
|
| 121 |
+
st.markdown(chat["content"])
|
| 122 |
+
elif chat["role"] == "bot":
|
| 123 |
+
st.markdown(chat["content"])
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
except Exception as e:
|
| 127 |
+
st.error(f"An error occurred: {e}")
|
| 128 |
+
|
| 129 |
+
else:
|
| 130 |
+
try:
|
| 131 |
+
# GEMINI Replace with your Gemini Api key
|
| 132 |
+
GOOGLE_API_KEY = "GEMINI"
|
| 133 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 134 |
+
model = genai.GenerativeModel(selected_model)
|
| 135 |
+
prompt = user_input
|
| 136 |
+
response = model.generate_content(prompt)
|
| 137 |
+
bot_response = response.candidates[0].content.parts[0].text
|
| 138 |
+
|
| 139 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
| 140 |
+
st.session_state.chat_history.append({"role": "bot", "content": bot_response})
|
| 141 |
+
|
| 142 |
+
# Store chat in the database
|
| 143 |
+
for chat in st.session_state.chat_history:
|
| 144 |
+
c.execute("INSERT INTO chat_history VALUES (?, ?, ?)",
|
| 145 |
+
(st.session_state.conversation_id, chat["role"], chat["content"]))
|
| 146 |
+
conn.commit()
|
| 147 |
+
|
| 148 |
+
for index, chat in enumerate(st.session_state.chat_history):
|
| 149 |
+
with st.chat_message(chat["role"]):
|
| 150 |
+
if chat["role"] == "user":
|
| 151 |
+
st.markdown(chat["content"])
|
| 152 |
+
elif chat["role"] == "bot":
|
| 153 |
+
st.markdown(chat["content"])
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
st.error(f"An error occurred: {e}")
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
except Exception as e:
|
| 163 |
+
st.error(f"An error occurred: {e}")
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def display_conversation(conversation_id):
|
| 167 |
+
c.execute("SELECT * FROM chat_history WHERE conversation_id=?", (conversation_id,))
|
| 168 |
+
chats = c.fetchall()
|
| 169 |
+
st.markdown(f"### Conversation")
|
| 170 |
+
for chat in chats:
|
| 171 |
+
st.markdown(f"{chat[1]}")
|
| 172 |
+
st.markdown(f"{chat[2]}")
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
if __name__ == "__main__":
|
| 176 |
+
main()
|
requirements.txt
ADDED
|
Binary file (452 Bytes). View file
|
|
|