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