captain-awesome commited on
Commit
7ffdbce
1 Parent(s): 0b0f1c5

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. app.py +108 -0
  3. ingest.py +23 -0
  4. pet.pdf +3 -0
  5. requirements.txt +8 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ 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
+ pet.pdf filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain import PromptTemplate, LLMChain
2
+ from langchain.llms import CTransformers
3
+ import os
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain.vectorstores import Chroma
6
+ from langchain.chains import RetrievalQA
7
+ from langchain.embeddings import HuggingFaceBgeEmbeddings
8
+ from io import BytesIO
9
+ from langchain.document_loaders import PyPDFLoader
10
+ import gradio as gr
11
+
12
+
13
+ local_llm = "TheBloke/zephyr-7B-beta-GGUF"
14
+ model_file = "zephyr-7b-beta.Q4_0.gguf"
15
+
16
+ config = {
17
+ 'max_new_tokens': 1024,
18
+ 'repetition_penalty': 1.1,
19
+ 'temperature': 0.1,
20
+ 'top_k': 50,
21
+ 'top_p': 0.9,
22
+ 'stream': True,
23
+ 'threads': int(os.cpu_count() / 2)
24
+ }
25
+
26
+ llm = CTransformers(
27
+ model=local_llm,
28
+ model_file=model_file,
29
+ model_type="mistral",
30
+ lib="avx2", #for CPU use
31
+ **config
32
+ )
33
+
34
+ print("LLM Initialized...")
35
+
36
+
37
+ prompt_template = """Use the following pieces of information to answer the user's question.
38
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
39
+
40
+ Context: {context}
41
+ Question: {question}
42
+
43
+ Only return the helpful answer below and nothing else.
44
+ Helpful answer:
45
+ """
46
+
47
+ model_name = "BAAI/bge-large-en"
48
+ model_kwargs = {'device': 'cpu'}
49
+ encode_kwargs = {'normalize_embeddings': False}
50
+ embeddings = HuggingFaceBgeEmbeddings(
51
+ model_name=model_name,
52
+ model_kwargs=model_kwargs,
53
+ encode_kwargs=encode_kwargs
54
+ )
55
+
56
+
57
+ prompt = PromptTemplate(template=prompt_template, input_variables=['context', 'question'])
58
+ load_vector_store = Chroma(persist_directory="stores/pet_cosine", embedding_function=embeddings)
59
+ retriever = load_vector_store.as_retriever(search_kwargs={"k":1})
60
+ # query = "what is the fastest speed for a greyhound dog?"
61
+ # semantic_search = retriever.get_relevant_documents(query)
62
+ # print(semantic_search)
63
+
64
+ print("######################################################################")
65
+
66
+ chain_type_kwargs = {"prompt": prompt}
67
+
68
+ # qa = RetrievalQA.from_chain_type(
69
+ # llm=llm,
70
+ # chain_type="stuff",
71
+ # retriever=retriever,
72
+ # return_source_documents = True,
73
+ # chain_type_kwargs= chain_type_kwargs,
74
+ # verbose=True
75
+ # )
76
+
77
+ # response = qa(query)
78
+
79
+ # print(response)
80
+
81
+ sample_prompts = ["what is the fastest speed for a greyhound dog?", "Why should we not feed chocolates to the dogs?", "Name two factors which might contribute to why some dogs might get scared?"]
82
+
83
+ def get_response(input):
84
+ query = input
85
+ chain_type_kwargs = {"prompt": prompt}
86
+ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True, chain_type_kwargs=chain_type_kwargs, verbose=True)
87
+ response = qa(query)
88
+ return response
89
+
90
+ input = gr.Text(
91
+ label="Prompt",
92
+ show_label=False,
93
+ max_lines=1,
94
+ placeholder="Enter your prompt",
95
+ container=False,
96
+ )
97
+
98
+ iface = gr.Interface(fn=get_response,
99
+ inputs=input,
100
+ outputs="text",
101
+ title="My Dog PetCare Bot",
102
+ description="This is a RAG implementation based on Zephyr 7B Beta LLM.",
103
+ examples=sample_prompts,
104
+ allow_screenshot=False,
105
+ allow_flagging=False
106
+ )
107
+
108
+ iface.launch()
ingest.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
+ from langchain.vectorstores import Chroma
4
+ from langchain.embeddings import HuggingFaceBgeEmbeddings
5
+ from langchain.document_loaders import PyPDFLoader
6
+
7
+ model_name = "BAAI/bge-large-en"
8
+ model_kwargs = {'device': 'cpu'}
9
+ encode_kwargs = {'normalize_embeddings': False}
10
+ embeddings = HuggingFaceBgeEmbeddings(
11
+ model_name=model_name,
12
+ model_kwargs=model_kwargs,
13
+ encode_kwargs=encode_kwargs
14
+ )
15
+
16
+ loader = PyPDFLoader("pet.pdf")
17
+ documents = loader.load()
18
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
19
+ texts = text_splitter.split_documents(documents)
20
+
21
+ vector_store = Chroma.from_documents(texts, embeddings, collection_metadata={"hnsw:space": "cosine"}, persist_directory="stores/pet_cosine")
22
+
23
+ print("Vector Store Created.......")
pet.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:09130a774b5f6d864cbed5b14b88f7f6bb84e39f647d218aa54b2f89a5cf0a0f
3
+ size 2451167
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ chainlit
2
+ ctransformers
3
+ torch
4
+ sentence_transformers
5
+ chromadb
6
+ langchain
7
+ pypdf
8
+ PyPDF2