codewithdark commited on
Commit
5759094
·
verified ·
1 Parent(s): 37c94b6

Upload app.py

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