Vinit710 commited on
Commit
91d9fd7
Β·
verified Β·
1 Parent(s): 9b49e94

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +36 -24
src/streamlit_app.py CHANGED
@@ -1,47 +1,59 @@
1
  import streamlit as st
2
  from langchain.document_loaders import TextLoader
 
3
  from langchain.text_splitter import RecursiveCharacterTextSplitter
4
  from langchain.embeddings import HuggingFaceEmbeddings
5
  from langchain.vectorstores import FAISS
6
  from langchain.chains import RetrievalQA
7
  from langchain.llms import HuggingFaceHub
 
 
8
 
9
  @st.cache_resource
10
- def load_vector_store():
11
  # Load and chunk the document
12
- loader = TextLoader("data/sample.txt") # Make sure this file exists
13
  documents = loader.load()
14
 
15
  splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
16
  chunks = splitter.split_documents(documents)
17
 
18
- # Create embeddings and vector store
19
  embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
20
  db = FAISS.from_documents(chunks, embedding_model)
21
  return db
22
 
23
  def main():
24
- st.title("πŸ“„ Ask Your Document")
25
- st.write("Powered by LangChain + Hugging Face")
26
-
27
- query = st.text_input("Enter your question:")
28
- if query:
29
- db = load_vector_store()
30
-
31
- # Create RAG chain with Hugging Face model
32
- qa_chain = RetrievalQA.from_chain_type(
33
- llm=HuggingFaceHub(
34
- repo_id="google/flan-t5-base",
35
- model_kwargs={"temperature": 0.5, "max_length": 256}
36
- ),
37
- retriever=db.as_retriever(),
38
- return_source_documents=True
39
- )
40
-
41
- result = qa_chain.run(query)
42
-
43
- st.write("### πŸ“Œ Answer")
44
- st.write(result)
 
 
 
 
 
 
 
 
 
45
 
46
  if __name__ == "__main__":
47
  main()
 
1
  import streamlit as st
2
  from langchain.document_loaders import TextLoader
3
+ from langchain.document_loaders import UnstructuredFileLoader
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
  from langchain.embeddings import HuggingFaceEmbeddings
6
  from langchain.vectorstores import FAISS
7
  from langchain.chains import RetrievalQA
8
  from langchain.llms import HuggingFaceHub
9
+ import tempfile
10
+ import os
11
 
12
  @st.cache_resource
13
+ def load_vector_store(file_path):
14
  # Load and chunk the document
15
+ loader = TextLoader(file_path)
16
  documents = loader.load()
17
 
18
  splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
19
  chunks = splitter.split_documents(documents)
20
 
21
+ # Create embeddings and store in FAISS
22
  embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
23
  db = FAISS.from_documents(chunks, embedding_model)
24
  return db
25
 
26
  def main():
27
+ st.title("πŸ“„ Ask Questions About Your Document")
28
+ st.write("Upload a `.txt` file and ask anything!")
29
+
30
+ uploaded_file = st.file_uploader("Upload a text file", type=["txt"])
31
+
32
+ if uploaded_file:
33
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp_file:
34
+ tmp_file.write(uploaded_file.read())
35
+ tmp_path = tmp_file.name
36
+
37
+ db = load_vector_store(tmp_path)
38
+
39
+ query = st.text_input("Enter your question:")
40
+ if query:
41
+ qa_chain = RetrievalQA.from_chain_type(
42
+ llm=HuggingFaceHub(
43
+ repo_id="google/flan-t5-base",
44
+ model_kwargs={"temperature": 0.5, "max_length": 256}
45
+ ),
46
+ retriever=db.as_retriever(),
47
+ return_source_documents=True
48
+ )
49
+
50
+ result = qa_chain.run(query)
51
+
52
+ st.write("### πŸ“Œ Answer")
53
+ st.write(result)
54
+
55
+ # Clean up temp file
56
+ os.remove(tmp_path)
57
 
58
  if __name__ == "__main__":
59
  main()