Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install required libraries
|
| 2 |
+
!pip install transformers langchain sentence-transformers streamlit
|
| 3 |
+
!pip install -U langchain-community # Install langchain-community
|
| 4 |
+
!pip install faiss-cpu # Install faiss-cpu
|
| 5 |
+
|
| 6 |
+
# Continue with the rest of the code
|
| 7 |
+
from langchain.chains import RetrievalQA
|
| 8 |
+
from langchain.document_loaders import TextLoader
|
| 9 |
+
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 10 |
+
from langchain.vectorstores import FAISS
|
| 11 |
+
from transformers import pipeline
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Paste your data here
|
| 16 |
+
data = """
|
| 17 |
+
Enter your text data here. For example:
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
# Split data into chunks for embedding
|
| 21 |
+
def chunk_text(text, chunk_size=500):
|
| 22 |
+
words = text.split()
|
| 23 |
+
chunks = [" ".join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 24 |
+
return chunks
|
| 25 |
+
|
| 26 |
+
# Prepare the text chunks
|
| 27 |
+
text_chunks = chunk_text(data)
|
| 28 |
+
|
| 29 |
+
# Generate embeddings and index the data
|
| 30 |
+
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 31 |
+
vectorstore = FAISS.from_texts(text_chunks, embeddings)
|
| 32 |
+
|
| 33 |
+
# Load a simple LLM (Hugging Face model)
|
| 34 |
+
from transformers import pipeline
|
| 35 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
| 36 |
+
|
| 37 |
+
# Define a function to perform QA
|
| 38 |
+
def answer_question(question):
|
| 39 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 40 |
+
relevant_docs = retriever.get_relevant_documents(question)
|
| 41 |
+
context = " ".join([doc.page_content for doc in relevant_docs])
|
| 42 |
+
answer = qa_pipeline(question=question, context=context)
|
| 43 |
+
return answer["answer"]
|
| 44 |
+
|
| 45 |
+
# Ask a question
|
| 46 |
+
print("Paste the text and ask your question.")
|
| 47 |
+
question = input("Your question: ")
|
| 48 |
+
answer = answer_question(question)
|
| 49 |
+
print("Answer:", answer)
|