Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,26 +58,45 @@ def read_file_content(file):
|
|
| 58 |
|
| 59 |
@cl.on_chat_start
|
| 60 |
async def on_chat_start():
|
| 61 |
-
files
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
max_size_mb=20,
|
| 66 |
).send()
|
| 67 |
|
| 68 |
-
|
| 69 |
-
await cl.Message(content="No files were uploaded. Please try again.").send()
|
| 70 |
-
return
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
try:
|
| 76 |
-
documents = []
|
| 77 |
for file in files:
|
| 78 |
text = read_file_content(file)
|
| 79 |
documents.append(Document(text=text, metadata={"filename": file.name}))
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Create index
|
| 82 |
index = VectorStoreIndex.from_documents(
|
| 83 |
documents, service_context=service_context
|
|
|
|
| 58 |
|
| 59 |
@cl.on_chat_start
|
| 60 |
async def on_chat_start():
|
| 61 |
+
# First, ask if the user wants to upload files or paste text
|
| 62 |
+
choice = await cl.AskUserMessage(
|
| 63 |
+
content="Would you like to upload files or paste text?",
|
| 64 |
+
choices=["Upload Files", "Paste Text"]
|
|
|
|
| 65 |
).send()
|
| 66 |
|
| 67 |
+
documents = []
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
if choice == "Upload Files":
|
| 70 |
+
files = await cl.AskFileMessage(
|
| 71 |
+
content="Please upload PDF or TXT files to begin!",
|
| 72 |
+
accept=["application/pdf", "text/plain"],
|
| 73 |
+
max_files=5,
|
| 74 |
+
max_size_mb=20,
|
| 75 |
+
).send()
|
| 76 |
+
|
| 77 |
+
if not files:
|
| 78 |
+
await cl.Message(content="No files were uploaded. Please try again.").send()
|
| 79 |
+
return
|
| 80 |
|
|
|
|
|
|
|
| 81 |
for file in files:
|
| 82 |
text = read_file_content(file)
|
| 83 |
documents.append(Document(text=text, metadata={"filename": file.name}))
|
| 84 |
|
| 85 |
+
elif choice == "Paste Text":
|
| 86 |
+
pasted_text = await cl.AskUserMessage(
|
| 87 |
+
content="Please paste your document text here:"
|
| 88 |
+
).send()
|
| 89 |
+
|
| 90 |
+
if not pasted_text:
|
| 91 |
+
await cl.Message(content="No text was pasted. Please try again.").send()
|
| 92 |
+
return
|
| 93 |
+
|
| 94 |
+
documents.append(Document(text=pasted_text, metadata={"source": "pasted_text"}))
|
| 95 |
+
|
| 96 |
+
msg = cl.Message(content="Processing input...")
|
| 97 |
+
await msg.send()
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
# Create index
|
| 101 |
index = VectorStoreIndex.from_documents(
|
| 102 |
documents, service_context=service_context
|