Spaces:
Sleeping
Sleeping
Asaad Almutareb
commited on
Commit
Β·
ea36e00
1
Parent(s):
76d4a7e
changed proj name
Browse files
README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
emoji: π
|
4 |
colorFrom: gray
|
5 |
colorTo: gray
|
@@ -9,13 +9,13 @@ app_file: app.py
|
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
-
#
|
13 |
|
14 |
## Overview
|
15 |
-
|
16 |
|
17 |
## Key Features
|
18 |
-
- **AI-Powered Documentation Retrieval:** Utilizes various fine-tuned
|
19 |
- **Rich User Interface:** Features a user-friendly interface built with Gradio.
|
20 |
- **Advanced Language Understanding:** Employs LangChain for implementing RAG setups and sophisticated natural language processing.
|
21 |
- **Efficient Data Handling:** Leverages Chroma DB and FAISS for optimized data storage and retrieval.
|
@@ -34,7 +34,7 @@ This setup is tested with the following models:
|
|
34 |
|
35 |
## Prerequisites
|
36 |
- Python 3.8 or later
|
37 |
-
- [Additional prerequisites
|
38 |
|
39 |
## Installation
|
40 |
1. Clone the repository:
|
@@ -68,14 +68,20 @@ python app.py
|
|
68 |
[Include additional usage instructions and examples]
|
69 |
|
70 |
## Contributing
|
71 |
-
Contributions to
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
## Support
|
74 |
-
For support,
|
75 |
|
76 |
## Authors and Acknowledgement
|
77 |
- [Name]
|
78 |
-
-
|
79 |
|
80 |
## License
|
81 |
This project is licensed under the [License] - see the LICENSE file for details.
|
|
|
1 |
---
|
2 |
+
title: LC Gradio DocsAI
|
3 |
emoji: π
|
4 |
colorFrom: gray
|
5 |
colorTo: gray
|
|
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
+
# LC Gradio DocsAI π
|
13 |
|
14 |
## Overview
|
15 |
+
LC-Gradio-DocAI is a demo project showcasing a privately hosted advanced Documentation AI helper, demonstrating a fine-tuned 7B model's capabilities in aiding users with software documentation. This application integrates technologies like Retrieval-Augmented Generation (RAG) using LangChain, a vector store using Chroma DB or and FAISS and Gradio for a model UI to offer insightful documentation assistance. It's designed to help users navigate and utilize software tools efficiently by retrieving relevant documentation pages and maintaining conversational flow.
|
16 |
|
17 |
## Key Features
|
18 |
+
- **AI-Powered Documentation Retrieval:** Utilizes various fine-tuned 7B models for precise and context-aware responses.
|
19 |
- **Rich User Interface:** Features a user-friendly interface built with Gradio.
|
20 |
- **Advanced Language Understanding:** Employs LangChain for implementing RAG setups and sophisticated natural language processing.
|
21 |
- **Efficient Data Handling:** Leverages Chroma DB and FAISS for optimized data storage and retrieval.
|
|
|
34 |
|
35 |
## Prerequisites
|
36 |
- Python 3.8 or later
|
37 |
+
- [Additional prerequisites...]
|
38 |
|
39 |
## Installation
|
40 |
1. Clone the repository:
|
|
|
68 |
[Include additional usage instructions and examples]
|
69 |
|
70 |
## Contributing
|
71 |
+
Contributions to LC-Gradio-DocsAI are welcome. Here's how you can contribute:
|
72 |
+
|
73 |
+
1. Fork the repository.
|
74 |
+
2. Create a new branch (git checkout -b feature/YourFeature).
|
75 |
+
3. Make changes and commit (git commit -m 'Add some feature').
|
76 |
+
4. Push to the branch (git push origin feature/YourFeature).
|
77 |
+
5. Create a new Pull Request.
|
78 |
|
79 |
## Support
|
80 |
+
For support, please open an issue here on Github.
|
81 |
|
82 |
## Authors and Acknowledgement
|
83 |
- [Name]
|
84 |
+
- Thanks to contributors of all the awesome open-source LLMs, LangChain, HuggingFace, Chroma Vector Store, FAISS and Graido UI.
|
85 |
|
86 |
## License
|
87 |
This project is licensed under the [License] - see the LICENSE file for details.
|
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
# gradio
|
2 |
import gradio as gr
|
3 |
-
import random
|
4 |
-
import time
|
5 |
#boto3 for S3 access
|
6 |
import boto3
|
7 |
from botocore import UNSIGNED
|
@@ -18,12 +18,22 @@ from langchain.vectorstores import Chroma
|
|
18 |
from langchain.vectorstores import FAISS
|
19 |
# retrieval chain
|
20 |
from langchain.chains import RetrievalQA
|
|
|
21 |
# prompt template
|
22 |
from langchain.prompts import PromptTemplate
|
23 |
from langchain.memory import ConversationBufferMemory
|
24 |
# logging
|
25 |
-
|
26 |
import zipfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# load .env variables
|
29 |
config = load_dotenv(".env")
|
@@ -32,6 +42,7 @@ AWS_S3_LOCATION=os.getenv('AWS_S3_LOCATION')
|
|
32 |
AWS_S3_FILE=os.getenv('AWS_S3_FILE')
|
33 |
VS_DESTINATION=os.getenv('VS_DESTINATION')
|
34 |
|
|
|
35 |
model_id = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta", model_kwargs={
|
36 |
"temperature":0.1,
|
37 |
"max_new_tokens":1024,
|
@@ -43,7 +54,7 @@ model_id = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta", model_kwargs={
|
|
43 |
model_name = "sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
44 |
embeddings = HuggingFaceHubEmbeddings(repo_id=model_name)
|
45 |
|
46 |
-
|
47 |
s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
|
48 |
|
49 |
## Chroma DB
|
@@ -60,6 +71,12 @@ db.get()
|
|
60 |
# db = FAISS.load_local(FAISS_INDEX_PATH, embeddings)
|
61 |
|
62 |
retriever = db.as_retriever(search_type = "mmr")#, search_kwargs={'k': 5, 'fetch_k': 25})
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
global qa
|
64 |
template = """
|
65 |
You are the friendly documentation buddy Arti, who helps the Human in using RAY, the open-source unified framework for scaling AI and Python applications.\
|
@@ -81,13 +98,28 @@ prompt = PromptTemplate(
|
|
81 |
template=template,
|
82 |
)
|
83 |
memory = ConversationBufferMemory(memory_key="history", input_key="question")
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
"verbose": True,
|
86 |
"memory": memory,
|
87 |
-
"prompt": prompt
|
|
|
88 |
}
|
89 |
)
|
90 |
|
|
|
|
|
91 |
|
92 |
def add_text(history, text):
|
93 |
history = history + [(text, None)]
|
@@ -95,18 +127,20 @@ def add_text(history, text):
|
|
95 |
|
96 |
def bot(history):
|
97 |
response = infer(history[-1][0], history)
|
|
|
98 |
print(*memory)
|
99 |
-
sources = [doc.metadata.get("source") for doc in response['
|
100 |
src_list = '\n'.join(sources)
|
101 |
-
print_this = response['
|
|
|
102 |
|
103 |
#history[-1][1] = ""
|
104 |
#for character in response['result']: #print_this:
|
105 |
# history[-1][1] += character
|
106 |
# time.sleep(0.05)
|
107 |
# yield history
|
108 |
-
history[-1][1] =
|
109 |
-
return history
|
110 |
|
111 |
def infer(question, history):
|
112 |
query = question
|
@@ -137,5 +171,4 @@ with gr.Blocks(css=css) as demo:
|
|
137 |
)
|
138 |
clear.click(lambda: None, None, chatbot, queue=False)
|
139 |
|
140 |
-
demo.queue()
|
141 |
-
demo.launch()
|
|
|
1 |
# gradio
|
2 |
import gradio as gr
|
3 |
+
#import random
|
4 |
+
#import time
|
5 |
#boto3 for S3 access
|
6 |
import boto3
|
7 |
from botocore import UNSIGNED
|
|
|
18 |
from langchain.vectorstores import FAISS
|
19 |
# retrieval chain
|
20 |
from langchain.chains import RetrievalQA
|
21 |
+
from langchain.chains import RetrievalQAWithSourcesChain
|
22 |
# prompt template
|
23 |
from langchain.prompts import PromptTemplate
|
24 |
from langchain.memory import ConversationBufferMemory
|
25 |
# logging
|
26 |
+
import logging
|
27 |
import zipfile
|
28 |
+
#contextual retriever
|
29 |
+
from langchain.retrievers import ContextualCompressionRetriever
|
30 |
+
from langchain.retrievers.document_compressors import LLMChainExtractor
|
31 |
+
from langchain.retrievers.document_compressors import EmbeddingsFilter
|
32 |
+
from langchain.retrievers.multi_query import MultiQueryRetriever
|
33 |
+
# streaming
|
34 |
+
from threading import Thread
|
35 |
+
from transformers import TextIteratorStreamer
|
36 |
+
|
37 |
|
38 |
# load .env variables
|
39 |
config = load_dotenv(".env")
|
|
|
42 |
AWS_S3_FILE=os.getenv('AWS_S3_FILE')
|
43 |
VS_DESTINATION=os.getenv('VS_DESTINATION')
|
44 |
|
45 |
+
# initialize Model config
|
46 |
model_id = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta", model_kwargs={
|
47 |
"temperature":0.1,
|
48 |
"max_new_tokens":1024,
|
|
|
54 |
model_name = "sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
55 |
embeddings = HuggingFaceHubEmbeddings(repo_id=model_name)
|
56 |
|
57 |
+
# retrieve vectorsrore
|
58 |
s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED))
|
59 |
|
60 |
## Chroma DB
|
|
|
71 |
# db = FAISS.load_local(FAISS_INDEX_PATH, embeddings)
|
72 |
|
73 |
retriever = db.as_retriever(search_type = "mmr")#, search_kwargs={'k': 5, 'fetch_k': 25})
|
74 |
+
|
75 |
+
compressor = LLMChainExtractor.from_llm(model_id)
|
76 |
+
compression_retriever = ContextualCompressionRetriever(base_compressor=compressor, base_retriever=retriever)
|
77 |
+
# embeddings_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.76)
|
78 |
+
# compression_retriever = ContextualCompressionRetriever(base_compressor=embeddings_filter, base_retriever=retriever)
|
79 |
+
|
80 |
global qa
|
81 |
template = """
|
82 |
You are the friendly documentation buddy Arti, who helps the Human in using RAY, the open-source unified framework for scaling AI and Python applications.\
|
|
|
98 |
template=template,
|
99 |
)
|
100 |
memory = ConversationBufferMemory(memory_key="history", input_key="question")
|
101 |
+
|
102 |
+
# logging for the chain
|
103 |
+
logging.basicConfig()
|
104 |
+
logging.getLogger("langchain.chains").setLevel(logging.INFO)
|
105 |
+
|
106 |
+
|
107 |
+
# qa = RetrievalQA.from_chain_type(llm=model_id, chain_type="stuff", retriever=compression_retriever, verbose=True, return_source_documents=True, chain_type_kwargs={
|
108 |
+
# "verbose": True,
|
109 |
+
# "memory": memory,
|
110 |
+
# "prompt": prompt
|
111 |
+
# }
|
112 |
+
# )
|
113 |
+
qa = RetrievalQAWithSourcesChain.from_chain_type(llm=model_id, retriever=compression_retriever, verbose=True, chain_type_kwargs={
|
114 |
"verbose": True,
|
115 |
"memory": memory,
|
116 |
+
"prompt": prompt,
|
117 |
+
"document_variable_name": "context"
|
118 |
}
|
119 |
)
|
120 |
|
121 |
+
def pretty_print_docs(docs):
|
122 |
+
print(f"\n{'-' * 100}\n".join([f"Document {i+1}:\n\n" + d.page_content for i, d in enumerate(docs)]))
|
123 |
|
124 |
def add_text(history, text):
|
125 |
history = history + [(text, None)]
|
|
|
127 |
|
128 |
def bot(history):
|
129 |
response = infer(history[-1][0], history)
|
130 |
+
print(*response)
|
131 |
print(*memory)
|
132 |
+
sources = [doc.metadata.get("source") for doc in response['sources']]
|
133 |
src_list = '\n'.join(sources)
|
134 |
+
print_this = response['answer'] + "\n\n\n Sources: \n\n\n" + src_list
|
135 |
+
#sources = f"`Sources:`\n\n' + response['sources']"
|
136 |
|
137 |
#history[-1][1] = ""
|
138 |
#for character in response['result']: #print_this:
|
139 |
# history[-1][1] += character
|
140 |
# time.sleep(0.05)
|
141 |
# yield history
|
142 |
+
history[-1][1] = response['answer']
|
143 |
+
return history #, sources
|
144 |
|
145 |
def infer(question, history):
|
146 |
query = question
|
|
|
171 |
)
|
172 |
clear.click(lambda: None, None, chatbot, queue=False)
|
173 |
|
174 |
+
demo.queue().launch()
|
|