Jayem-11 commited on
Commit
58b29d7
1 Parent(s): 11b602d

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +20 -0
  2. main.py +29 -0
  3. qa.py +64 -0
  4. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # you will also find guides on how best to write your Dockerfile
2
+
3
+ FROM python:3.9
4
+
5
+ WORKDIR /code
6
+
7
+ COPY ./requirements.txt /code/requirements.txt
8
+
9
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
10
+
11
+ RUN useradd -m -u 1000 user
12
+ USER user
13
+ ENV HOME=/home/user \
14
+ PATH=/home/user/.local/bin:$PATH
15
+
16
+ WORKDIR $HOME/app
17
+
18
+ COPY --chown=user . $HOME/app
19
+
20
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+
4
+ app = FastAPI()
5
+
6
+
7
+ app.add_middleware(
8
+ CORSMiddleware,
9
+ allow_origins=["*"],
10
+ allow_credentials=True,
11
+ allow_methods=["*"],
12
+ allow_headers=["*"],
13
+ )
14
+
15
+
16
+ @app.get("/ping")
17
+ async def ping():
18
+ return "Hello, I am alive"
19
+
20
+
21
+ @app.post("/qa")
22
+ async def section(request: Request):
23
+
24
+ data = await request.json()
25
+
26
+ from qa import query
27
+ answer = query(data["Question"])
28
+
29
+ return {"Answer": answer}
qa.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.document_loaders import DirectoryLoader, TextLoader
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
+ from langchain.embeddings import HuggingFaceEmbeddings
4
+ from langchain.llms import CTransformers
5
+ from langchain import PromptTemplate
6
+ from langchain.chains import RetrievalQA
7
+ from langchain.vectorstores import FAISS
8
+ import time
9
+
10
+ loader = DirectoryLoader("./infotext", glob="*.txt", loader_cls=TextLoader)
11
+
12
+ # interpret information in the documents
13
+ documents = loader.load()
14
+ splitter = RecursiveCharacterTextSplitter()
15
+ texts = splitter.split_documents(documents)
16
+ embeddings = HuggingFaceEmbeddings(
17
+ model_name="sentence-transformers/all-MiniLM-L6-v2",
18
+ model_kwargs={'device': 'cpu'})
19
+
20
+ # create and save the local database
21
+ db = FAISS.from_documents(texts, embeddings)
22
+ db.save_local("faiss")
23
+
24
+ # prepare the template we will use when prompting the AI
25
+ template = """Use the following pieces of information to answer the user's question.
26
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
27
+ Context: {context}
28
+ Question: {question}
29
+ Only return the helpful answer below and nothing else.
30
+ Helpful answer:
31
+ """
32
+
33
+ # load the language model
34
+ config = {'max_new_tokens': 256, 'temperature': 0.01}
35
+
36
+ llm = CTransformers(model="TheBloke/Llama-2-13B-chat-GGML",
37
+ model_file="llama-2-13b-chat.ggmlv3.q2_K.bin",
38
+ model_type="llama",config=config)
39
+
40
+ # load the interpreted information from the local database
41
+ embeddings = HuggingFaceEmbeddings(
42
+ model_name="sentence-transformers/all-MiniLM-L6-v2",
43
+ model_kwargs={'device': 'cpu'})
44
+
45
+ db = FAISS.load_local("faiss", embeddings)
46
+
47
+ # prepare a version of the llm pre-loaded with the local content
48
+ retriever = db.as_retriever(search_kwargs={'k': 2})
49
+ prompt = PromptTemplate(
50
+ template=template,
51
+ input_variables=['context', 'question'])
52
+
53
+ def query(question):
54
+ model = RetrievalQA.from_chain_type(llm=llm,
55
+ chain_type='stuff',
56
+ retriever=retriever,
57
+ return_source_documents=True,
58
+ chain_type_kwargs={'prompt': prompt})
59
+ time_start = time.time()
60
+ output = model({'query': question})
61
+ response = output["result"]
62
+ time_elapsed = time.time() - time_start
63
+
64
+ return [response, time_elapsed]
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ langchain
4
+ faiss-cpu
5
+ transformers
6
+ ctransformers>=0.2.24
7
+ sentence-transformers