muhammadshaheryar commited on
Commit
1205153
·
verified ·
1 Parent(s): dda64b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -32
app.py CHANGED
@@ -1,43 +1,28 @@
1
- # Continue with the rest of the code
2
- from langchain.chains import RetrievalQA
3
- from langchain.document_loaders import TextLoader
4
  from langchain.embeddings import SentenceTransformerEmbeddings
5
  from langchain.vectorstores import FAISS
6
  from transformers import pipeline
7
 
 
 
 
8
 
9
-
10
- # Paste your data here
11
- data = """
12
- Enter your text data here. For example:
13
- """
14
-
15
- # Split data into chunks for embedding
16
  def chunk_text(text, chunk_size=500):
17
  words = text.split()
18
  chunks = [" ".join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
19
  return chunks
20
 
21
- # Prepare the text chunks
22
- text_chunks = chunk_text(data)
23
-
24
- # Generate embeddings and index the data
25
- embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
26
- vectorstore = FAISS.from_texts(text_chunks, embeddings)
27
-
28
- # Load a simple LLM (Hugging Face model)
29
- from transformers import pipeline
30
- qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
31
-
32
- # Define a function to perform QA
33
- def answer_question(question):
34
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
35
- relevant_docs = retriever.get_relevant_documents(question)
36
- context = " ".join([doc.page_content for doc in relevant_docs])
37
- answer = qa_pipeline(question=question, context=context)
38
- return answer["answer"]
39
-
40
- # Ask a question
41
- import sentence_transformers
42
- print("sentence-transformers is installed successfully!")
43
-
 
1
+ import streamlit as st
 
 
2
  from langchain.embeddings import SentenceTransformerEmbeddings
3
  from langchain.vectorstores import FAISS
4
  from transformers import pipeline
5
 
6
+ # Initialize embedding model
7
+ embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
8
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
9
 
 
 
 
 
 
 
 
10
  def chunk_text(text, chunk_size=500):
11
  words = text.split()
12
  chunks = [" ".join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
13
  return chunks
14
 
15
+ # Streamlit app
16
+ st.title("Simple RAG Application")
17
+ data = st.text_area("Paste your text here:")
18
+ if data:
19
+ text_chunks = chunk_text(data)
20
+ vectorstore = FAISS.from_texts(text_chunks, embeddings)
 
 
 
 
 
 
 
21
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
22
+
23
+ question = st.text_input("Ask a question:")
24
+ if question:
25
+ relevant_docs = retriever.get_relevant_documents(question)
26
+ context = " ".join([doc.page_content for doc in relevant_docs])
27
+ answer = qa_pipeline(question=question, context=context)
28
+ st.write("Answer:", answer["answer"])