Spaces:
Sleeping
Sleeping
sapatevaibhav
commited on
Commit
·
c7a656d
1
Parent(s):
7922b0e
created app.py
Browse files- app.py +65 -0
- app/__init__.py +0 -0
- app/embed_and_store.py +2 -2
- app/rag.py +1 -4
- app/ui.py +51 -51
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from app.pdf_to_image import convert_pdf_to_images
|
| 4 |
+
from app.rag import query_rag
|
| 5 |
+
from app.embed_and_store import embed_all_images
|
| 6 |
+
|
| 7 |
+
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 8 |
+
pdf_dir = os.path.join(base_dir, "data", "pdfs")
|
| 9 |
+
image_dir = os.path.join(base_dir, "data", "images")
|
| 10 |
+
|
| 11 |
+
os.makedirs(pdf_dir, exist_ok=True)
|
| 12 |
+
os.makedirs(image_dir, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
st.title("🧠 Image-based PDF RAG")
|
| 15 |
+
|
| 16 |
+
if "processed_files" not in st.session_state:
|
| 17 |
+
st.session_state.processed_files = set()
|
| 18 |
+
if "image_paths" not in st.session_state:
|
| 19 |
+
st.session_state.image_paths = []
|
| 20 |
+
|
| 21 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 22 |
+
if uploaded_file:
|
| 23 |
+
pdf_path = os.path.join(pdf_dir, uploaded_file.name)
|
| 24 |
+
|
| 25 |
+
if uploaded_file.name not in st.session_state.processed_files:
|
| 26 |
+
with st.spinner("Processing PDF and creating embeddings..."):
|
| 27 |
+
with open(pdf_path, "wb") as f:
|
| 28 |
+
f.write(uploaded_file.read())
|
| 29 |
+
|
| 30 |
+
image_paths = convert_pdf_to_images(pdf_path, image_dir)
|
| 31 |
+
|
| 32 |
+
for image_path in image_paths:
|
| 33 |
+
embed_all_images(image_path)
|
| 34 |
+
|
| 35 |
+
st.session_state.processed_files.add(uploaded_file.name)
|
| 36 |
+
st.session_state.image_paths = image_paths
|
| 37 |
+
|
| 38 |
+
st.success(
|
| 39 |
+
f"PDF processed! {len(image_paths)} pages converted and embedded for search."
|
| 40 |
+
)
|
| 41 |
+
else:
|
| 42 |
+
st.success("PDF already processed. Ready to query.")
|
| 43 |
+
image_paths = st.session_state.image_paths
|
| 44 |
+
else:
|
| 45 |
+
image_paths = []
|
| 46 |
+
|
| 47 |
+
# Query UI
|
| 48 |
+
if image_paths:
|
| 49 |
+
st.subheader("Query PDF Content")
|
| 50 |
+
page_options = [f"Page {i+1}" for i in range(len(image_paths))]
|
| 51 |
+
selected_page = st.selectbox("Select a page to query:", page_options)
|
| 52 |
+
|
| 53 |
+
if page_options:
|
| 54 |
+
page_idx = page_options.index(selected_page)
|
| 55 |
+
selected_image = image_paths[page_idx]
|
| 56 |
+
|
| 57 |
+
with st.expander("📄 View selected page"):
|
| 58 |
+
st.image(selected_image, caption=f"Page {page_idx+1}")
|
| 59 |
+
|
| 60 |
+
query = st.text_input("🔍 Ask a question about this page:")
|
| 61 |
+
if query and st.button("Search"):
|
| 62 |
+
with st.spinner("Searching with Gemini..."):
|
| 63 |
+
result = query_rag(selected_image, query)
|
| 64 |
+
st.markdown("### 💬 Answer")
|
| 65 |
+
st.write(result)
|
app/__init__.py
ADDED
|
File without changes
|
app/embed_and_store.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
# app/embed_and_store.py
|
| 2 |
|
| 3 |
import os
|
| 4 |
-
from embed import get_image_embedding
|
| 5 |
-
from chroma_utils import add_embedding
|
| 6 |
|
| 7 |
# Create absolute paths for data directories
|
| 8 |
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
| 1 |
# app/embed_and_store.py
|
| 2 |
|
| 3 |
import os
|
| 4 |
+
from app.embed import get_image_embedding
|
| 5 |
+
from app.chroma_utils import add_embedding
|
| 6 |
|
| 7 |
# Create absolute paths for data directories
|
| 8 |
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
app/rag.py
CHANGED
|
@@ -1,7 +1,4 @@
|
|
| 1 |
-
from
|
| 2 |
-
from chroma_utils import search_embedding
|
| 3 |
-
# app/rag.py
|
| 4 |
-
from gemini_vision import ask_image_question
|
| 5 |
|
| 6 |
def query_rag(image_path: str, question: str) -> str:
|
| 7 |
return ask_image_question(image_path, question)
|
|
|
|
| 1 |
+
from app.gemini_vision import ask_image_question
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def query_rag(image_path: str, question: str) -> str:
|
| 4 |
return ask_image_question(image_path, question)
|
app/ui.py
CHANGED
|
@@ -1,65 +1,65 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
from pdf_to_image import convert_pdf_to_images
|
| 4 |
-
from rag import query_rag
|
| 5 |
-
from embed_and_store import embed_all_images
|
| 6 |
|
| 7 |
-
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 8 |
-
pdf_dir = os.path.join(base_dir, "data", "pdfs")
|
| 9 |
-
image_dir = os.path.join(base_dir, "data", "images")
|
| 10 |
|
| 11 |
-
os.makedirs(pdf_dir, exist_ok=True)
|
| 12 |
-
os.makedirs(image_dir, exist_ok=True)
|
| 13 |
|
| 14 |
-
st.title("🧠 Image-based PDF RAG")
|
| 15 |
|
| 16 |
-
if "processed_files" not in st.session_state:
|
| 17 |
-
|
| 18 |
-
if "image_paths" not in st.session_state:
|
| 19 |
-
|
| 20 |
|
| 21 |
-
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 22 |
-
if uploaded_file:
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
else:
|
| 45 |
-
|
| 46 |
|
| 47 |
-
# Query UI
|
| 48 |
-
if image_paths:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# import os
|
| 3 |
+
# from pdf_to_image import convert_pdf_to_images
|
| 4 |
+
# from rag import query_rag
|
| 5 |
+
# from embed_and_store import embed_all_images
|
| 6 |
|
| 7 |
+
# base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 8 |
+
# pdf_dir = os.path.join(base_dir, "data", "pdfs")
|
| 9 |
+
# image_dir = os.path.join(base_dir, "data", "images")
|
| 10 |
|
| 11 |
+
# os.makedirs(pdf_dir, exist_ok=True)
|
| 12 |
+
# os.makedirs(image_dir, exist_ok=True)
|
| 13 |
|
| 14 |
+
# st.title("🧠 Image-based PDF RAG")
|
| 15 |
|
| 16 |
+
# if "processed_files" not in st.session_state:
|
| 17 |
+
# st.session_state.processed_files = set()
|
| 18 |
+
# if "image_paths" not in st.session_state:
|
| 19 |
+
# st.session_state.image_paths = []
|
| 20 |
|
| 21 |
+
# uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 22 |
+
# if uploaded_file:
|
| 23 |
+
# pdf_path = os.path.join(pdf_dir, uploaded_file.name)
|
| 24 |
|
| 25 |
+
# if uploaded_file.name not in st.session_state.processed_files:
|
| 26 |
+
# with st.spinner("Processing PDF and creating embeddings..."):
|
| 27 |
+
# with open(pdf_path, "wb") as f:
|
| 28 |
+
# f.write(uploaded_file.read())
|
| 29 |
|
| 30 |
+
# image_paths = convert_pdf_to_images(pdf_path, image_dir)
|
| 31 |
|
| 32 |
+
# for image_path in image_paths:
|
| 33 |
+
# embed_all_images(image_path)
|
| 34 |
|
| 35 |
+
# st.session_state.processed_files.add(uploaded_file.name)
|
| 36 |
+
# st.session_state.image_paths = image_paths
|
| 37 |
|
| 38 |
+
# st.success(
|
| 39 |
+
# f"PDF processed! {len(image_paths)} pages converted and embedded for search."
|
| 40 |
+
# )
|
| 41 |
+
# else:
|
| 42 |
+
# st.success("PDF already processed. Ready to query.")
|
| 43 |
+
# image_paths = st.session_state.image_paths
|
| 44 |
+
# else:
|
| 45 |
+
# image_paths = []
|
| 46 |
|
| 47 |
+
# # Query UI
|
| 48 |
+
# if image_paths:
|
| 49 |
+
# st.subheader("Query PDF Content")
|
| 50 |
+
# page_options = [f"Page {i+1}" for i in range(len(image_paths))]
|
| 51 |
+
# selected_page = st.selectbox("Select a page to query:", page_options)
|
| 52 |
|
| 53 |
+
# if page_options:
|
| 54 |
+
# page_idx = page_options.index(selected_page)
|
| 55 |
+
# selected_image = image_paths[page_idx]
|
| 56 |
|
| 57 |
+
# with st.expander("📄 View selected page"):
|
| 58 |
+
# st.image(selected_image, caption=f"Page {page_idx+1}")
|
| 59 |
|
| 60 |
+
# query = st.text_input("🔍 Ask a question about this page:")
|
| 61 |
+
# if query and st.button("Search"):
|
| 62 |
+
# with st.spinner("Searching with Gemini..."):
|
| 63 |
+
# result = query_rag(selected_image, query)
|
| 64 |
+
# st.markdown("### 💬 Answer")
|
| 65 |
+
# st.write(result)
|