Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from files import file_uploader, url_uploader
|
| 7 |
+
from question import chat_with_doc
|
| 8 |
+
from brain import brain
|
| 9 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 10 |
+
from langchain.vectorstores import SupabaseVectorStore
|
| 11 |
+
from supabase import Client, create_client
|
| 12 |
+
from explorer import view_document
|
| 13 |
+
from stats import get_usage_today
|
| 14 |
+
|
| 15 |
+
supabase_url = st.secrets.supabase_url
|
| 16 |
+
supabase_key = st.secrets.supabase_service_key
|
| 17 |
+
openai_api_key = st.secrets.openai_api_key
|
| 18 |
+
anthropic_api_key = st.secrets.anthropic_api_key
|
| 19 |
+
supabase: Client = create_client(supabase_url, supabase_key)
|
| 20 |
+
self_hosted = st.secrets.self_hosted
|
| 21 |
+
|
| 22 |
+
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
| 23 |
+
vector_store = SupabaseVectorStore(
|
| 24 |
+
supabase, embeddings, table_name="documents")
|
| 25 |
+
models = ["gpt-3.5-turbo", "gpt-4"]
|
| 26 |
+
if anthropic_api_key:
|
| 27 |
+
models += ["claude-v1", "claude-v1.3",
|
| 28 |
+
"claude-instant-v1-100k", "claude-instant-v1.1-100k"]
|
| 29 |
+
|
| 30 |
+
# Set the theme
|
| 31 |
+
st.set_page_config(
|
| 32 |
+
page_title="KPMG GPT",
|
| 33 |
+
layout="wide",
|
| 34 |
+
initial_sidebar_state="expanded",
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
st.title("KPMG GPT")
|
| 39 |
+
st.markdown("")
|
| 40 |
+
if self_hosted == "false":
|
| 41 |
+
st.markdown('**📢 Note: In the public demo, access to functionality is restricted. You can only use the GPT-3.5-turbo model and upload files up to 1Mb. To use more models and upload larger files, consider self-hosting Quivr.**')
|
| 42 |
+
|
| 43 |
+
st.markdown("---\n\n")
|
| 44 |
+
|
| 45 |
+
st.session_state["overused"] = False
|
| 46 |
+
if self_hosted == "false":
|
| 47 |
+
usage = get_usage_today(supabase)
|
| 48 |
+
if usage > st.secrets.usage_limit:
|
| 49 |
+
st.markdown(
|
| 50 |
+
f"<span style='color:red'>You have used {usage} tokens today, which is more than your daily limit of {st.secrets.usage_limit} tokens. Please come back later or consider self-hosting.</span>", unsafe_allow_html=True)
|
| 51 |
+
st.session_state["overused"] = True
|
| 52 |
+
else:
|
| 53 |
+
st.markdown(f"<span style='color:blue'>Usage today: {usage} tokens out of {st.secrets.usage_limit}</span>", unsafe_allow_html=True)
|
| 54 |
+
st.write("---")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# Initialize session state variables
|
| 60 |
+
if 'model' not in st.session_state:
|
| 61 |
+
st.session_state['model'] = "gpt-3.5-turbo"
|
| 62 |
+
if 'temperature' not in st.session_state:
|
| 63 |
+
st.session_state['temperature'] = 0.0
|
| 64 |
+
if 'chunk_size' not in st.session_state:
|
| 65 |
+
st.session_state['chunk_size'] = 500
|
| 66 |
+
if 'chunk_overlap' not in st.session_state:
|
| 67 |
+
st.session_state['chunk_overlap'] = 0
|
| 68 |
+
if 'max_tokens' not in st.session_state:
|
| 69 |
+
st.session_state['max_tokens'] = 256
|
| 70 |
+
|
| 71 |
+
# Create a radio button for user to choose between adding knowledge or asking a question
|
| 72 |
+
user_choice = st.radio(
|
| 73 |
+
"Choose an action", ('Add Knowledge', 'Chat with your Brain', 'Forget', "Explore"))
|
| 74 |
+
|
| 75 |
+
st.markdown("---\n\n")
|
| 76 |
+
|
| 77 |
+
if user_choice == 'Add Knowledge':
|
| 78 |
+
# Display chunk size and overlap selection only when adding knowledge
|
| 79 |
+
st.sidebar.title("Configuration")
|
| 80 |
+
st.sidebar.markdown(
|
| 81 |
+
"Choose your chunk size and overlap for adding knowledge.")
|
| 82 |
+
st.session_state['chunk_size'] = st.sidebar.slider(
|
| 83 |
+
"Select Chunk Size", 100, 2000, st.session_state['chunk_size'], 50)
|
| 84 |
+
st.session_state['chunk_overlap'] = st.sidebar.slider(
|
| 85 |
+
"Select Chunk Overlap", 0, 200, st.session_state['chunk_overlap'], 10)
|
| 86 |
+
|
| 87 |
+
# Create two columns for the file uploader and URL uploader
|
| 88 |
+
col1, col2 = st.columns(2)
|
| 89 |
+
|
| 90 |
+
with col1:
|
| 91 |
+
file_uploader(supabase, vector_store)
|
| 92 |
+
with col2:
|
| 93 |
+
url_uploader(supabase, vector_store)
|
| 94 |
+
elif user_choice == 'Chat with your Brain':
|
| 95 |
+
# Display model and temperature selection only when asking questions
|
| 96 |
+
st.sidebar.title("Configuration")
|
| 97 |
+
st.sidebar.markdown(
|
| 98 |
+
"Choose your model and temperature for asking questions.")
|
| 99 |
+
if self_hosted != "false":
|
| 100 |
+
st.session_state['model'] = st.sidebar.selectbox(
|
| 101 |
+
"Select Model", models, index=(models).index(st.session_state['model']))
|
| 102 |
+
else:
|
| 103 |
+
st.sidebar.write("**Model**: gpt-3.5-turbo")
|
| 104 |
+
st.sidebar.write("**Self Host to unlock more models such as claude-v1 and GPT4**")
|
| 105 |
+
st.session_state['model'] = "gpt-3.5-turbo"
|
| 106 |
+
st.session_state['temperature'] = st.sidebar.slider(
|
| 107 |
+
"Select Temperature", 0.0, 1.0, st.session_state['temperature'], 0.1)
|
| 108 |
+
if st.secrets.self_hosted != "false":
|
| 109 |
+
st.session_state['max_tokens'] = st.sidebar.slider(
|
| 110 |
+
"Select Max Tokens", 256, 2048, st.session_state['max_tokens'], 2048)
|
| 111 |
+
else:
|
| 112 |
+
st.session_state['max_tokens'] = 500
|
| 113 |
+
|
| 114 |
+
chat_with_doc(st.session_state['model'], vector_store, stats_db=supabase)
|
| 115 |
+
elif user_choice == 'Forget':
|
| 116 |
+
st.sidebar.title("Configuration")
|
| 117 |
+
|
| 118 |
+
brain(supabase)
|
| 119 |
+
elif user_choice == 'Explore':
|
| 120 |
+
st.sidebar.title("Configuration")
|
| 121 |
+
view_document(supabase)
|
| 122 |
+
|
| 123 |
+
st.markdown("---\n\n")
|