Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
@@ -1,166 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from g4f.client import Client
|
3 |
-
import sqlite3
|
4 |
-
import subprocess
|
5 |
-
import pyttsx3
|
6 |
-
import os
|
7 |
-
from cookies import *
|
8 |
-
from undetected_chromedriver import *
|
9 |
-
|
10 |
-
def local_css(file_name):
|
11 |
-
with open(file_name) as f:
|
12 |
-
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
13 |
-
|
14 |
-
local_css("style.css")
|
15 |
-
|
16 |
-
# Create a connection to the database
|
17 |
-
conn = sqlite3.connect('chat_history.db')
|
18 |
-
c = conn.cursor()
|
19 |
-
|
20 |
-
# Create table if not exists
|
21 |
-
try:
|
22 |
-
c.execute('''CREATE TABLE IF NOT EXISTS chat_history
|
23 |
-
(conversation_id INTEGER, role TEXT, content TEXT)''')
|
24 |
-
conn.commit()
|
25 |
-
except Exception as e:
|
26 |
-
st.error(f"An error occurred: {e}")
|
27 |
-
|
28 |
-
|
29 |
-
# def copy(text):
|
30 |
-
# """
|
31 |
-
# Copy text to clipboard on Windows.
|
32 |
-
|
33 |
-
# Parameters:
|
34 |
-
# text (str): The text to copy to the clipboard.
|
35 |
-
|
36 |
-
# Returns:
|
37 |
-
# bool: True if the text was successfully copied, False otherwise.
|
38 |
-
# """
|
39 |
-
# try:
|
40 |
-
# subprocess.run(['clip'], input=text.strip().encode('utf-16'), check=True)
|
41 |
-
# return True
|
42 |
-
# except subprocess.CalledProcessError:
|
43 |
-
# st.error("Error: Unable to copy text to clipboard on Windows.")
|
44 |
-
# return False
|
45 |
-
|
46 |
-
|
47 |
-
# Streamlit app
|
48 |
-
def main():
|
49 |
-
|
50 |
-
try:
|
51 |
-
if "chat_history" not in st.session_state:
|
52 |
-
st.session_state.chat_history = []
|
53 |
-
|
54 |
-
if "conversation_id" not in st.session_state:
|
55 |
-
st.session_state.conversation_id = 1
|
56 |
-
|
57 |
-
models = {
|
58 |
-
"🚀Airoboros 70B": "airoboros-70b",
|
59 |
-
"⚡GPT-4 Turbo": "gpt-4-turbo"
|
60 |
-
}
|
61 |
-
|
62 |
-
columns = st.columns(3) # Split the layout into three columns
|
63 |
-
with columns[0]:
|
64 |
-
st.header("DarkGPT")
|
65 |
-
|
66 |
-
with columns[2]:
|
67 |
-
selected_model_display_name = st.selectbox("Select Model", list(models.keys()), index=0)
|
68 |
-
|
69 |
-
with columns[1]:
|
70 |
-
selected_model = models[selected_model_display_name]
|
71 |
-
|
72 |
-
# Sidebar (left side) - New chat button
|
73 |
-
if st.sidebar.button("✨New Chat", key="new_chat_button"):
|
74 |
-
st.session_state.chat_history.clear()
|
75 |
-
st.session_state.conversation_id += 1
|
76 |
-
|
77 |
-
# Sidebar (left side) - Display saved chat
|
78 |
-
st.sidebar.write("Chat History")
|
79 |
-
c.execute("SELECT DISTINCT conversation_id FROM chat_history")
|
80 |
-
conversations = c.fetchall()
|
81 |
-
for conv_id in reversed(conversations):
|
82 |
-
c.execute("SELECT content FROM chat_history WHERE conversation_id=? AND role='bot' LIMIT 1",
|
83 |
-
(conv_id[0],))
|
84 |
-
first_bot_response = c.fetchone()
|
85 |
-
if first_bot_response:
|
86 |
-
if st.sidebar.button(" ".join(first_bot_response[0].split()[0:5])):
|
87 |
-
display_conversation(conv_id[0])
|
88 |
-
|
89 |
-
# Sidebar (left side) - Clear Chat History button
|
90 |
-
if st.sidebar.button("Clear Chat History ✖️"):
|
91 |
-
st.session_state.chat_history.clear()
|
92 |
-
c.execute("DELETE FROM chat_history")
|
93 |
-
conn.commit()
|
94 |
-
|
95 |
-
# Main content area (center)
|
96 |
-
st.markdown("---")
|
97 |
-
if selected_model == "gpt-4-turbo":
|
98 |
-
with st.chat_message("bot"):
|
99 |
-
st.markdown("Working with this model used the default model for generation.")
|
100 |
-
|
101 |
-
user_input = st.chat_input("Ask Anything ...")
|
102 |
-
|
103 |
-
# Listen for changes in user input and generate completion
|
104 |
-
if user_input:
|
105 |
-
client = Client()
|
106 |
-
response = client.chat.completions.create(
|
107 |
-
model=selected_model,
|
108 |
-
messages=[{"role": "user", "content": user_input}],
|
109 |
-
)
|
110 |
-
bot_response = response.choices[0].message.content
|
111 |
-
|
112 |
-
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
113 |
-
st.session_state.chat_history.append({"role": "bot", "content": bot_response})
|
114 |
-
|
115 |
-
# Store chat in the database
|
116 |
-
for chat in st.session_state.chat_history:
|
117 |
-
c.execute("INSERT INTO chat_history VALUES (?, ?, ?)",
|
118 |
-
(st.session_state.conversation_id, chat["role"], chat["content"]))
|
119 |
-
conn.commit()
|
120 |
-
|
121 |
-
# Display chat history
|
122 |
-
for index, chat in enumerate(st.session_state.chat_history):
|
123 |
-
with st.chat_message(chat["role"]):
|
124 |
-
if chat["role"] == "user":
|
125 |
-
st.markdown(chat["content"])
|
126 |
-
elif chat["role"] == "bot":
|
127 |
-
st.markdown(chat["content"])
|
128 |
-
col1 = st.columns(10)
|
129 |
-
# with col1[0]:
|
130 |
-
# copy_button = f"text_copy_{index}"
|
131 |
-
# if st.button('📋', key=copy_button):
|
132 |
-
# copy(chat["content"]) # Assuming chat["content"] contains the text to copy
|
133 |
-
|
134 |
-
# # Add a speak button in the second column
|
135 |
-
# with col1[1]:
|
136 |
-
# speak_button = f"text_regenerate_{index}"
|
137 |
-
# if st.button('🔊', key=speak_button):
|
138 |
-
# engine = pyttsx3.init()
|
139 |
-
# engine.say(chat["content"])
|
140 |
-
# engine.runAndWait()
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
except Exception as e:
|
146 |
-
st.error(f"An error occurred: {e}")
|
147 |
-
|
148 |
-
except TimeoutError:
|
149 |
-
st.error("Check Your Internet Connection:")
|
150 |
-
|
151 |
-
except ConnectionError:
|
152 |
-
st.error("Check Your Internet Connection:")
|
153 |
-
|
154 |
-
except RuntimeError:
|
155 |
-
st.error("Check Your Internet Connection:")
|
156 |
-
|
157 |
-
def display_conversation(conversation_id):
|
158 |
-
c.execute("SELECT * FROM chat_history WHERE conversation_id=?", (conversation_id,))
|
159 |
-
chats = c.fetchall()
|
160 |
-
st.markdown(f"### Conversation")
|
161 |
-
for chat in chats:
|
162 |
-
st.markdown(f"{chat[1]}")
|
163 |
-
st.markdown(f"{chat[2]}")
|
164 |
-
|
165 |
-
if __name__ == "__main__":
|
166 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|