Spaces:
Runtime error
Runtime error
init
Browse files- Dockerfile +11 -0
- __pycache__/app.cpython-310.pyc +0 -0
- __pycache__/app.cpython-311.pyc +0 -0
- app.py +116 -0
- chainlit.md +1 -0
- requirements.txt +13 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
RUN useradd -m -u 1000 user
|
3 |
+
USER user
|
4 |
+
ENV HOME=/home/user \
|
5 |
+
PATH=/home/user/.local/bin:$PATH
|
6 |
+
WORKDIR $HOME/app
|
7 |
+
COPY --chown=user . $HOME/app
|
8 |
+
COPY ./requirements.txt ~/app/requirements.txt
|
9 |
+
RUN pip install -r requirements.txt
|
10 |
+
COPY . .
|
11 |
+
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
__pycache__/app.cpython-310.pyc
ADDED
Binary file (2.93 kB). View file
|
|
__pycache__/app.cpython-311.pyc
ADDED
Binary file (3.88 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import chainlit as cl
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from langchain_openai import OpenAIEmbeddings
|
6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
+
from langchain_community.vectorstores import Pinecone
|
9 |
+
from operator import itemgetter
|
10 |
+
from langchain.schema.runnable import RunnablePassthrough
|
11 |
+
from langchain_openai import ChatOpenAI
|
12 |
+
from langchain.schema.runnable.config import RunnableConfig
|
13 |
+
from langchain_core.output_parsers import StrOutputParser
|
14 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
15 |
+
from langchain_community.document_loaders import UnstructuredPDFLoader
|
16 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM,BitsAndBytesConfig
|
17 |
+
import torch
|
18 |
+
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
|
19 |
+
|
20 |
+
|
21 |
+
load_dotenv()
|
22 |
+
|
23 |
+
|
24 |
+
RAG_PROMPT = """
|
25 |
+
|
26 |
+
CONTEXT:
|
27 |
+
{context}
|
28 |
+
|
29 |
+
QUERY:
|
30 |
+
{question}
|
31 |
+
|
32 |
+
You house builder and can only provide your answers from the context.
|
33 |
+
You can only provide a response in danish
|
34 |
+
|
35 |
+
Don't tell in your response that you are getting it from the context.
|
36 |
+
|
37 |
+
"""
|
38 |
+
|
39 |
+
|
40 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
41 |
+
chunk_size = 1800,
|
42 |
+
chunk_overlap = 50,
|
43 |
+
length_function=len,
|
44 |
+
is_separator_regex=True,
|
45 |
+
separators=[
|
46 |
+
"\n\n",
|
47 |
+
"\n",
|
48 |
+
" ",
|
49 |
+
".",
|
50 |
+
",",
|
51 |
+
"\u200B",
|
52 |
+
"\uff0c",
|
53 |
+
"\u3001",
|
54 |
+
"\uff0e",
|
55 |
+
"\u3002",
|
56 |
+
"",
|
57 |
+
],
|
58 |
+
)
|
59 |
+
|
60 |
+
bnb_config = BitsAndBytesConfig(
|
61 |
+
load_in_4bit=True,
|
62 |
+
bnb_4bit_quant_type="nf4",
|
63 |
+
bnb_double_quant=True,
|
64 |
+
bnb_4bit_compute_dtype=torch.float16,
|
65 |
+
)
|
66 |
+
|
67 |
+
|
68 |
+
# tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct",
|
69 |
+
# trust_remote_code=True,
|
70 |
+
# quantization_config=bnb_config,
|
71 |
+
# attn_implementation='eager',
|
72 |
+
# device_map='auto',)
|
73 |
+
# model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
|
74 |
+
|
75 |
+
|
76 |
+
# hf = HuggingFacePipeline.from_model_id(
|
77 |
+
# model_id="microsoft/Phi-3-mini-4k-instruct",
|
78 |
+
# task="text-generation",
|
79 |
+
# device_map="auto",
|
80 |
+
# pipeline_kwargs={"max_new_tokens": 10},
|
81 |
+
# )
|
82 |
+
|
83 |
+
|
84 |
+
loader = UnstructuredPDFLoader("./br_syvoghalvfjerds.pdf")
|
85 |
+
data = loader.load_and_split(text_splitter)
|
86 |
+
|
87 |
+
embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
|
88 |
+
|
89 |
+
vector_store = Pinecone.from_documents(data, embedding_model, index_name=os.environ.get('index'))
|
90 |
+
retriever = vector_store.as_retriever()
|
91 |
+
|
92 |
+
rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
|
93 |
+
|
94 |
+
model = ChatOpenAI(model="gpt-3.5-turbo")
|
95 |
+
|
96 |
+
@cl.on_chat_start
|
97 |
+
async def main():
|
98 |
+
mecanic_qa_chain = ""
|
99 |
+
mecanic_qa_chain = (
|
100 |
+
{"context": itemgetter("question") | retriever, "question": itemgetter("question")}
|
101 |
+
| RunnablePassthrough.assign(context=itemgetter("context"))
|
102 |
+
| rag_prompt | model | StrOutputParser()
|
103 |
+
)
|
104 |
+
|
105 |
+
cl.user_session.set("runnable", mecanic_qa_chain)
|
106 |
+
|
107 |
+
@cl.on_message
|
108 |
+
async def on_message(message: cl.Message):
|
109 |
+
runnable = cl.user_session.get("runnable")
|
110 |
+
msg = cl.Message(content="")
|
111 |
+
|
112 |
+
async for chunk in runnable.astream(
|
113 |
+
{"question":message.content},
|
114 |
+
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
115 |
+
):
|
116 |
+
await msg.stream_token(chunk)
|
chainlit.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
welcome to bygningsreglementer!
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
chainlit==0.7.700
|
2 |
+
cohere==4.37
|
3 |
+
openai
|
4 |
+
python-dotenv==1.0.0
|
5 |
+
langchain
|
6 |
+
langchain-community
|
7 |
+
langchain-openai
|
8 |
+
pdfminer
|
9 |
+
pinecone-client
|
10 |
+
unstructured
|
11 |
+
pdf2image
|
12 |
+
transformers
|
13 |
+
bitsandbytes
|