File size: 4,793 Bytes
c1bd5b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import tempfile
import streamlit as st
from langchain_community.document_loaders import PyPDFLoader, Docx2txtLoader, UnstructuredMarkdownLoader, WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain_community.chat_models import ChatOpenAI

# Streamlit App Title
st.title("📄 DeepSeek-Powered RAG Chatbot")

# Step 1: Input API Key
api_key = st.text_input("🔑 Enter your DeepSeek API Key:", type="password")

if api_key:
    # Set the API key as an environment variable (optional)
    os.environ["DEEPSEEK_API_KEY"] = api_key

    # Step 2: Upload Document or Enter Web Link
    input_option = st.radio("Choose input type:", ("Upload Document", "Web Link"))

    if input_option == "Upload Document":
        uploaded_file = st.file_uploader("📂 Upload a document", type=["pdf", "docx", "md"])
    else:
        web_link = st.text_input("🌐 Enter the web link:")

    # Use session state to persist the vector_store
    if "vector_store" not in st.session_state:
        st.session_state.vector_store = None

    if (input_option == "Upload Document" and uploaded_file and st.session_state.vector_store is None) or \
       (input_option == "Web Link" and web_link and st.session_state.vector_store is None):
        try:
            with st.spinner("Processing document..."):
                if input_option == "Upload Document":
                    # Save the uploaded file temporarily
                    with tempfile.NamedTemporaryFile(delete=False, suffix=f".{uploaded_file.name.split('.')[-1]}") as tmp_file:
                        tmp_file.write(uploaded_file.getvalue())
                        tmp_file_path = tmp_file.name

                    # Load the document based on file type
                    if uploaded_file.name.endswith(".pdf"):
                        loader = PyPDFLoader(tmp_file_path)
                    elif uploaded_file.name.endswith(".docx"):
                        loader = Docx2txtLoader(tmp_file_path)
                    elif uploaded_file.name.endswith(".md"):
                        loader = UnstructuredMarkdownLoader(tmp_file_path)
                    else:
                        st.error("Unsupported file type!")
                        st.stop()

                    documents = loader.load()

                    # Remove the temporary file
                    os.unlink(tmp_file_path)
                else:
                    # Load the web page content
                    loader = WebBaseLoader(web_link)
                    documents = loader.load()

                # Split the document into chunks
                text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
                chunks = text_splitter.split_documents(documents)

                # Generate embeddings and store them in a vector database
                embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
                st.session_state.vector_store = FAISS.from_documents(chunks, embeddings)

            st.success("Document processed successfully!")
        except Exception as e:
            st.error(f"Error processing document: {e}")
            st.stop()

    # Step 3: Ask Questions About the Document
    if st.session_state.vector_store:
        st.subheader("💬 Chat with Your Document")
        user_query = st.text_input("Ask a question:")

        if user_query:
            try:
                # Set up the RAG pipeline with DeepSeek LLM
                retriever = st.session_state.vector_store.as_retriever()
                llm = ChatOpenAI(
                    model="deepseek-chat",
                    openai_api_key=api_key,
                    openai_api_base="https://api.deepseek.com/v1",
                    temperature=0.85,
                    max_tokens=1000  # Adjust token limit for safety
                )
                qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)

                # Generate response
                with st.spinner("Generating response..."):
                    response = qa_chain.run(user_query)

                    # Check if the response is relevant or not
                    if "I don't know" in response or "not in the document" in response.lower():
                        response = "I'm here to assist you with questions about uploaded documents or related web links."

                    st.write(f"**Answer:** {response}")
            except Exception as e:
                st.error(f"Error generating response: {e}")
else:
    st.warning("Please enter your DeepSeek API key to proceed.")