-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathapp.py
More file actions
162 lines (137 loc) · 5.06 KB
/
Copy pathapp.py
File metadata and controls
162 lines (137 loc) · 5.06 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import streamlit as st
from langchain_community.document_loaders import PDFPlumberLoader
from langchain_experimental.text_splitter import SemanticChunker
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.llms import Ollama
from langchain.prompts import PromptTemplate
from langchain.chains.llm import LLMChain
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.chains import RetrievalQA
# Define color palette with improved contrast
primary_color = "#007BFF" # Bright blue for primary buttons
secondary_color = "#FFC107" # Amber for secondary buttons
background_color = "#F8F9FA" # Light gray for the main background
sidebar_background = "#2C2F33" # Dark gray for sidebar (better contrast)
text_color = "#212529" # Dark gray for content text
sidebar_text_color = "#FFFFFF" # White text for sidebar
header_text_color = "#000000" # Black headings for better visibility
st.markdown("""
<style>
/* Main Background */
.stApp {{
background-color: #F8F9FA;
color: #212529;
}}
/* Sidebar Styling */
[data-testid="stSidebar"] {{
background-color: #2C2F33 !important;
color: #FFFFFF !important;
}}
[data-testid="stSidebar"] * {{
color: #FFFFFF !important;
font-size: 16px !important;
}}
/* Headings */
h1, h2, h3, h4, h5, h6 {{
color: #000000 !important;
font-weight: bold;
}}
/* Fix Text Visibility */
p, span, div {{
color: #212529 !important;
}}
/* File Uploader */
.stFileUploader>div>div>div>button {{
background-color: #FFC107;
color: #000000;
font-weight: bold;
border-radius: 8px;
}}
/* Fix Navigation Bar (Top Bar) */
header {{
background-color: #1E1E1E !important;
}}
header * {{
color: #FFFFFF !important;
}}
</style>
""", unsafe_allow_html=True)
# App title
st.title("📄 Build a RAG System with DeepSeek R1 & Ollama")
# Sidebar for instructions and settings
with st.sidebar:
st.header("Instructions")
st.markdown("""
1. Upload a PDF file using the uploader below.
2. Ask questions related to the document.
3. The system will retrieve relevant content and provide a concise answer.
""")
st.header("Settings")
st.markdown("""
- **Embedding Model**: HuggingFace
- **Retriever Type**: Similarity Search
- **LLM**: DeepSeek R1 (Ollama)
""")
# Main file uploader section
st.header("📁 Upload a PDF Document")
uploaded_file = st.file_uploader("Upload your PDF file here", type="pdf")
if uploaded_file is not None:
st.success("PDF uploaded successfully! Processing...")
# Save the uploaded file
with open("temp.pdf", "wb") as f:
f.write(uploaded_file.getvalue())
# Load the PDF
loader = PDFPlumberLoader("temp.pdf")
docs = loader.load()
# Split the document into chunks
st.subheader("📚 Splitting the document into chunks...")
text_splitter = SemanticChunker(HuggingFaceEmbeddings())
documents = text_splitter.split_documents(docs)
# Instantiate the embedding model
embedder = HuggingFaceEmbeddings()
# Create vector store and retriever
st.subheader("🔍 Creating embeddings and setting up the retriever...")
vector = FAISS.from_documents(documents, embedder)
retriever = vector.as_retriever(search_type="similarity", search_kwargs={"k": 3})
# Define the LLM and the prompt
llm = Ollama(model="deepseek-r1:1.5b")
prompt = """
1. Use the following pieces of context to answer the question at the end.
2. If you don't know the answer, just say that "I don't know" but don't make up an answer on your own.\n
3. Keep the answer crisp and limited to 3,4 sentences.
Context: {context}
Question: {question}
Helpful Answer:"""
QA_CHAIN_PROMPT = PromptTemplate.from_template(prompt)
# Define the document and combination chains
llm_chain = LLMChain(llm=llm, prompt=QA_CHAIN_PROMPT, verbose=True)
document_prompt = PromptTemplate(
input_variables=["page_content", "source"],
template="Context:\ncontent:{page_content}\nsource:{source}",
)
combine_documents_chain = StuffDocumentsChain(
llm_chain=llm_chain,
document_variable_name="context",
document_prompt=document_prompt,
verbose=True
)
qa = RetrievalQA(
combine_documents_chain=combine_documents_chain,
retriever=retriever,
verbose=True,
return_source_documents=True
)
# Question input and response display
st.header("❓ Ask a Question")
user_input = st.text_input("Type your question related to the document:")
if user_input:
with st.spinner("Processing your query..."):
try:
response = qa(user_input)["result"]
st.success("✅ Response:")
st.write(response)
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.info("Please upload a PDF file to start.")