Nechba commited on
Commit
49ffc7b
1 Parent(s): 5eadc60
app.py CHANGED
@@ -1,10 +1,32 @@
1
  from utlis.helper import *
 
 
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  initialize_session_state()
4
 
5
  with st.sidebar:
6
  st.image("logo.png", width=170)
7
- st.title("AGDC")
8
  # Get List of models
9
  llms = ['gpt-3.5-turbo', 'gemini']
10
  st.session_state.llm = st.selectbox("Choose LLM",llms)
@@ -178,11 +200,24 @@ with st.container():
178
  if st.button('Process Text'):
179
  add_text_document(st.session_state.token, st.session_state.service)
180
  if st.session_state.genre=="Select document" and st.session_state.service_slected_to_chat:
181
- schema = display_and_validate_schema()
182
- comments = None
 
 
 
 
 
 
 
183
  if schema and st.checkbox("Add comments") :
184
- keys = get_all_keys(schema)
185
- comments = handle_comments(keys)
 
 
 
 
 
 
186
  if schema and st.button('Process') :
187
  if st.session_state.doument_slected_to_chat.split("_")[-1]=="pdf":
188
  data = {"token": st.session_state.token,
 
1
  from utlis.helper import *
2
+ import sqlite3
3
+ import hashlib
4
 
5
+ def create_document_id(token, service_selected, document_selected):
6
+ # Create a unique document ID from token, service, and document name
7
+ unique_id = f"{token}{service_selected}{document_selected}"
8
+ # Hash the unique ID using SHA-256
9
+ hashed_id = hashlib.sha256(unique_id.encode()).hexdigest()
10
+ return hashed_id
11
+
12
+ def create_database():
13
+ conn = sqlite3.connect('document_cache.db')
14
+ c = conn.cursor()
15
+ # Create table for schemas
16
+ c.execute('''CREATE TABLE IF NOT EXISTS schemas
17
+ (document_id TEXT PRIMARY KEY, schema TEXT)''')
18
+ # Create table for comments
19
+ c.execute('''CREATE TABLE IF NOT EXISTS comments
20
+ (document_id TEXT PRIMARY KEY, comments TEXT)''')
21
+ conn.commit()
22
+ conn.close()
23
+
24
+ create_database()
25
  initialize_session_state()
26
 
27
  with st.sidebar:
28
  st.image("logo.png", width=170)
29
+ st.title("AGDS")
30
  # Get List of models
31
  llms = ['gpt-3.5-turbo', 'gemini']
32
  st.session_state.llm = st.selectbox("Choose LLM",llms)
 
200
  if st.button('Process Text'):
201
  add_text_document(st.session_state.token, st.session_state.service)
202
  if st.session_state.genre=="Select document" and st.session_state.service_slected_to_chat:
203
+ #print(st.session_state.document_selected_to_chat)
204
+ #document_id = st.session_state.token+st.session_state.service_slected_to_chat+st.session_state.doument_slected_to_chat
205
+ document_id = create_document_id(st.session_state.token, st.session_state.service_slected_to_chat, st.session_state.doument_slected_to_chat)
206
+ print(document_id)
207
+ schema = get_schema(document_id)
208
+ schema = display_and_validate_schema(schema)
209
+ if schema:
210
+ save_schema(document_id, schema)
211
+
212
  if schema and st.checkbox("Add comments") :
213
+ comments = get_comments(document_id)
214
+ if not comments:
215
+ comments = {}
216
+ keys = get_all_keys(schema)
217
+ else:
218
+ keys = list(comments.keys())
219
+ comments = handle_comments(comments, keys)
220
+ save_comments(document_id, comments)
221
  if schema and st.button('Process') :
222
  if st.session_state.doument_slected_to_chat.split("_")[-1]=="pdf":
223
  data = {"token": st.session_state.token,
document_cache.db ADDED
Binary file (20.5 kB). View file
 
utlis/__pycache__/helper.cpython-39.pyc CHANGED
Binary files a/utlis/__pycache__/helper.cpython-39.pyc and b/utlis/__pycache__/helper.cpython-39.pyc differ
 
utlis/helper.py CHANGED
@@ -15,6 +15,7 @@ from PIL import Image
15
  import google.generativeai as genai
16
  genai.configure(api_key="AIzaSyAhz9UBzkEIYI886zZRm40qqB1Kd_9Y4-0")
17
  import base64
 
18
  def initialize_session_state():
19
  if "token" not in st.session_state:
20
  st.session_state["token"] ="abcd"
@@ -194,8 +195,11 @@ def get_all_keys(d):
194
  get_keys(item)
195
  get_keys(d)
196
  return list(all_keys)
197
- def display_and_validate_schema():
198
- schema_str = json.dumps(DEFAULT_SCHEMA, indent=2)
 
 
 
199
  schema_input = st.text_area("JSON Schema", schema_str, height=300)
200
  try:
201
  schema = json.loads(schema_input)
@@ -204,8 +208,7 @@ def display_and_validate_schema():
204
  except json.JSONDecodeError:
205
  st.error("The JSON schema is invalid. Please correct it and try again.")
206
  return None
207
- def handle_comments(keys):
208
- comments = {}
209
  items_per_page = 6 # Adjust this number based on your preference
210
  total_pages = (len(keys) + items_per_page - 1) // items_per_page
211
 
@@ -217,7 +220,37 @@ def handle_comments(keys):
217
 
218
  for key in keys[start_idx:end_idx]:
219
  with st.expander(f"{key}"):
220
- comments[key] = st.text_input(f"{key}")
221
  # if st.button("Submit"):
222
  # st.session_state.flag=False
223
- return comments
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  import google.generativeai as genai
16
  genai.configure(api_key="AIzaSyAhz9UBzkEIYI886zZRm40qqB1Kd_9Y4-0")
17
  import base64
18
+ import sqlite3
19
  def initialize_session_state():
20
  if "token" not in st.session_state:
21
  st.session_state["token"] ="abcd"
 
195
  get_keys(item)
196
  get_keys(d)
197
  return list(all_keys)
198
+ def display_and_validate_schema(schema):
199
+ if schema:
200
+ schema_str = json.dumps(schema, indent=2)
201
+ else:
202
+ schema_str = json.dumps(DEFAULT_SCHEMA, indent=2)
203
  schema_input = st.text_area("JSON Schema", schema_str, height=300)
204
  try:
205
  schema = json.loads(schema_input)
 
208
  except json.JSONDecodeError:
209
  st.error("The JSON schema is invalid. Please correct it and try again.")
210
  return None
211
+ def handle_comments(comments,keys):
 
212
  items_per_page = 6 # Adjust this number based on your preference
213
  total_pages = (len(keys) + items_per_page - 1) // items_per_page
214
 
 
220
 
221
  for key in keys[start_idx:end_idx]:
222
  with st.expander(f"{key}"):
223
+ comments[key] = st.text_input(f"{key}", value=comments.get(key,""))
224
  # if st.button("Submit"):
225
  # st.session_state.flag=False
226
+ return comments
227
+
228
+ def save_schema(document_id, schema):
229
+ conn = sqlite3.connect('document_cache.db')
230
+ c = conn.cursor()
231
+ c.execute('REPLACE INTO schemas (document_id, schema) VALUES (?, ?)', (document_id, json.dumps(schema)))
232
+ conn.commit()
233
+ conn.close()
234
+
235
+ def get_schema(document_id):
236
+ conn = sqlite3.connect('document_cache.db')
237
+ c = conn.cursor()
238
+ c.execute('SELECT schema FROM schemas WHERE document_id = ?', (document_id,))
239
+ result = c.fetchone()
240
+ conn.close()
241
+ return json.loads(result[0]) if result else None
242
+
243
+ def save_comments(document_id, comments):
244
+ conn = sqlite3.connect('document_cache.db')
245
+ c = conn.cursor()
246
+ c.execute('REPLACE INTO comments (document_id, comments) VALUES (?, ?)', (document_id, json.dumps(comments)))
247
+ conn.commit()
248
+ conn.close()
249
+
250
+ def get_comments(document_id):
251
+ conn = sqlite3.connect('document_cache.db')
252
+ c = conn.cursor()
253
+ c.execute('SELECT comments FROM comments WHERE document_id = ?', (document_id,))
254
+ result = c.fetchone()
255
+ conn.close()
256
+ return json.loads(result[0]) if result else None