chalisesagun commited on
Commit
91c8836
Β·
verified Β·
1 Parent(s): 1d0983f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from langchain.document_loaders import PyPDFLoader
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain.embeddings import HuggingFaceEmbeddings
6
+ from langchain.vectorstores import FAISS
7
+ from langchain.chains import RetrievalQA
8
+ from langchain.chat_models import ChatOpenAI
9
+
10
+ # Streamlit App Title
11
+ st.title("πŸ“„ DeepSeek-Powered RAG Chatbot")
12
+
13
+ # Step 1: Input API Key
14
+ api_key = st.text_input("πŸ”‘ Enter your DeepSeek API Key:", type="password")
15
+
16
+ if api_key:
17
+ # Set the API key as an environment variable (optional)
18
+ os.environ["DEEPSEEK_API_KEY"] = api_key
19
+
20
+ # Step 2: Upload PDF Document
21
+ uploaded_file = st.file_uploader("πŸ“‚ Upload a PDF document", type=["pdf"])
22
+
23
+ if uploaded_file:
24
+ # Load and process the document
25
+ with st.spinner("Processing document..."):
26
+ loader = PyPDFLoader(uploaded_file)
27
+ documents = loader.load()
28
+
29
+ # Split the document into chunks
30
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
31
+ chunks = text_splitter.split_documents(documents)
32
+
33
+ # Generate embeddings and store them in a vector database
34
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
35
+ vector_store = FAISS.from_documents(chunks, embeddings)
36
+
37
+ st.success("Document processed successfully!")
38
+
39
+ # Step 3: Ask Questions About the Document
40
+ st.subheader("πŸ’¬ Chat with Your Document")
41
+ user_query = st.text_input("Ask a question:")
42
+
43
+ if user_query:
44
+ # Set up the RAG pipeline with DeepSeek LLM
45
+ retriever = vector_store.as_retriever()
46
+ llm = ChatOpenAI(
47
+ model="deepseek-chat",
48
+ openai_api_key=api_key,
49
+ openai_api_base="https://api.deepseek.com/v1",
50
+ temperature=0.85,
51
+ max_tokens=4000 # Ensure compliance with DeepSeek's token limit
52
+ )
53
+ qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
54
+
55
+ # Generate response
56
+ with st.spinner("Generating response..."):
57
+ try:
58
+ response = qa_chain.run(user_query)
59
+ st.write(f"**Answer:** {response}")
60
+ except Exception as e:
61
+ st.error(f"Error: {e}")
62
+ else:
63
+ st.warning("Please enter your DeepSeek API key to proceed.")