Murtuza Saifee commited on
Commit
a99c482
·
1 Parent(s): e8e0f6a

Multiple type of Loader RAG

Browse files
Files changed (3) hide show
  1. .gitignore +171 -0
  2. app.py +116 -0
  3. requirements.txt +9 -0
.gitignore ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # PyPI configuration file
171
+ .pypirc
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain_community.document_loaders import TextLoader, PyPDFLoader, WebBaseLoader
3
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
4
+ from langchain_openai import OpenAIEmbeddings
5
+ from langchain_community.vectorstores import FAISS
6
+ from langchain.llms import OpenAI
7
+ from langchain.chains import RetrievalQA
8
+ import os
9
+ import tempfile
10
+ from dotenv import load_dotenv
11
+
12
+ # Load environment variables
13
+ load_dotenv()
14
+
15
+ os.environ["LANGCHAIN_TRACING_V2"] = "true"
16
+ os.environ["LANGCHAIN_PROJECT"]="Multi Loader RAG"
17
+
18
+
19
+ # Streamlit app title
20
+ st.title("Multi Loader RAG")
21
+
22
+ # File upload and web link input
23
+ st.header("Upload Documents")
24
+ text_file = st.file_uploader("Upload a Text File", type=["txt"])
25
+ pdf_file = st.file_uploader("Upload a PDF File", type=["pdf"])
26
+ web_link = st.text_input("Enter a Web URL")
27
+
28
+ # Load documents function
29
+ def load_documents(text_file, pdf_file, web_link):
30
+ docs = []
31
+
32
+ # Load text file
33
+ if text_file is not None:
34
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp_file:
35
+ tmp_file.write(text_file.getvalue())
36
+ tmp_file_path = tmp_file.name
37
+ text_loader = TextLoader(tmp_file_path)
38
+ docs.extend(text_loader.load())
39
+ os.remove(tmp_file_path)
40
+
41
+ # Load PDF file
42
+ if pdf_file is not None:
43
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
44
+ tmp_file.write(pdf_file.getvalue())
45
+ tmp_file_path = tmp_file.name
46
+ pdf_loader = PyPDFLoader(tmp_file_path)
47
+ docs.extend(pdf_loader.load())
48
+ os.remove(tmp_file_path)
49
+
50
+ # Load web content
51
+ if web_link:
52
+ web_loader = WebBaseLoader([web_link])
53
+ docs.extend(web_loader.load())
54
+
55
+ return docs
56
+
57
+ # Split documents function
58
+ def split_documents(docs, chunk_size, chunk_overlap):
59
+ text_splitter = RecursiveCharacterTextSplitter(
60
+ chunk_size=chunk_size,
61
+ chunk_overlap=chunk_overlap
62
+ )
63
+ return text_splitter.split_documents(docs)
64
+
65
+ # Create FAISS vector store function
66
+ def create_vector_store(splits):
67
+ embeddings = OpenAIEmbeddings()
68
+ vectorstore = FAISS.from_documents(splits, embeddings)
69
+ return vectorstore
70
+
71
+ # Main app logic
72
+ if st.button("Process Documents"):
73
+ if not (text_file or pdf_file or web_link):
74
+ st.error("Please upload at least one document or provide a web link.")
75
+ else:
76
+ with st.spinner("Processing documents..."):
77
+ # Load documents
78
+ documents = load_documents(text_file, pdf_file, web_link)
79
+
80
+ # Split documents
81
+ splits = split_documents(documents, 1000, 300)
82
+
83
+ # Create FAISS vector store
84
+ st.session_state.vector_store = create_vector_store(splits)
85
+
86
+ st.success("Documents processed and FAISS vector store created!")
87
+
88
+ st.header("Get Summary/Answer")
89
+ query = st.text_input("Enter your query")
90
+
91
+ if st.button("Search"):
92
+ if st.session_state.vector_store is None:
93
+ st.error("Please process documents first.")
94
+ elif not query:
95
+ st.error("Please enter a query.")
96
+ else:
97
+ with st.spinner("Searching..."):
98
+ # Create retriever and chain
99
+ retriever = st.session_state.vector_store.as_retriever(
100
+ search_type="similarity",
101
+ search_kwargs={"k": 5}
102
+ )
103
+ llm = OpenAI(temperature=0.6)
104
+ qa_chain = RetrievalQA.from_chain_type(
105
+ llm=llm,
106
+ chain_type="stuff",
107
+ retriever=retriever,
108
+ return_source_documents=True
109
+ )
110
+
111
+ # Execute query
112
+ result = qa_chain({"query": query})
113
+
114
+ # Display the result
115
+ st.markdown("### Answer:")
116
+ st.write(result["result"])
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ langchain
2
+ python-dotenv
3
+ langchain-openai
4
+ langchain-community
5
+ langchain-core
6
+ bs4
7
+ faiss-cpu
8
+ streamlit
9
+ pypdf