PierreBrunelle commited on
Commit
f618a2b
·
verified ·
1 Parent(s): 63743cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -43,17 +43,23 @@ def validate_token(token):
43
  except Exception:
44
  return False
45
 
 
 
 
46
  def process_files(token, pdf_files, chunk_limit, chunk_separator):
47
- if not validate_token(token):
 
48
  return "Invalid token. Please enter a valid Hugging Face token."
 
 
49
 
50
  # Initialize Pixeltable
51
- pxt.drop_dir('chatbot_demo', force=True)
52
- pxt.create_dir('chatbot_demo')
53
 
54
  # Create a table to store the uploaded PDF documents
55
  t = pxt.create_table(
56
- 'chatbot_demo.documents',
57
  {'document': pxt.DocumentType(nullable=True),
58
  'question': pxt.StringType(nullable=True)}
59
  )
@@ -63,7 +69,7 @@ def process_files(token, pdf_files, chunk_limit, chunk_separator):
63
 
64
  # Create a view that splits the documents into smaller chunks
65
  chunks_t = pxt.create_view(
66
- 'chatbot_demo.chunks',
67
  t,
68
  iterator=DocumentSplitter.create(
69
  document=t.document,
@@ -118,11 +124,13 @@ def process_files(token, pdf_files, chunk_limit, chunk_separator):
118
  return "Files processed successfully. You can start the discussion."
119
 
120
  def get_answer(token, msg):
121
- if not validate_token(token):
 
122
  return "Invalid token. Please enter a valid Hugging Face token."
123
 
124
- t = pxt.get_table('chatbot_demo.documents')
125
- chunks_t = pxt.get_table('chatbot_demo.chunks')
 
126
 
127
  # Insert the question into the table
128
  t.insert([{'question': msg}])
 
43
  except Exception:
44
  return False
45
 
46
+ def get_user_dir(username):
47
+ return f'chatbot_demo_{username}'
48
+
49
  def process_files(token, pdf_files, chunk_limit, chunk_separator):
50
+ username = validate_token(token)
51
+ if not username:
52
  return "Invalid token. Please enter a valid Hugging Face token."
53
+
54
+ user_dir = get_user_dir(username)
55
 
56
  # Initialize Pixeltable
57
+ pxt.drop_dir('user_dir', force=True)
58
+ pxt.create_dir('user_dir')
59
 
60
  # Create a table to store the uploaded PDF documents
61
  t = pxt.create_table(
62
+ f'{user_dir}.documents',
63
  {'document': pxt.DocumentType(nullable=True),
64
  'question': pxt.StringType(nullable=True)}
65
  )
 
69
 
70
  # Create a view that splits the documents into smaller chunks
71
  chunks_t = pxt.create_view(
72
+ f'{user_dir}.chunks',
73
  t,
74
  iterator=DocumentSplitter.create(
75
  document=t.document,
 
124
  return "Files processed successfully. You can start the discussion."
125
 
126
  def get_answer(token, msg):
127
+ username = validate_token(token)
128
+ if not username:
129
  return "Invalid token. Please enter a valid Hugging Face token."
130
 
131
+ user_dir = get_user_dir(username)
132
+ t = pxt.get_table(f'{user_dir}.documents')
133
+ chunks_t = pxt.get_table(f'{user_dir}.chunks')
134
 
135
  # Insert the question into the table
136
  t.insert([{'question': msg}])