Spaces:
Paused
Paused
BGarida commited on
Commit ·
fc1ab54
1
Parent(s): f62f25d
Deploying PDF Version v15
Browse files- app.py +21 -7
- chainlit-config.md +13 -0
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
import logging
|
|
|
|
| 4 |
from typing import List, Dict
|
| 5 |
from langchain.embeddings import OpenAIEmbeddings
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
@@ -91,14 +92,13 @@ def process_file(file: AskFileResponse) -> List[Dict[str, str]]:
|
|
| 91 |
|
| 92 |
@cl.on_chat_start
|
| 93 |
async def start():
|
| 94 |
-
"""Initialize chat session and process uploaded PDF."""
|
| 95 |
await cl.Message(f"Welcome! Chat session started using {APP_CONFIG['model_name']} for text generation and {APP_CONFIG['embedding_model']} for embeddings. This combination is optimized for processing and querying book-length content. You can now upload a PDF of a book to begin!").send()
|
| 96 |
|
| 97 |
files = await cl.AskFileMessage(
|
| 98 |
content="Please upload a PDF file of a book to begin!",
|
| 99 |
accept=["application/pdf"],
|
| 100 |
max_size_mb=APP_CONFIG["max_file_size_mb"],
|
| 101 |
-
timeout=
|
| 102 |
).send()
|
| 103 |
|
| 104 |
if not files:
|
|
@@ -106,11 +106,26 @@ async def start():
|
|
| 106 |
return
|
| 107 |
|
| 108 |
file = files[0]
|
| 109 |
-
msg = cl.Message(content=f"Processing `{file.name}`..."
|
| 110 |
await msg.send()
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
try:
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
embeddings = OpenAIEmbeddings(model=APP_CONFIG["embedding_model"])
|
| 116 |
docsearch = await cl.make_async(Chroma.from_documents)(
|
|
@@ -132,15 +147,14 @@ async def start():
|
|
| 132 |
streaming=True
|
| 133 |
),
|
| 134 |
chain_type="stuff",
|
| 135 |
-
retriever=docsearch.as_retriever(search_kwargs={"k": 3}),
|
| 136 |
memory=memory,
|
| 137 |
return_source_documents=True,
|
| 138 |
combine_docs_chain_kwargs={"prompt": prompt}
|
| 139 |
)
|
| 140 |
|
| 141 |
cl.user_session.set("chain", chain)
|
| 142 |
-
msg.content
|
| 143 |
-
await msg.update()
|
| 144 |
|
| 145 |
except Exception as e:
|
| 146 |
error_msg = f"An error occurred while processing the file: {str(e)}"
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
import logging
|
| 4 |
+
import time
|
| 5 |
from typing import List, Dict
|
| 6 |
from langchain.embeddings import OpenAIEmbeddings
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
| 92 |
|
| 93 |
@cl.on_chat_start
|
| 94 |
async def start():
|
|
|
|
| 95 |
await cl.Message(f"Welcome! Chat session started using {APP_CONFIG['model_name']} for text generation and {APP_CONFIG['embedding_model']} for embeddings. This combination is optimized for processing and querying book-length content. You can now upload a PDF of a book to begin!").send()
|
| 96 |
|
| 97 |
files = await cl.AskFileMessage(
|
| 98 |
content="Please upload a PDF file of a book to begin!",
|
| 99 |
accept=["application/pdf"],
|
| 100 |
max_size_mb=APP_CONFIG["max_file_size_mb"],
|
| 101 |
+
timeout=3600,
|
| 102 |
).send()
|
| 103 |
|
| 104 |
if not files:
|
|
|
|
| 106 |
return
|
| 107 |
|
| 108 |
file = files[0]
|
| 109 |
+
msg = cl.Message(content=f"Processing `{file.name}`...")
|
| 110 |
await msg.send()
|
| 111 |
|
| 112 |
+
# Simulate upload progress
|
| 113 |
+
for i in range(0, 101, 10):
|
| 114 |
+
await msg.update(content=f"Uploading: {i}%")
|
| 115 |
+
if i < 100:
|
| 116 |
+
time.sleep(0.5) # Simulate upload time
|
| 117 |
+
|
| 118 |
+
await msg.update(content=f"Upload complete. Now processing `{file.name}`...")
|
| 119 |
+
|
| 120 |
try:
|
| 121 |
+
# Simulate processing progress
|
| 122 |
+
for i in range(0, 101, 10):
|
| 123 |
+
await msg.update(content=f"Processing: {i}%")
|
| 124 |
+
if i < 100:
|
| 125 |
+
time.sleep(1) # Simulate processing time
|
| 126 |
+
if i == 50:
|
| 127 |
+
# Actually process the file at the midpoint
|
| 128 |
+
chunks_with_metadata = process_file(file)
|
| 129 |
|
| 130 |
embeddings = OpenAIEmbeddings(model=APP_CONFIG["embedding_model"])
|
| 131 |
docsearch = await cl.make_async(Chroma.from_documents)(
|
|
|
|
| 147 |
streaming=True
|
| 148 |
),
|
| 149 |
chain_type="stuff",
|
| 150 |
+
retriever=docsearch.as_retriever(search_kwargs={"k": 3}),
|
| 151 |
memory=memory,
|
| 152 |
return_source_documents=True,
|
| 153 |
combine_docs_chain_kwargs={"prompt": prompt}
|
| 154 |
)
|
| 155 |
|
| 156 |
cl.user_session.set("chain", chain)
|
| 157 |
+
await msg.update(content=f"Processing `{file.name}` complete. You can now ask questions about the book!")
|
|
|
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
error_msg = f"An error occurred while processing the file: {str(e)}"
|
chainlit-config.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Welcome to Chat with Your Book
|
| 2 |
+
|
| 3 |
+
With this application, you can chat with an uploaded PDF book that is smaller than 100MB!
|
| 4 |
+
|
| 5 |
+
## Upload Instructions
|
| 6 |
+
|
| 7 |
+
1. Click the upload button or drag and drop a single PDF file here.
|
| 8 |
+
2. Wait for the file to be processed.
|
| 9 |
+
3. Start asking questions about the book!
|
| 10 |
+
|
| 11 |
+
## Privacy Notice
|
| 12 |
+
|
| 13 |
+
Please note that uploaded files are processed in-memory and are not stored permanently. Your conversations are not saved after the session ends.
|