Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
+
from langchain.document_loaders import PyPDFLoader
|
| 7 |
+
from langchain.chains import RetrievalQA
|
| 8 |
+
from langchain.llms import OpenAI
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
# Load API key from Hugging Face secrets
|
| 12 |
+
load_dotenv()
|
| 13 |
+
OPENAI_API_KEY = os.getenv("GROQ_API_KEY")
|
| 14 |
+
if not OPENAI_API_KEY:
|
| 15 |
+
st.error("API key is not set. Please set GROQ_API_KEY in Hugging Face secrets.")
|
| 16 |
+
|
| 17 |
+
# Configure OpenAI API key
|
| 18 |
+
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
| 19 |
+
|
| 20 |
+
# Streamlit app UI
|
| 21 |
+
st.title("RAG-based Application")
|
| 22 |
+
st.write("Upload a PDF, ask questions, and get answers based on the document content.")
|
| 23 |
+
|
| 24 |
+
# Upload PDF file
|
| 25 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 26 |
+
|
| 27 |
+
if uploaded_file:
|
| 28 |
+
# Load PDF
|
| 29 |
+
loader = PyPDFLoader(uploaded_file)
|
| 30 |
+
documents = loader.load()
|
| 31 |
+
|
| 32 |
+
# Split the text into chunks
|
| 33 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 34 |
+
chunks = text_splitter.split_documents(documents)
|
| 35 |
+
|
| 36 |
+
# Tokenize and store data in FAISS vector database
|
| 37 |
+
st.write("Processing the document and creating vector database...")
|
| 38 |
+
embeddings = OpenAIEmbeddings()
|
| 39 |
+
vector_db = FAISS.from_documents(chunks, embeddings)
|
| 40 |
+
|
| 41 |
+
# Save vector database
|
| 42 |
+
faiss_file = "vector_store.faiss"
|
| 43 |
+
vector_db.save_local(faiss_file)
|
| 44 |
+
st.success(f"Vector database saved as {faiss_file}.")
|
| 45 |
+
|
| 46 |
+
# Question-Answer Retrieval
|
| 47 |
+
st.write("You can now ask questions about the document.")
|
| 48 |
+
query = st.text_input("Enter your question:")
|
| 49 |
+
|
| 50 |
+
if query:
|
| 51 |
+
# Initialize QA Chain
|
| 52 |
+
retriever = vector_db.as_retriever()
|
| 53 |
+
llm = OpenAI(model="text-davinci-003", temperature=0.7)
|
| 54 |
+
qa_chain = RetrievalQA(llm=llm, retriever=retriever)
|
| 55 |
+
|
| 56 |
+
# Get the answer
|
| 57 |
+
with st.spinner("Generating answer..."):
|
| 58 |
+
answer = qa_chain.run(query)
|
| 59 |
+
st.success("Answer:")
|
| 60 |
+
st.write(answer)
|
| 61 |
+
|
| 62 |
+
# Deployment instructions
|
| 63 |
+
st.write("To deploy this app on Hugging Face, use the following command:")
|
| 64 |
+
st.code("huggingface-cli login && huggingface-cli deploy --app-dir <your_streamlit_directory>")
|