|
|
import streamlit as st |
|
|
import os |
|
|
import openai |
|
|
import json |
|
|
import bcrypt |
|
|
import os |
|
|
from groq import Groq |
|
|
import re |
|
|
|
|
|
import warnings |
|
|
warnings.filterwarnings('ignore') |
|
|
|
|
|
client = Groq( |
|
|
api_key=os.environ.get("GROQ_API_KEY"), |
|
|
) |
|
|
|
|
|
|
|
|
hide_menu = """ |
|
|
<style> |
|
|
#MainMenu {visibiliyt: hidden;} |
|
|
footer {visibility: hidden;} |
|
|
header {visibility: hidden;} |
|
|
</style>""" |
|
|
|
|
|
st.markdown(hide_menu, unsafe_allow_html = True) |
|
|
|
|
|
|
|
|
def set_page_title(title): |
|
|
st.markdown(unsafe_allow_html=True, body=f""" |
|
|
<iframe height=0 srcdoc="<script> |
|
|
const title = window.parent.document.querySelector('title') \ |
|
|
|
|
|
const oldObserver = window.parent.titleObserver |
|
|
if (oldObserver) {{ |
|
|
oldObserver.disconnect() |
|
|
}} \ |
|
|
|
|
|
const newObserver = new MutationObserver(function(mutations) {{ |
|
|
const target = mutations[0].target |
|
|
if (target.text !== '{title}') {{ |
|
|
target.text = '{title}' |
|
|
}} |
|
|
}}) \ |
|
|
|
|
|
newObserver.observe(title, {{ childList: true }}) |
|
|
window.parent.titleObserver = newObserver \ |
|
|
|
|
|
title.text = '{title}' |
|
|
</script>" /> |
|
|
""") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'authenticated' not in st.session_state: |
|
|
st.session_state.authenticated = False |
|
|
|
|
|
|
|
|
config_file_path = "config.json" |
|
|
|
|
|
with open(config_file_path) as config_file: |
|
|
credentials = json.load(config_file) |
|
|
|
|
|
|
|
|
|
|
|
def check_credentials(password): |
|
|
|
|
|
|
|
|
|
|
|
if password=='123': |
|
|
return True |
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def login_page(): |
|
|
|
|
|
set_page_title("TGPT") |
|
|
|
|
|
st.markdown("<h1 style='text-align: center;'>Welcome to Team-GPT</h1>", unsafe_allow_html=True) |
|
|
st.markdown('\n') |
|
|
|
|
|
|
|
|
|
|
|
with st.form(key='login_form'): |
|
|
username = st.text_input("Username") |
|
|
password = st.text_input("Password", type="password") |
|
|
|
|
|
|
|
|
login_button = st.form_submit_button(label='Login', type = 'primary', use_container_width = True) |
|
|
|
|
|
st.session_state.username = username |
|
|
|
|
|
if login_button: |
|
|
if check_credentials(password): |
|
|
pattern = r"^[a-zA-Z]+$" |
|
|
if re.fullmatch(pattern, username): |
|
|
st.session_state['authenticated'] = True |
|
|
st.rerun() |
|
|
else: |
|
|
st.error("Username can't contain numbers or special characters!") |
|
|
else: |
|
|
st.error("The password is incorrect!") |
|
|
|
|
|
st.write('Password: 123') |
|
|
|
|
|
st.markdown('\n') |
|
|
st.markdown("<span style='color:#2180b9; text-align:left; font-weight:bold; font-size:20px;'>Instructions :</span>", unsafe_allow_html=True) |
|
|
st.markdown(""" |
|
|
<div style='text-align: left; padding-left: 20px;'> |
|
|
<ul> |
|
|
<li>Username can't have numbers or special characters.</li> |
|
|
<li>By default, every new conversation is Private.</li> |
|
|
<li>To make the conversation Public, select 'Shared' before submitting first prompt.</li> |
|
|
<li style='color:red;'>Once the first prompt is submitted, you can't change the chat visibility.</li> |
|
|
</ul> |
|
|
</div> |
|
|
""", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_new_chat(st): |
|
|
|
|
|
st.session_state.messages = [] |
|
|
|
|
|
|
|
|
|
|
|
def update_chat(st): |
|
|
|
|
|
|
|
|
file_var = open(st.session_state.file_name, encoding='utf-8') |
|
|
file_content = file_var.read() |
|
|
|
|
|
file_messages = file_content.split('-end-\n') |
|
|
file_messages = [file_messages[i].replace('-end-','') for i in range(len(file_messages))] |
|
|
|
|
|
|
|
|
session_messages = st.session_state.messages |
|
|
|
|
|
|
|
|
diff = len(file_messages) - len(session_messages) |
|
|
|
|
|
|
|
|
new_messages = [] |
|
|
|
|
|
if diff>0: |
|
|
for i in range(len(session_messages), len(file_messages)): |
|
|
session_messages.append(file_messages[i]) |
|
|
new_messages.append(file_messages[i]) |
|
|
|
|
|
|
|
|
for message in new_messages: |
|
|
print(message) |
|
|
with st.chat_message(message["added_by"]): |
|
|
st.markdown(message["content"]) |
|
|
|
|
|
|
|
|
def run_chatbot(st): |
|
|
|
|
|
set_page_title("TGPT") |
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
|
st.session_state.messages = [] |
|
|
|
|
|
|
|
|
if "chat_type" not in st.session_state: |
|
|
st.session_state.chat_type = 'New' |
|
|
|
|
|
|
|
|
if "replace_current_chat" not in st.session_state: |
|
|
st.session_state.replace_current_chat = False |
|
|
|
|
|
|
|
|
if "file_name" not in st.session_state: |
|
|
st.session_state.file_name = '' |
|
|
|
|
|
|
|
|
if "visibility" not in st.session_state: |
|
|
st.session_state.visibility = 'Shared👥' |
|
|
|
|
|
|
|
|
|
|
|
with st.sidebar: |
|
|
if st.button("Logout", type = "secondary", key = 'logout'): |
|
|
st.session_state.clear() |
|
|
st.rerun() |
|
|
|
|
|
if st.button("New Chat ➕", type = "primary", key = 'new_chat'): |
|
|
st.session_state.messages = [] |
|
|
st.session_state.file_name = '' |
|
|
st.session_state.chat_type = 'New' |
|
|
st.session_state.replace_current_chat = False |
|
|
st.session_state.visibility = 'Private🔒' |
|
|
|
|
|
|
|
|
|
|
|
st.header('Chat History') |
|
|
|
|
|
history_files = os.listdir('chat_history/') |
|
|
|
|
|
|
|
|
complete_files = sorted(history_files, key=lambda x: os.path.getctime(os.path.join('chat_history/', x)), |
|
|
reverse=True) |
|
|
|
|
|
|
|
|
usersnames = [f.split('_')[0] for f in complete_files] |
|
|
visibility_status = [f.split('_')[1] for f in complete_files] |
|
|
files = [f.split('_')[2] for f in complete_files] |
|
|
|
|
|
|
|
|
|
|
|
for i in range(len(complete_files)): |
|
|
|
|
|
add_chat = False |
|
|
|
|
|
|
|
|
if (visibility_status[i]=='Private🔒') & (usersnames[i]==st.session_state.username): |
|
|
add_chat = True |
|
|
elif visibility_status[i]=='Shared👥': |
|
|
add_chat = True |
|
|
|
|
|
if add_chat: |
|
|
|
|
|
unique_key = f"{i}_{files[i][:-4]}" |
|
|
|
|
|
if st.button(files[i][:-4], key=unique_key): |
|
|
|
|
|
|
|
|
st.session_state.replace_current_chat = True |
|
|
st.session_state.chat_type = 'Old' |
|
|
st.session_state.file_name = 'chat_history/' + complete_files[i] |
|
|
|
|
|
else: |
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if st.session_state.replace_current_chat: |
|
|
|
|
|
st.session_state.messages = [] |
|
|
st.session_state.visibility = st.session_state.file_name.split('_')[2] |
|
|
|
|
|
prev_file = open(st.session_state.file_name, encoding='utf-8') |
|
|
file_content = prev_file.read() |
|
|
|
|
|
msgs = file_content.split('-end-\n') |
|
|
|
|
|
for i in range(len(msgs)): |
|
|
|
|
|
msgs[i] = msgs[i].replace('-end-','') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg_content = msgs[i].split(':',2) |
|
|
user_type = msg_content[0] |
|
|
from_user = msg_content[1] |
|
|
msg_text = msg_content[2] |
|
|
|
|
|
st.session_state.messages.append({"role": user_type, "added_by" : from_user, "content": msg_text}) |
|
|
|
|
|
|
|
|
|
|
|
header1, header2 = st.columns(2) |
|
|
|
|
|
with header1: |
|
|
|
|
|
st.markdown("<h4 style='font-family: Arial;'>Team-GPT v0.2</h4>", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
|
|
|
with header2: |
|
|
|
|
|
|
|
|
st.markdown( |
|
|
""" |
|
|
<style> |
|
|
.stRadio > div { |
|
|
flex-direction: row; |
|
|
justify-content: flex-end; |
|
|
} |
|
|
.st-Radio input[type="radio"] + label::before { |
|
|
width: 8px; |
|
|
height: 8px; |
|
|
margin-right: 4px; |
|
|
} |
|
|
</style> |
|
|
""", |
|
|
unsafe_allow_html=True |
|
|
) |
|
|
|
|
|
|
|
|
if st.session_state.chat_type=='Old': |
|
|
option = st.radio(label = '', options = (st.session_state.visibility,)) |
|
|
else: |
|
|
option = st.radio(label = '', options = ('Private🔒', 'Shared👥')) |
|
|
st.session_state.visibility = option |
|
|
|
|
|
|
|
|
|
|
|
for message in st.session_state.messages: |
|
|
with st.chat_message(message["added_by"]): |
|
|
st.markdown(message["content"]) |
|
|
|
|
|
|
|
|
|
|
|
prompt = st.chat_input("Type here...") |
|
|
|
|
|
if prompt: |
|
|
|
|
|
if st.session_state.chat_type=='New': |
|
|
|
|
|
file_name = "".join(c if c.isalnum() or c in {'_', ' ', '.'} else ' ' for c in prompt) |
|
|
file_name = " ".join(file_name.split()[:5]) |
|
|
file_name = file_name.strip() |
|
|
|
|
|
|
|
|
file_name = "chat_history/" + st.session_state.username + '_' + st.session_state.visibility + '_' + file_name + ".txt" |
|
|
|
|
|
st.session_state.file_name = file_name |
|
|
|
|
|
|
|
|
|
|
|
with st.chat_message(st.session_state.username): |
|
|
st.markdown(prompt) |
|
|
|
|
|
|
|
|
st.session_state.messages.append({"role":"user", "added_by":st.session_state.username, "content":prompt}) |
|
|
|
|
|
|
|
|
|
|
|
file = open(st.session_state.file_name, "a", encoding='utf-8') |
|
|
|
|
|
if st.session_state.chat_type=='New': |
|
|
file.write('user:' + st.session_state.username + ':' + prompt + "-end-") |
|
|
st.session_state.chat_type = 'Old' |
|
|
|
|
|
elif st.session_state.chat_type=='Old': |
|
|
file.write("\n" + 'user:' + st.session_state.username + ':' + prompt + "-end-") |
|
|
|
|
|
|
|
|
|
|
|
with st.chat_message("assistant"): |
|
|
|
|
|
message_placeholder = st.empty() |
|
|
full_response = "" |
|
|
|
|
|
|
|
|
response = client.chat.completions.create( |
|
|
messages = [{"role":m["role"], "content":m["content"]} for m in st.session_state.messages[-10:]], |
|
|
model= 'llama-3.1-8b-instant', |
|
|
temperature = 1, |
|
|
max_tokens = 3000 |
|
|
) |
|
|
|
|
|
full_response = response.choices[0].message.content |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
message_placeholder.markdown(full_response) |
|
|
file.write("\n" + 'assistant:assistant:' + full_response + "-end-") |
|
|
|
|
|
st.session_state.messages.append({"role":"assistant", "added_by":"assistant", "content":full_response}) |
|
|
print(st.session_state.messages,'\n') |
|
|
update_chat(st) |
|
|
|
|
|
if not st.session_state['authenticated']: |
|
|
login_page() |
|
|
|
|
|
else: |
|
|
run_chatbot(st) |