-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
166 lines (142 loc) · 5.25 KB
/
Copy pathapp.py
File metadata and controls
166 lines (142 loc) · 5.25 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
161
162
163
164
165
166
import hashlib
import os
import tempfile
import streamlit as st
from config import ConfigurationError, validate_environment
from ingest_data import ingest_pdf
from rag_engine import RAGEngine
# Page configuration
st.set_page_config(page_title="AI Resume Chatbot", page_icon="📄", layout="wide")
# Custom CSS for premium look
st.markdown(
"""
<style>
.main {
background-color: #0e1117;
}
.stTextInput > div > div > input {
color: #ffffff;
}
.stChatMessage {
border-radius: 15px;
padding: 10px;
margin-bottom: 10px;
}
</style>
""",
unsafe_allow_html=True,
)
# Initialize RAG Engine
@st.cache_resource
def get_rag_engine():
return RAGEngine()
try:
validate_environment()
engine = get_rag_engine()
except ConfigurationError as error:
st.error(f"Configuration error: {error}")
st.info("Copy .env.example to .env and provide the required credentials.")
st.stop()
except Exception as e:
st.error(f"Failed to initialize RAG Engine: {e}")
st.stop()
# Session state defaults
if "messages" not in st.session_state:
st.session_state.messages = []
if "ingested_docs" not in st.session_state:
st.session_state.ingested_docs = {}
if "selected_doc_ids" not in st.session_state:
st.session_state.selected_doc_ids = []
# Sidebar for file upload
with st.sidebar:
st.title("📂 Document Manager")
uploaded_files = st.file_uploader(
"Upload one or more PDFs for RAG", type="pdf", accept_multiple_files=True
)
if uploaded_files and st.button("Ingest Selected Documents"):
success_count = 0
failed_files = []
ingestion_warnings = []
for uploaded_file in uploaded_files:
with st.spinner(f"Processing {uploaded_file.name}..."):
tmp_path = None
try:
file_bytes = uploaded_file.getvalue()
doc_id = hashlib.sha256(file_bytes).hexdigest()[:16]
with tempfile.NamedTemporaryFile(
delete=False, suffix=".pdf"
) as tmp:
tmp.write(file_bytes)
tmp_path = tmp.name
result = ingest_pdf(
tmp_path, doc_id=doc_id, source_name=uploaded_file.name
)
if result:
st.session_state.ingested_docs[doc_id] = uploaded_file.name
success_count += 1
if result.warnings:
ingestion_warnings.append(
f"{uploaded_file.name}: captioned "
f"{result.images_captioned} of "
f"{result.images_found} image(s)."
)
else:
failed_files.append(uploaded_file.name)
except Exception:
failed_files.append(uploaded_file.name)
finally:
if tmp_path and os.path.exists(tmp_path):
os.remove(tmp_path)
if success_count:
st.success(f"✅ Ingested {success_count} document(s).")
if ingestion_warnings:
st.warning(
"⚠️ Text was indexed, but vision processing was incomplete. "
+ " ".join(ingestion_warnings)
)
if failed_files:
st.error(f"❌ Failed: {', '.join(failed_files)}")
if st.session_state.ingested_docs:
st.subheader("Indexed Documents")
doc_options = {
f"{name} ({doc_id[:8]})": doc_id
for doc_id, name in st.session_state.ingested_docs.items()
}
selected_labels = st.multiselect(
"Filter retrieval to these files",
options=list(doc_options.keys()),
default=list(doc_options.keys()),
)
st.session_state.selected_doc_ids = [
doc_options[label] for label in selected_labels
]
if st.button("Clear Chat"):
st.session_state.messages = []
st.rerun()
st.title("📄 AI Resume Chatbot")
st.caption("Ask me anything about the uploaded documents!")
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# User input
if prompt := st.chat_input("What would you like to know?"):
if not st.session_state.ingested_docs:
st.error("Please upload and ingest at least one document first.")
st.stop()
doc_filter = st.session_state.selected_doc_ids
if not doc_filter:
st.error("Please select at least one indexed file in the sidebar.")
st.stop()
# Add user message to history
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate response
with st.chat_message("assistant"), st.spinner("Thinking..."):
try:
response = engine.generate_answer(prompt, doc_ids=doc_filter)
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
except Exception as e:
st.error(f"Error generating response: {e}")