-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
64 lines (48 loc) · 1.86 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import io
import chainlit as cl
from PIL import Image
from components.chainlit.create_retriever import create_retriever
from components.chainlit.run_rag_workflow import run_rag_workflow
from components.rag_workflow import RAGWorkflow
@cl.on_chat_start
async def on_chat_start():
files = None
# Wait for the user to upload a file
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]
msg = cl.Message(content=f"Processing `{file.name}`...")
await msg.send()
retriever = await create_retriever(file)
rag_workflow = RAGWorkflow(retriever=retriever)
workflow = rag_workflow.create_workflow()
app = workflow.compile()
# Generate and save the workflow graph
img_data = app.get_graph().draw_mermaid_png()
img = Image.open(io.BytesIO(img_data))
img.save("resources/rag_workflow.png")
image = cl.Image(
path="resources/rag_workflow.png", name="rag_workflow", display="side"
)
# Attach the image to the message
await cl.Message(
content="Here is the workflow graph: rag_workflow",
elements=[image],
).send()
# Let the user know that the system is ready
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
await msg.update()
cl.user_session.set("app", app)
cl.user_session.set("file_path", file.path)
@cl.on_message
async def main(message: cl.Message):
app = cl.user_session.get("app") # type: RAGWorkflow
file_path = cl.user_session.get("file_path")
inputs = {"question": message.content, "iterations": 0}
answer, pdf_elements = await run_rag_workflow(app, inputs, file_path)
await cl.Message(content=answer, elements=pdf_elements).send()