-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
46 lines (37 loc) · 1.43 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from langchain.chains import ConversationalRetrievalChain
from langchain.docstore.document import Document
import chainlit as cl
from typing import List
from src.data_parse import read_pdf_chain
from src.chatbot import Chatbot
@cl.on_chat_start
async def on_chat_start():
# Sending an image with the local file path
elements = [
cl.Image(name="image1", display="inline", path="OIP.jpeg")
]
files = None
await cl.Message(content="Hello there, I am Gemma. How can I help you ?", elements=elements).send()
while files is None:
files = await cl.AskFileMessage(
content="Please upload a pdf file to begin!",
accept=["application/pdf"],
max_size_mb=20,
timeout=180,
).send()
file = files[0]
print(file)
read_pdf_chain(file, file.name)
msg = cl.Message(content=f"File processed `{file.name}`...")
await msg.send()
chat = Chatbot()
cl.user_session.set("chain", chat.chain)
@cl.on_message
async def main(message: cl.Message):
chain = cl.user_session.get("chain") # type: ConversationalRetrievalChain
cb = cl.AsyncLangchainCallbackHandler()
res = await chain.ainvoke({"question": message.content}, callbacks=[cb])
answer = res["answer"]
source_documents = res["source_documents"] # type: List[Document]
text_elements = [] # type: List[cl.Text]
await cl.Message(content=answer, elements=text_elements).send()