axjh03 commited on
Commit
6ff7308
2 Parent(s): 1b01b82 2371911

Initial commit

Browse files
.DS_Store ADDED
Binary file (6.15 kB). View file
 
.gitattributes CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.pdf filter=lfs diff=lfs merge=lfs -text
37
+ vectorstores/db_faiss/index.faiss filter=lfs diff=lfs merge=lfs -text
38
+
Dockerfile CHANGED
@@ -1,16 +1,31 @@
1
- # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
- # you will also find guides on how best to write your Dockerfile
3
-
4
- FROM python:3.9
5
 
 
6
  RUN useradd -m -u 1000 user
7
 
 
8
  WORKDIR /app
9
 
 
 
 
 
 
 
 
10
  COPY --chown=user ./requirements.txt requirements.txt
11
 
12
- RUN pip install --no-cache-dir --upgrade -r requirements.txt
 
 
 
 
 
 
13
 
 
14
  COPY --chown=user . /app
15
 
16
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
1
+ # Use the official Python image as base
2
+ FROM python:3.9-slim
 
 
3
 
4
+ # Create a non-root user
5
  RUN useradd -m -u 1000 user
6
 
7
+ # Set the working directory in the container
8
  WORKDIR /app
9
 
10
+ # Install system dependencies
11
+ RUN apt-get update && \
12
+ apt-get install -y gcc python3-dev python3-venv && \
13
+ apt-get clean && \
14
+ rm -rf /var/lib/apt/lists/*
15
+
16
+ # Copy requirements file
17
  COPY --chown=user ./requirements.txt requirements.txt
18
 
19
+ # Install dependencies
20
+ RUN python3 -m venv env && \
21
+ /bin/bash -c "source env/bin/activate && pip install --no-cache-dir --upgrade -r requirements.txt" && \
22
+ /bin/bash -c "source env/bin/activate && pip install chainlit langchain_community"
23
+
24
+ # Make port 7680 available to the world outside the container
25
+ EXPOSE 7680
26
 
27
+ # Copy the application code into the container
28
  COPY --chown=user . /app
29
 
30
+ # Run the application
31
+ CMD ["/bin/bash", "-c", "source env/bin/activate && uvicorn app:app --host 0.0.0.0 --port 7680 && python3 downloadLLM.py && python3 ingest.py && chainlit run app.py --host 0.0.0.0 --port 7680"]
__pycache__/main.cpython-39.pyc ADDED
Binary file (980 Bytes). View file
 
__pycache__/model.cpython-311.pyc ADDED
Binary file (4.46 kB). View file
 
__pycache__/model.cpython-39.pyc ADDED
Binary file (2.86 kB). View file
 
app.py CHANGED
@@ -1,7 +1,32 @@
1
- from fastapi import FastAPI
2
 
3
- app = FastAPI()
 
4
 
5
- @app.get("/")
6
- def greet_json():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
+ from langchain.document_loaders import PyPDFLoader, DirectoryLoader # could have done any unstructured text loader like ppt and xlsx
4
 
5
+
6
+ from langchain.embeddings import HuggingFaceBgeEmbeddings # we can replace huggingface with facetransformers
7
+ from chainlit import cl
8
+ from langchain.vectorstores import FAISS
9
+
10
+ DATA_PATH = "data/"
11
+ DB_FAISS_PATH = "vectorstores/db_faiss"
12
+
13
+ #create vector database
14
+ def create_vector_db():
15
+ # WE can change .pdf with any other unstructured text format
16
+ loader = DirectoryLoader(DATA_PATH, glob="*.pdf", loader_cls = PyPDFLoader)
17
+ documents = loader.load()
18
+
19
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=50)
20
+
21
+ texts = text_splitter.split_documents(documents)
22
+
23
+ embeddings = HuggingFaceBgeEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"}) # change to GPU if you want
24
+
25
+ # cuda is not supported in my MAC M1! SADLY.
26
+
27
+ db = FAISS.from_documents(texts, embeddings)
28
+ db.save_local(DB_FAISS_PATH)
29
+
30
+ if __name__ == "__main__":
31
+ create_vector_db()
32
+ cl.run()
chainlit.md ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AskAway - HackUTA 2023 🚀🚀🚀
2
+ This project was built for HackUTA 2023 to demonstrate an LLM which can be trained and ran offline on local host.
3
+
4
+ ## Developers
5
+ -**Alok Jha**
6
+ -**Ribesh Joshi**
7
+ -**Khushi Gauli**
8
+ -**Kritazya Upreti**
9
+
10
+
11
+ ## About the project
12
+
13
+ This project showcases how a large language model can be fine-tuned on a custom dataset and deployed for low-latency inference completely offline on a local machine.
14
+
15
+ The project uses the LangChain library to handle data ingestion, model training loops and managing the model lifecycle. The ChainLit framework is used to package and deploy the LLM inference server for easy access through a web UI or APIs.
16
+
17
+ ## Some key aspects:
18
+ -**Fine-tunes a small-sized LLM like MiniLM for custom data**
19
+ -**Does not require an internet connection after deployment**
20
+ -**Low-latency querying against the fine-tuned LLM**
21
+ -**Easy to update model as new data comes in**
22
+ -**Python implementation allows full customization**
23
+
24
+ This demonstrates how modern transfer learning techniques can be used to take a pre-trained LLM and specialize it for niche domains and offline usage.
25
+
26
+ ## Usage
27
+ -**Give the LLM domain-specific data**
28
+ -**Ask natural language questions to get information about the data**
data/anatomy+phys+vol2a.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7e3365119163aa2501b4ef0f57cf498f63ef4e3a1b9bc1cd4833a293499f3efa
3
+ size 48242701
dcoker commands.txt ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ docker build -t anatomyai .
2
+ docker run -p 8000:8000 anatomyai
3
+
4
+
5
+ Push Your Docker Image to Docker Hub
6
+ Log in to Docker Hub:
7
+
8
+ bash
9
+ Copy code
10
+ docker login
11
+ Tag your Docker image:
12
+
13
+ bash
14
+ Copy code
15
+ docker tag anatomyai your_dockerhub_username/anatomyai:latest
16
+ Push your Docker image:
17
+
18
+ bash
19
+ Copy code
20
+ docker push your_dockerhub_username/anatomyai:latest
21
+ Deploy to Hugging Face
22
+ Create a new Space on Hugging Face by going to the Spaces page and clicking on "New Space".
23
+
24
+ Choose the Docker runtime for your Space.
25
+
26
+ Configure your Space:
27
+
28
+ In the Space settings, provide the name of your Docker image (your_dockerhub_username/anatomyai:latest).
29
+ Set any necessary environment variables or secrets required by your application.
30
+ Deploy the Space:
31
+
32
+ Once you have configured the Space, Hugging Face will pull the Docker image from Docker Hub and deploy it.
dcoker mac ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Install necessary packages
8
+ RUN apt-get update && \
9
+ apt-get install -y gcc python3-dev && \
10
+ apt-get clean && \
11
+ rm -rf /var/lib/apt/lists/*
12
+
13
+ # Copy the current directory contents into the container at /app
14
+ COPY . /app
15
+
16
+ # Install virtualenv
17
+ RUN pip install virtualenv
18
+
19
+ # Create and activate virtual environment, install dependencies, and Chainlit
20
+ RUN python3 -m venv env && \
21
+ /bin/bash -c "source env/bin/activate && \
22
+ pip install --no-cache-dir -r requirements.txt && \
23
+ pip install chainlit && \
24
+ pip install langchain_community"
25
+
26
+ # Make port 8000 available to the world outside this container
27
+ EXPOSE 8000
28
+
29
+ # Run your Python scripts and Chainlit app
30
+ CMD /bin/bash -c "source env/bin/activate && \
31
+ python3 downloadLLM.py && \
32
+ python3 ingest.py && \
33
+ chainlit run main.py --host 0.0.0.0 --port 8000"
downloadLLM.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ url = "https://cdn-lfs.huggingface.co/repos/30/e3/30e3aca7233f7337633262ff6d59dd98559ecd8982e7419b39752c8d0daae1ca/8daa9615cce30c259a9555b1cc250d461d1bc69980a274b44d7eda0be78076d8?response-content-disposition=attachment%3B+filename*%3DUTF-8%27%27llama-2-7b-chat.ggmlv3.q4_0.bin%3B+filename%3D%22llama-2-7b-chat.ggmlv3.q4_0.bin%22%3B&response-content-type=application%2Foctet-stream&Expires=1717963444&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTcxNzk2MzQ0NH19LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy5odWdnaW5nZmFjZS5jby9yZXBvcy8zMC9lMy8zMGUzYWNhNzIzM2Y3MzM3NjMzMjYyZmY2ZDU5ZGQ5ODU1OWVjZDg5ODJlNzQxOWIzOTc1MmM4ZDBkYWFlMWNhLzhkYWE5NjE1Y2NlMzBjMjU5YTk1NTViMWNjMjUwZDQ2MWQxYmM2OTk4MGEyNzRiNDRkN2VkYTBiZTc4MDc2ZDg%7EcmVzcG9uc2UtY29udGVudC1kaXNwb3NpdGlvbj0qJnJlc3BvbnNlLWNvbnRlbnQtdHlwZT0qIn1dfQ__&Signature=SyIt4jnVZLhKHXELakpuirrJy9HHG7ETsYGWP0u8KhmKD2sv8hY35khc0F7fOG6Gh9sdSqvebSzFz-RrqMw-ibcLmzqoRJ35ZKNE0JjMmk61APtW0DiMMD8bCwRHQs8T3IA-eIP4ybz06UD3NTVTTrKCGkPw8nZMImph-BFmx6fOO9JD9CrrQ7TBE-LOAbfsGzOF-nAXjPhGzYvtliIsATipqDkTgGKOcJx9PeQDyHBRaHHr5jrss20%7EUixaWRHAt6Og2JJUA7CK%7ElCEy7Jgo5--%7EqDZoyfXxhvV6zqsaZrs1aXxoCov-QBoz6hEN5yKPjpWJC9DYjMJn4kce3o9fQ__&Key-Pair-Id=KVTP0A1DKRTAX"
4
+
5
+ file_name = "llama-2-7b-chat.ggmlv3.q4_0.bin"
6
+
7
+ # Send GET request to the URL
8
+ response = requests.get(url, stream=True)
9
+
10
+ # Raise an exception in case of an HTTP error
11
+ response.raise_for_status()
12
+
13
+ # Write the content of the response to a file
14
+ with open(file_name, 'wb') as file:
15
+ for chunk in response.iter_content(chunk_size=8192):
16
+ if chunk:
17
+ file.write(chunk)
18
+ print("Downloading llm")
19
+ print(f"Downloaded 'llama-2-7b-chat.ggmlv3.q4_0.bin' successfully.")
ingest.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
2
+ from langchain.document_loaders import PyPDFLoader, DirectoryLoader # could have done any unstructured text loader like ppt and xlsx
3
+
4
+
5
+ from langchain.embeddings import HuggingFaceBgeEmbeddings # we can replace huggingface with facetransformers
6
+
7
+ from langchain.vectorstores import FAISS
8
+
9
+ DATA_PATH = "data/"
10
+ DB_FAISS_PATH = "vectorstores/db_faiss"
11
+
12
+ #create vector database
13
+ def create_vector_db():
14
+ # WE can change .pdf with any other unstructured text format
15
+ loader = DirectoryLoader(DATA_PATH, glob="*.pdf", loader_cls = PyPDFLoader)
16
+ documents = loader.load()
17
+
18
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
19
+
20
+ texts = text_splitter.split_documents(documents)
21
+
22
+ embeddings = HuggingFaceBgeEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"}) # change to GPU if you want
23
+
24
+ # cuda is not supported in my MAC M1! SADLY.
25
+
26
+ db = FAISS.from_documents(texts, embeddings)
27
+ db.save_local(DB_FAISS_PATH)
28
+
29
+ if __name__ == "__main__":
30
+ create_vector_db()
main.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
2
+ from langchain.document_loaders import PyPDFLoader, DirectoryLoader # could have done any unstructured text loader like ppt and xlsx
3
+
4
+
5
+ from langchain.embeddings import HuggingFaceBgeEmbeddings # we can replace huggingface with facetransformers
6
+ from chainlit import cl
7
+ from langchain.vectorstores import FAISS
8
+
9
+ DATA_PATH = "data/"
10
+ DB_FAISS_PATH = "vectorstores/db_faiss"
11
+
12
+ #create vector database
13
+ def create_vector_db():
14
+ # WE can change .pdf with any other unstructured text format
15
+ loader = DirectoryLoader(DATA_PATH, glob="*.pdf", loader_cls = PyPDFLoader)
16
+ documents = loader.load()
17
+
18
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=50)
19
+
20
+ texts = text_splitter.split_documents(documents)
21
+
22
+ embeddings = HuggingFaceBgeEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"}) # change to GPU if you want
23
+
24
+ # cuda is not supported in my MAC M1! SADLY.
25
+
26
+ db = FAISS.from_documents(texts, embeddings)
27
+ db.save_local(DB_FAISS_PATH)
28
+
29
+ if __name__ == "__main__":
30
+ create_vector_db()
31
+ cl.run()
model.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain import PromptTemplate
2
+ from langchain.embeddings import HuggingFaceBgeEmbeddings
3
+ from langchain.vectorstores import FAISS
4
+ from langchain.llms import ctransformers
5
+ from langchain.chains import RetrievalQA
6
+
7
+ import chainlit as cl
8
+
9
+
10
+ DB_FAISS_PATH = "vectorstores/db_faiss"
11
+
12
+
13
+ input_variables = ["Context", "question"]
14
+
15
+
16
+ custom_prompt_template = """Use the following pieces of information to answer the user's question.
17
+
18
+
19
+ Context = {Context}
20
+
21
+ Question = {question}
22
+
23
+
24
+
25
+ only returns the helpful answer below and nothing else
26
+
27
+ Helpful answer
28
+
29
+ """
30
+
31
+
32
+ def set_custon_prompt():
33
+
34
+ prompt = PromptTemplate(template=custom_prompt_template,
35
+
36
+ input_variables=input_variables,
37
+
38
+ validate_variable_names=False)
39
+
40
+ return prompt
41
+
42
+
43
+ def load_llm():
44
+
45
+ llm = ctransformers.CTransformers(
46
+
47
+ # old model = llama-2-7b-chat.ggmlv3.q8_0.bin
48
+ model="llama-2-7b-chat.ggmlv3.q4_0.bin",
49
+
50
+ model_type='llama',
51
+
52
+ max_new_tokens=512,
53
+
54
+ temperature=0.5
55
+
56
+ )
57
+
58
+ return llm
59
+
60
+
61
+ def retrival_qa_chain(llm, prompt, db):
62
+
63
+ qa_chain = RetrievalQA.from_chain_type(
64
+
65
+ llm=llm,
66
+
67
+ chain_type="stuff",
68
+
69
+ retriever=db.as_retriever(search_kwargs={"k": 2}),
70
+
71
+
72
+
73
+ return_source_documents=True,
74
+
75
+ chain_type_kwargs={'prompt': prompt, 'document_variable_name': 'Context'}
76
+
77
+ )
78
+
79
+ return qa_chain
80
+
81
+
82
+ def qa_bot():
83
+
84
+ embeddings = HuggingFaceBgeEmbeddings(
85
+ model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"})
86
+
87
+ db = FAISS.load_local(DB_FAISS_PATH, embeddings)
88
+
89
+ llm = load_llm()
90
+
91
+ qa_prompt = set_custon_prompt()
92
+
93
+ qa = retrival_qa_chain(llm, qa_prompt, db)
94
+
95
+ return qa
96
+
97
+
98
+ def final_result(query):
99
+
100
+ qa_result = qa_bot()
101
+
102
+ response = qa_result({'query': query})
103
+
104
+ return response
105
+
106
+
107
+ ### Chain LIT ###
108
+
109
+ @cl.on_chat_start
110
+ async def start():
111
+
112
+ chain = qa_bot()
113
+
114
+ cl.user_session.set("chain", chain)
115
+
116
+ msg = cl.Message(content="Starting the bot.....")
117
+
118
+ await msg.send()
119
+
120
+ msg.content = "Hi, What is your query?"
121
+
122
+ await msg.update()
123
+
124
+
125
+ @cl.on_message
126
+ async def main(message):
127
+
128
+ chain = cl.user_session.get("chain")
129
+
130
+ cb = cl.AsyncLangchainCallbackHandler(
131
+
132
+ stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
133
+
134
+
135
+
136
+ )
137
+
138
+ cb.answer_reached = True
139
+
140
+ res = await chain.acall(message, callbacks=[cb])
141
+
142
+ answer = res['result']
143
+
144
+ sources = res['source_documents']
145
+
146
+ if sources:
147
+
148
+ answer += f"\nSources: " + str(sources)
149
+
150
+ else:
151
+
152
+ answer += f"\nSources: No sources found"
153
+
154
+ await cl.Message(content=answer).send()
package.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "chainlit-app",
3
+ "version": "1.0.0",
4
+ "scripts": {
5
+ "build": "sh build.sh"
6
+ }
7
+ }
8
+
requirements.txt CHANGED
@@ -1,2 +1,11 @@
1
- fastapi
2
- uvicorn[standard]
 
 
 
 
 
 
 
 
 
 
1
+ pypdf
2
+ langchain
3
+ torch
4
+ accelerate
5
+ bitsandbytes
6
+ transformers
7
+ sentence_transformers
8
+ faiss_cpu
9
+ chainlit
10
+ langchain_community
11
+ gdown