koler commited on
Commit
574ab93
·
verified ·
1 Parent(s): 978f862

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +126 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,128 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import faiss
3
+ import os
4
+ from io import BytesIO
5
+ from docx import Document
6
+ import numpy as np
7
+ from langchain_community.document_loaders import WebBaseLoader
8
+ from PyPDF2 import PdfReader
9
+ from langchain.chains import RetrievalQA
10
+ from langchain.text_splitter import CharacterTextSplitter
11
+ from langchain_community.embeddings import HuggingFaceEmbeddings
12
+ from langchain_community.vectorstores import FAISS
13
+ from langchain_community.docstore.in_memory import InMemoryDocstore
14
+ from langchain_huggingface import HuggingFaceEndpoint
15
+
16
+ from secret_api_keys import huggingface_api_key # Set the Hugging Face Hub API token as an environment variable
17
+ os.environ['HUGGINGFACEHUB_API_TOKEN'] = huggingface_api_key
18
+
19
+ def process_input(input_type, input_data):
20
+ """Processes different input types and returns a vectorstore."""
21
+ loader = None
22
+ if input_type == "Link":
23
+ loader = WebBaseLoader(input_data)
24
+ documents = loader.load()
25
+ elif input_type == "PDF":
26
+ if isinstance(input_data, BytesIO):
27
+ pdf_reader = PdfReader(input_data)
28
+ elif isinstance(input_data, UploadedFile):
29
+ pdf_reader = PdfReader(BytesIO(input_data.read()))
30
+ else:
31
+ raise ValueError("Invalid input data for PDF")
32
+ text = ""
33
+ for page in pdf_reader.pages:
34
+ text += page.extract_text()
35
+ documents = text
36
+ elif input_type == "Text":
37
+ if isinstance(input_data, str):
38
+ documents = input_data # Input is already a text string
39
+ else:
40
+ raise ValueError("Expected a string for 'Text' input type.")
41
+ elif input_type == "DOCX":
42
+ if isinstance(input_data, BytesIO):
43
+ doc = Document(input_data)
44
+ elif isinstance(input_data, UploadedFile):
45
+ doc = Document(BytesIO(input_data.read()))
46
+ else:
47
+ raise ValueError("Invalid input data for DOCX")
48
+ text = "\n".join([para.text for para in doc.paragraphs])
49
+ documents = text
50
+ elif input_type == "TXT":
51
+ if isinstance(input_data, BytesIO):
52
+ text = input_data.read().decode('utf-8')
53
+ elif isinstance(input_data, UploadedFile):
54
+ text = str(input_data.read().decode('utf-8'))
55
+ else:
56
+ raise ValueError("Invalid input data for TXT")
57
+ documents = text
58
+ else:
59
+ raise ValueError("Unsupported input type")
60
+
61
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
62
+ if input_type == "Link":
63
+ texts = text_splitter.split_documents(documents)
64
+ texts = [ str(doc.page_content) for doc in texts ] # Access page_content from each Document
65
+ else:
66
+ texts = text_splitter.split_text(documents)
67
+
68
+ model_name = "sentence-transformers/all-mpnet-base-v2"
69
+ model_kwargs = {'device': 'cpu'}
70
+ encode_kwargs = {'normalize_embeddings': False}
71
+
72
+ hf_embeddings = HuggingFaceEmbeddings(
73
+ model_name=model_name,
74
+ model_kwargs=model_kwargs,
75
+ encode_kwargs=encode_kwargs
76
+ )
77
+ # Create FAISS index
78
+ sample_embedding = np.array(hf_embeddings.embed_query("sample text"))
79
+ dimension = sample_embedding.shape[0]
80
+ index = faiss.IndexFlatL2(dimension)
81
+ # Create FAISS vector store with the embedding function
82
+ vector_store = FAISS(
83
+ embedding_function=hf_embeddings.embed_query,
84
+ index=index,
85
+ docstore=InMemoryDocstore(),
86
+ index_to_docstore_id={},
87
+ )
88
+ vector_store.add_texts(texts) # Add documents to the vector store
89
+ return vector_store
90
+
91
+ def answer_question(vectorstore, query):
92
+ """Answers a question based on the provided vectorstore."""
93
+ llm = HuggingFaceEndpoint(repo_id= 'meta-llama/Meta-Llama-3-8B-Instruct',
94
+ token = huggingface_api_key, temperature= 0.6)
95
+ qa = RetrievalQA.from_chain_type(llm=llm, retriever=vectorstore.as_retriever())
96
+
97
+ answer = qa({"query": query})
98
+ return answer
99
+
100
+ def main():
101
+ st.title("RAG Q&A App")
102
+ input_type = st.selectbox("Input Type", ["Link", "PDF", "Text", "DOCX", "TXT"])
103
+ if input_type == "Link":
104
+ number_input = st.number_input(min_value=1, max_value=20, step=1, label = "Enter the number of Links")
105
+ input_data = []
106
+ for i in range(number_input):
107
+ url = st.sidebar.text_input(f"URL {i+1}")
108
+ input_data.append(url)
109
+ elif input_type == "Text":
110
+ input_data = st.text_input("Enter the text")
111
+ elif input_type == 'PDF':
112
+ input_data = st.file_uploader("Upload a PDF file", type=["pdf"])
113
+ elif input_type == 'TXT':
114
+ input_data = st.file_uploader("Upload a text file", type=['txt'])
115
+ elif input_type == 'DOCX':
116
+ input_data = st.file_uploader("Upload a DOCX file", type=[ 'docx', 'doc'])
117
+ if st.button("Proceed"):
118
+ # st.write(process_input(input_type, input_data))
119
+ vectorstore = process_input(input_type, input_data)
120
+ st.session_state["vectorstore"] = vectorstore
121
+ if "vectorstore" in st.session_state:
122
+ query = st.text_input("Ask your question")
123
+ if st.button("Submit"):
124
+ answer = answer_question(st.session_state["vectorstore"], query)
125
+ st.write(answer)
126
 
127
+ if __name__ == "__main__":
128
+ main()