import streamlit as st
from dotenv import load_dotenv
from ragflow_sdk import RAGFlow
import os
from helper_functions import parse_documents 

parse = True

# Load environment variables
load_dotenv()

# Get API key and base URL
RAGFLOW_API_KEY = os.getenv("RAGFLOW_API_KEY")
BASE_URL = os.getenv("BASE_URL")
documents_folder ="./documents/"

rag_object = RAGFlow(api_key=RAGFLOW_API_KEY, base_url=BASE_URL)

dataset = rag_object.list_datasets()[0]

#dataset = rag_object.create_dataset(name="Documents")
st.set_page_config(layout="wide")

st.markdown("""
    <style>
        /* Reduce sidebar width */
        [data-testid="stSidebar"] {
            min-width: 230px;
            max-width: 230px;
        }
        /* Optional: Adjust main content padding */
        .main {
            padding-left: 200px;
        }
    </style>
""", unsafe_allow_html=True)

st.markdown("""
<style>
.upload-box {
    border: 2px dashed #ccc;
    padding: 10px;
    border-radius: 10px;
    background-color: #f9f9f9;
}
.dropdown-label {
    font-weight: bold;
    color: #444;
}
</style>
""", unsafe_allow_html=True)

st.title("📤 Document Comparison Tool with RAGFlow📄")

col1, col2 = st.columns(2)

# ---------- Document 1 ----------
with col1:
    st.header("📘 Document 1")
    option1 = st.radio("Choose input method", ["Upload New"], key="doc1_method")

    if option1 == "Upload New":
        uploaded1 = st.file_uploader("Upload the First PDF", type=["pdf"], key="upload1")
        if uploaded1:
            file_bytes_1 = uploaded1.read()
            #dataset.upload_documents([{"displayed_name":uploaded1.name,"blob":file_bytes_1}])
            #file1_path = save_uploaded_file(uploaded1, "./documents/")
            #print(file_bytes_1)
            st.session_state["file1"] = uploaded1.name
            st.success(f"✅ Ready to {uploaded1.name}")

with col2:
    st.header("📗 Document 2")
    option2 = st.radio("Choose input method", ["Upload New"], key="doc2_method")

    if option2 == "Upload New":
        uploaded2 = st.file_uploader("Upload the Second PDF", type=["pdf"], key="upload2")
        if uploaded2:
            file_bytes_2 = uploaded2.read()
            #dataset.upload_documents([{"displayed_name":uploaded2.name,"blob":file_bytes_2}])
            st.session_state["file2"] = uploaded2.name
            #file1_path = save_uploaded_file(uploaded1, "./documents/")
            #print(file_bytes_2)
            st.success(f"✅ Ready to {uploaded2.name}")

if st.button("Compare documents"):
    if uploaded1 and uploaded2:
        dataset.upload_documents([{"displayed_name":uploaded1.name,"blob":file_bytes_1}])
        dataset.upload_documents([{"displayed_name":uploaded2.name,"blob":file_bytes_2}])
        with st.status("📚 Sit back & Relax... We are Processing Documents...", state="running", expanded=True):
            parse_output = parse_documents(dataset)
        #parse_output = parse_documents(dataset)
        st.write("Both documents are uploaded. Proceeding with submission.")
        
    else:
        st.warning("Please upload both documents before submitting.")

this is my code it will be present in file with streamlit.


I have created few more code for doing embedding & getting the chunks.

def fetch_chunks_for_documents(dataset, doc_names):
    def get_chunks(doc_name):
        docs = dataset.list_documents(keywords=doc_name)
        if not docs:
            return []
        return [chunk.content for chunk in docs[0].list_chunks()]

    with concurrent.futures.ThreadPoolExecutor() as executor:
        futures = {executor.submit(get_chunks, name): name for name in doc_names}
        results = {name: future.result() for future, name in futures.items()}

    return results


doc_names = ["23NYCRR500_0.pdf", "Minnesota Deepfake Ban.pdf"]
chunks_by_doc = fetch_chunks_for_documents(dataset, doc_names)

chunks1 = chunks_by_doc["23NYCRR500_0.pdf"]
chunks2 = chunks_by_doc["Minnesota Deepfake Ban.pdf"]


def get_embedding(text: str):
    response = client.embeddings.create(
        input=[text],
        model=EMBEDDING_MODEL_DEPLOYMENT,
    )
    return response.data[0].embedding

def generate_embeddings_for_documents(doc_chunks_map):
    def get_embeddings(chunks):
        return [get_embedding(sentence) for sentence in chunks]

    with ThreadPoolExecutor() as executor:
        futures = {
            executor.submit(get_embeddings, chunks): doc_name
            for doc_name, chunks in doc_chunks_map.items()
        }
        results = {
            doc_name: future.result()
            for future, doc_name in futures.items()
        }

    return results

doc_chunks_map = {
    "23NYCRR500_0.pdf": chunks1,
    "Minnesota Deepfake Ban.pdf": chunks2
}

embeddings_by_doc = generate_embeddings_for_documents(doc_chunks_map)

embeddings_1 = embeddings_by_doc["23NYCRR500_0.pdf"]
embeddings_2 = embeddings_by_doc["Minnesota Deepfake Ban.pdf"]

this is same .pdf I mentioned here, it should auto pick from uploaded with streamlit.

Integreate this code with above one.