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