Vinit710 commited on
Commit
1cb4585
·
verified ·
1 Parent(s): 63fc2fc

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +42 -0
  2. requirements.txt +6 -3
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ from langchain.document_loaders import TextLoader
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
+
10
+ @st.cache_resource
11
+ def load_vector_store():
12
+ loader = TextLoader("data/sample.txt")
13
+ documents = loader.load()
14
+
15
+ splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
16
+ chunks = splitter.split_documents(documents)
17
+
18
+ embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
19
+ db = FAISS.from_documents(chunks, embedding_model)
20
+ return db
21
+
22
+ def main():
23
+ st.title("📄 Ask Your Document (RAG with LangChain + Hugging Face)")
24
+ st.write("Upload a document, ask questions, and get answers powered by open-source LLMs!")
25
+
26
+ query = st.text_input("Enter your question:")
27
+ if query:
28
+ db = load_vector_store()
29
+ qa_chain = RetrievalQA.from_chain_type(
30
+ llm=HuggingFaceHub(
31
+ repo_id="google/flan-t5-base",
32
+ model_kwargs={"temperature": 0.5, "max_length": 256}
33
+ ),
34
+ retriever=db.as_retriever(),
35
+ return_source_documents=True
36
+ )
37
+ result = qa_chain.run(query)
38
+ st.write("### 📌 Answer")
39
+ st.write(result)
40
+
41
+ if __name__ == "__main__":
42
+ main()
requirements.txt CHANGED
@@ -1,3 +1,6 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
1
+
2
+ streamlit
3
+ langchain
4
+ openai
5
+ faiss-cpu
6
+ sentence-transformers