Update app.py
Browse files
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 |
-
|
|
|
|
| 48 |
return "Invalid token. Please enter a valid Hugging Face token."
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Initialize Pixeltable
|
| 51 |
-
pxt.drop_dir('
|
| 52 |
-
pxt.create_dir('
|
| 53 |
|
| 54 |
# Create a table to store the uploaded PDF documents
|
| 55 |
t = pxt.create_table(
|
| 56 |
-
'
|
| 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 |
-
'
|
| 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 |
-
|
|
|
|
| 122 |
return "Invalid token. Please enter a valid Hugging Face token."
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 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}])
|