Asankhaya Sharma commited on
Commit
8a70a7b
1 Parent(s): 2329c69

add user authentication

Browse files
Files changed (5) hide show
  1. brain.py +1 -1
  2. explorer.py +1 -1
  3. loaders/common.py +6 -2
  4. main.py +74 -66
  5. requirements.txt +2 -1
brain.py CHANGED
@@ -4,7 +4,7 @@ import streamlit as st
4
 
5
  def brain(supabase):
6
  ## List all documents
7
- response = supabase.table("documents").select("name:metadata->>file_name, size:metadata->>file_size", count="exact").execute()
8
 
9
  documents = response.data # Access the data from the response
10
 
 
4
 
5
  def brain(supabase):
6
  ## List all documents
7
+ response = supabase.table("documents").select("name:metadata->>file_name, size:metadata->>file_size", count="exact").filter('metadata->>user', 'eq', st.session_state["username"]).execute()
8
 
9
  documents = response.data # Access the data from the response
10
 
explorer.py CHANGED
@@ -3,7 +3,7 @@ import streamlit as st
3
 
4
  def view_document(supabase):
5
  # Get the document from the database
6
- response = supabase.table("documents").select("content").execute()
7
  # st.write("**This feature is in active development**")
8
  # Display a list of elements from the documents
9
  # If the user clicks on an element, display the content of the document
 
3
 
4
  def view_document(supabase):
5
  # Get the document from the database
6
+ response = supabase.table("documents").select("content").filter('metadata->>user', 'eq', st.session_state["username"]).execute()
7
  # st.write("**This feature is in active development**")
8
  # Display a list of elements from the documents
9
  # If the user clicks on an element, display the content of the document
loaders/common.py CHANGED
@@ -35,8 +35,12 @@ def process_file(vector_store, file, loader_class, file_suffix, stats_db=None):
35
  documents = text_splitter.split_documents(documents)
36
 
37
  # Add the document sha1 as metadata to each document
38
- docs_with_metadata = [Document(page_content=doc.page_content, metadata={"file_sha1": file_sha1,"file_size":file_size ,"file_name": file_name, "chunk_size": chunk_size, "chunk_overlap": chunk_overlap, "date": dateshort}) for doc in documents]
 
 
 
39
 
40
  vector_store.add_documents(docs_with_metadata)
41
  if stats_db:
42
- add_usage(stats_db, "embedding", "file", metadata={"file_name": file_name,"file_type": file_suffix, "chunk_size": chunk_size, "chunk_overlap": chunk_overlap})
 
 
35
  documents = text_splitter.split_documents(documents)
36
 
37
  # Add the document sha1 as metadata to each document
38
+ docs_with_metadata = [Document(page_content=doc.page_content, metadata={"file_sha1": file_sha1,"file_size":file_size ,"file_name": file_name,
39
+ "chunk_size": chunk_size, "chunk_overlap": chunk_overlap, "date": dateshort,
40
+ "user" : st.session_state["username"]})
41
+ for doc in documents]
42
 
43
  vector_store.add_documents(docs_with_metadata)
44
  if stats_db:
45
+ add_usage(stats_db, "embedding", "file", metadata={"file_name": file_name,"file_type": file_suffix,
46
+ "chunk_size": chunk_size, "chunk_overlap": chunk_overlap})
main.py CHANGED
@@ -11,9 +11,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
  hf_api_key = st.secrets.hf_api_key
@@ -64,72 +65,79 @@ if self_hosted == "false":
64
  else:
65
  st.markdown(f"<span style='color:blue'>Usage today: {usage} tokens out of {st.secrets.usage_limit}</span>", unsafe_allow_html=True)
66
  st.write("---")
67
-
68
 
 
69
 
70
-
71
- # Initialize session state variables
72
- if 'model' not in st.session_state:
73
- st.session_state['model'] = "llama-2"
74
- if 'temperature' not in st.session_state:
75
- st.session_state['temperature'] = 0.1
76
- if 'chunk_size' not in st.session_state:
77
- st.session_state['chunk_size'] = 500
78
- if 'chunk_overlap' not in st.session_state:
79
- st.session_state['chunk_overlap'] = 0
80
- if 'max_tokens' not in st.session_state:
81
- st.session_state['max_tokens'] = 500
82
-
83
- # Create a radio button for user to choose between adding knowledge or asking a question
84
- user_choice = st.radio(
85
- "Choose an action", ('Add Knowledge', 'Chat with your Brain', 'Forget', "Explore"))
86
-
87
- st.markdown("---\n\n")
88
-
89
- if user_choice == 'Add Knowledge':
90
- # Display chunk size and overlap selection only when adding knowledge
91
- st.sidebar.title("Configuration")
92
- st.sidebar.markdown(
93
- "Choose your chunk size and overlap for adding knowledge.")
94
- st.session_state['chunk_size'] = st.sidebar.slider(
95
- "Select Chunk Size", 100, 1000, st.session_state['chunk_size'], 50)
96
- st.session_state['chunk_overlap'] = st.sidebar.slider(
97
- "Select Chunk Overlap", 0, 100, st.session_state['chunk_overlap'], 10)
98
-
99
- # Create two columns for the file uploader and URL uploader
100
- col1, col2 = st.columns(2)
101
-
102
- with col1:
103
- file_uploader(supabase, vector_store)
104
- with col2:
105
- url_uploader(supabase, vector_store)
106
- elif user_choice == 'Chat with your Brain':
107
- # Display model and temperature selection only when asking questions
108
- st.sidebar.title("Configuration")
109
- st.sidebar.markdown(
110
- "Choose your model and temperature for asking questions.")
111
- if self_hosted != "false":
112
- st.session_state['model'] = st.sidebar.selectbox(
113
- "Select Model", models, index=(models).index(st.session_state['model']))
114
- else:
115
- st.sidebar.write("**Model**: gpt-3.5-turbo")
116
- st.sidebar.write("**Self Host to unlock more models such as claude-v1 and GPT4**")
117
- st.session_state['model'] = "gpt-3.5-turbo"
118
- st.session_state['temperature'] = st.sidebar.slider(
119
- "Select Temperature", 0.1, 1.0, st.session_state['temperature'], 0.1)
120
- if st.secrets.self_hosted != "false":
121
- st.session_state['max_tokens'] = st.sidebar.slider(
122
- "Select Max Tokens", 500, 4000, st.session_state['max_tokens'], 500)
123
  else:
 
 
 
 
 
 
 
 
 
 
 
 
124
  st.session_state['max_tokens'] = 500
125
-
126
- chat_with_doc(st.session_state['model'], vector_store, stats_db=supabase)
127
- elif user_choice == 'Forget':
128
- st.sidebar.title("Configuration")
129
-
130
- brain(supabase)
131
- elif user_choice == 'Explore':
132
- st.sidebar.title("Configuration")
133
- view_document(supabase)
134
 
135
- st.markdown("---\n\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  from supabase import Client, create_client
12
  from explorer import view_document
13
  from stats import get_usage_today
14
+ from st_login_form import login_form
15
 
16
+ supabase_url = st.secrets.SUPABASE_URL
17
+ supabase_key = st.secrets.SUPABASE_KEY
18
  openai_api_key = st.secrets.openai_api_key
19
  anthropic_api_key = st.secrets.anthropic_api_key
20
  hf_api_key = st.secrets.hf_api_key
 
65
  else:
66
  st.markdown(f"<span style='color:blue'>Usage today: {usage} tokens out of {st.secrets.usage_limit}</span>", unsafe_allow_html=True)
67
  st.write("---")
 
68
 
69
+ client = login_form()
70
 
71
+ if st.session_state["authenticated"]:
72
+ if st.session_state["username"]:
73
+ st.success(f"Welcome {st.session_state['username']}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  else:
75
+ st.success("Welcome guest")
76
+
77
+ # Initialize session state variables
78
+ if 'model' not in st.session_state:
79
+ st.session_state['model'] = "llama-2"
80
+ if 'temperature' not in st.session_state:
81
+ st.session_state['temperature'] = 0.1
82
+ if 'chunk_size' not in st.session_state:
83
+ st.session_state['chunk_size'] = 500
84
+ if 'chunk_overlap' not in st.session_state:
85
+ st.session_state['chunk_overlap'] = 0
86
+ if 'max_tokens' not in st.session_state:
87
  st.session_state['max_tokens'] = 500
 
 
 
 
 
 
 
 
 
88
 
89
+ # Create a radio button for user to choose between adding knowledge or asking a question
90
+ user_choice = st.radio(
91
+ "Choose an action", ('Add Knowledge', 'Chat with your Brain', 'Forget', "Explore"))
92
+
93
+ st.markdown("---\n\n")
94
+
95
+ if user_choice == 'Add Knowledge':
96
+ # Display chunk size and overlap selection only when adding knowledge
97
+ st.sidebar.title("Configuration")
98
+ st.sidebar.markdown(
99
+ "Choose your chunk size and overlap for adding knowledge.")
100
+ st.session_state['chunk_size'] = st.sidebar.slider(
101
+ "Select Chunk Size", 100, 1000, st.session_state['chunk_size'], 50)
102
+ st.session_state['chunk_overlap'] = st.sidebar.slider(
103
+ "Select Chunk Overlap", 0, 100, st.session_state['chunk_overlap'], 10)
104
+
105
+ # Create two columns for the file uploader and URL uploader
106
+ col1, col2 = st.columns(2)
107
+
108
+ with col1:
109
+ file_uploader(supabase, vector_store)
110
+ with col2:
111
+ url_uploader(supabase, vector_store)
112
+ elif user_choice == 'Chat with your Brain':
113
+ # Display model and temperature selection only when asking questions
114
+ st.sidebar.title("Configuration")
115
+ st.sidebar.markdown(
116
+ "Choose your model and temperature for asking questions.")
117
+ if self_hosted != "false":
118
+ st.session_state['model'] = st.sidebar.selectbox(
119
+ "Select Model", models, index=(models).index(st.session_state['model']))
120
+ else:
121
+ st.sidebar.write("**Model**: gpt-3.5-turbo")
122
+ st.sidebar.write("**Self Host to unlock more models such as claude-v1 and GPT4**")
123
+ st.session_state['model'] = "gpt-3.5-turbo"
124
+ st.session_state['temperature'] = st.sidebar.slider(
125
+ "Select Temperature", 0.1, 1.0, st.session_state['temperature'], 0.1)
126
+ if st.secrets.self_hosted != "false":
127
+ st.session_state['max_tokens'] = st.sidebar.slider(
128
+ "Select Max Tokens", 500, 4000, st.session_state['max_tokens'], 500)
129
+ else:
130
+ st.session_state['max_tokens'] = 500
131
+
132
+ chat_with_doc(st.session_state['model'], vector_store, stats_db=supabase)
133
+ elif user_choice == 'Forget':
134
+ st.sidebar.title("Configuration")
135
+
136
+ brain(supabase)
137
+ elif user_choice == 'Explore':
138
+ st.sidebar.title("Configuration")
139
+ view_document(supabase)
140
+
141
+ st.markdown("---\n\n")
142
+ else:
143
+ st.error("Not authenticated")
requirements.txt CHANGED
@@ -12,4 +12,5 @@ anthropic==0.2.8
12
  fastapi==0.95.2
13
  python-multipart==0.0.6
14
  uvicorn==0.22.0
15
- docx2txt
 
 
12
  fastapi==0.95.2
13
  python-multipart==0.0.6
14
  uvicorn==0.22.0
15
+ docx2txt
16
+ st-login-form