Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from utils.pdf_processing import extract_text_from_pdf, split_into_chunks
|
| 4 |
+
from utils.embeddings import generate_embeddings
|
| 5 |
+
from utils.database import initialize_faiss, insert_embeddings, search_embeddings
|
| 6 |
+
|
| 7 |
+
def main():
|
| 8 |
+
st.title("PDF Retrieval-Augmented Generation (RAG) Application")
|
| 9 |
+
|
| 10 |
+
# Initialize FAISS index
|
| 11 |
+
faiss_index = initialize_faiss()
|
| 12 |
+
|
| 13 |
+
# Upload PDF file
|
| 14 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
|
| 15 |
+
if uploaded_file is not None:
|
| 16 |
+
# Extract text from the uploaded PDF
|
| 17 |
+
with st.spinner("Processing PDF..."):
|
| 18 |
+
text = extract_text_from_pdf(uploaded_file)
|
| 19 |
+
chunks = split_into_chunks(text)
|
| 20 |
+
|
| 21 |
+
# Generate embeddings for text chunks
|
| 22 |
+
with st.spinner("Generating embeddings..."):
|
| 23 |
+
embeddings = generate_embeddings(chunks)
|
| 24 |
+
|
| 25 |
+
# Insert embeddings into FAISS index
|
| 26 |
+
with st.spinner("Inserting embeddings into FAISS..."):
|
| 27 |
+
insert_embeddings(faiss_index, embeddings, chunks)
|
| 28 |
+
|
| 29 |
+
st.success("PDF processed and embeddings stored successfully!")
|
| 30 |
+
|
| 31 |
+
# Search functionality
|
| 32 |
+
query = st.text_input("Enter a query to search:")
|
| 33 |
+
if query:
|
| 34 |
+
with st.spinner("Searching..."):
|
| 35 |
+
query_embedding = generate_embeddings([query])[0]
|
| 36 |
+
results = search_embeddings(faiss_index, query_embedding)
|
| 37 |
+
st.write("Results:", results)
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
main()
|