jeysshon commited on
Commit
1af833b
Β·
verified Β·
1 Parent(s): 65e81d2

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +81 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tempfile
3
+ import streamlit as st
4
+ from langchain_community.document_loaders import PyPDFLoader
5
+ from langchain.text_splitter import RecursiveCharacterTextSplitter # Correct import
6
+ from langchain_community.embeddings import HuggingFaceEmbeddings
7
+ from langchain_community.vectorstores import FAISS
8
+ from langchain.chains import RetrievalQA # Correct import
9
+ from langchain_community.chat_models import ChatOpenAI
10
+
11
+ # Streamlit App Title
12
+ st.title("πŸ“„ DeepSeek-Powered RAG Chatbot")
13
+
14
+ # Step 1: Input API Key
15
+ api_key = st.text_input("πŸ”‘ Enter your DeepSeek API Key:", type="password")
16
+
17
+ if api_key:
18
+ # Set the API key as an environment variable (optional)
19
+ os.environ["DEEPSEEK_API_KEY"] = api_key
20
+
21
+ # Step 2: Upload PDF Document
22
+ uploaded_file = st.file_uploader("πŸ“‚ Upload a PDF document", type=["pdf"])
23
+
24
+ # Use session state to persist the vector_store
25
+ if "vector_store" not in st.session_state:
26
+ st.session_state.vector_store = None
27
+
28
+ if uploaded_file and st.session_state.vector_store is None:
29
+ try:
30
+ with st.spinner("Processing document..."):
31
+ # Save the uploaded file temporarily
32
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
33
+ tmp_file.write(uploaded_file.getvalue())
34
+ tmp_file_path = tmp_file.name
35
+
36
+ # Use the temporary file path with PyPDFLoader
37
+ loader = PyPDFLoader(tmp_file_path)
38
+ documents = loader.load()
39
+
40
+ # Remove the temporary file
41
+ os.unlink(tmp_file_path)
42
+
43
+ # Split the document into chunks
44
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
45
+ chunks = text_splitter.split_documents(documents)
46
+
47
+ # Generate embeddings and store them in a vector database
48
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
49
+ st.session_state.vector_store = FAISS.from_documents(chunks, embeddings)
50
+
51
+ st.success("Document processed successfully!")
52
+ except Exception as e:
53
+ st.error(f"Error processing document: {e}")
54
+ st.stop()
55
+
56
+ # Step 3: Ask Questions About the Document
57
+ if st.session_state.vector_store:
58
+ st.subheader("πŸ’¬ Chat with Your Document")
59
+ user_query = st.text_input("Ask a question:")
60
+
61
+ if user_query:
62
+ try:
63
+ # Set up the RAG pipeline with DeepSeek LLM
64
+ retriever = st.session_state.vector_store.as_retriever()
65
+ llm = ChatOpenAI(
66
+ model="deepseek-chat",
67
+ openai_api_key=api_key,
68
+ openai_api_base="https://api.deepseek.com/v1",
69
+ temperature=0.85,
70
+ max_tokens=1000 # Adjust token limit for safety
71
+ )
72
+ qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
73
+
74
+ # Generate response
75
+ with st.spinner("Generating response..."):
76
+ response = qa_chain.run(user_query)
77
+ st.write(f"**Answer:** {response}")
78
+ except Exception as e:
79
+ st.error(f"Error generating response: {e}")
80
+ else:
81
+ st.warning("Please enter your DeepSeek API key to proceed.")
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit==1.25.0
2
+ langchain
3
+ langchain-community
4
+ faiss-cpu==1.7.4
5
+ sentence-transformers
6
+ pypdf==3.8.1
7
+ openai