-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.py
More file actions
205 lines (176 loc) Β· 7.22 KB
/
frontend.py
File metadata and controls
205 lines (176 loc) Β· 7.22 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import streamlit as st
import requests
API_BASE = "http://127.0.0.1:8001/api/v1"
# ============ Page Config ============
st.set_page_config(
page_title="π PDF Query Assistant",
page_icon="π€",
layout="wide"
)
# ============ Custom Dark Theme CSS ============
st.markdown("""
<style>
body, .main {
background-color: #1E1E1E;
color: #E5E5E5;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.main-title {
text-align: center;
color: #10A37F;
font-size: 32px;
margin-bottom: 15px;
}
/* Sidebar styling */
section[data-testid="stSidebar"] {
background-color: #D3D3D3;
padding: 20px;
}
.sidebar-title {
color: #10A37F;
font-size: 22px;
font-weight: bold;
margin-bottom: 15px;
}
.stRadio > label {
display: block;
padding: 10px 15px;
margin-bottom: 8px;
border-radius: 8px;
background-color: #333333;
color: #10A37F;
cursor: pointer;
transition: all 0.3s ease;
}
.stRadio > label:hover {
background-color: #10A37F;
color: white;
}
.stButton > button {
background-color: #10A37F;
color: white;
border-radius: 8px;
padding: 0.5rem 1rem;
font-weight: 600;
border: none;
}
.stButton > button:hover {
background-color: #0E866A;
}
/* Chat bubbles */
.chat-user {
background-color: #2563EB;
color: white;
padding: 10px 15px;
border-radius: 15px;
margin: 5px 0;
text-align: right;
}
.chat-bot {
background-color: #10A37F;
color: white;
padding: 10px 15px;
border-radius: 15px;
margin: 5px 0;
text-align: left;
}
</style>
""", unsafe_allow_html=True)
st.markdown('<h1 class="main-title">π PDF Query Assistant</h1>', unsafe_allow_html=True)
# ============ Sidebar Navigation ============
st.sidebar.markdown('<div class="sidebar-title">π Navigation</div>', unsafe_allow_html=True)
menu = st.sidebar.radio(
"Navigation",
["π€ Upload", "π₯ Retrieve", "βοΈ Update", "ποΈ Delete", "π€ Chat with PDF"],
label_visibility="collapsed"
)
# ------------------ Upload ------------------
if menu == "π€ Upload":
st.subheader("π€ Upload PDF")
uploaded_file = st.file_uploader("Choose a PDF file", type=["pdf"], key="upload_file")
if uploaded_file and st.button("Upload", key="upload_btn"):
with st.spinner("Uploading and processing..."):
response = requests.post(
f"{API_BASE}/uploadfile",
files={"file": (uploaded_file.name, uploaded_file, "application/pdf")},
)
if response.status_code == 201:
result = response.json()
st.success(f"β
{result['message']}")
st.info(f"π File ID: `{result['file_id']}`")
st.session_state["last_file_id"] = result["file_id"]
else:
st.error(f"β Upload failed: {response.text}")
# ------------------ Retrieve ------------------
elif menu == "π₯ Retrieve":
st.subheader("π₯ Retrieve File Data")
file_id = st.text_input("Enter File ID", value=st.session_state.get("last_file_id", ""), key="retrieve_id")
if st.button("Retrieve", key="retrieve_btn"):
if not file_id:
st.warning("β οΈ Please provide a File ID.")
else:
response = requests.get(f"{API_BASE}/uploadfile/{file_id}")
if response.status_code == 200:
result = response.json()
st.success(f"β
{result['message']}")
st.write("**File Name:**", result["file_name"])
st.text_area("Extracted Text", result["text"], height=250, key="retrieve_text")
else:
st.error(f"β Retrieval failed: {response.text}")
# ------------------ Update ------------------
elif menu == "βοΈ Update":
st.subheader("βοΈ Update PDF File")
file_id = st.text_input("Enter File ID to Update", value=st.session_state.get("last_file_id", ""), key="update_id")
updated_file = st.file_uploader("Choose New PDF File", type=["pdf"], key="update_file")
if updated_file and st.button("Update", key="update_btn"):
with st.spinner("Updating file..."):
response = requests.put(
f"{API_BASE}/uploadfile/{file_id}",
files={"file": (updated_file.name, updated_file, "application/pdf")},
)
if response.status_code == 200:
result = response.json()
st.success(f"β
{result['message']}")
else:
st.error(f"β Update failed: {response.text}")
# ------------------ Delete ------------------
elif menu == "ποΈ Delete":
st.subheader("ποΈ Delete PDF File")
file_id = st.text_input("Enter File ID to Delete", value=st.session_state.get("last_file_id", ""), key="delete_id")
if st.button("Delete", key="delete_btn"):
with st.spinner("Deleting file..."):
response = requests.delete(f"{API_BASE}/uploadfile/{file_id}")
if response.status_code == 200:
result = response.json()
st.success(f"β
{result['message']}")
else:
st.error(f"β Delete failed: {response.text}")
# ------------------ Query with LLM ------------------
elif menu == "π€ Chat with PDF":
st.subheader("π€ Chat with PDF")
file_id = st.text_input("Enter File ID", value=st.session_state.get("last_file_id", ""), key="query_id")
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
user_question = st.text_area("Ask a Question", key="query_text")
if st.button("Send", key="query_btn"):
if not file_id or not user_question.strip():
st.warning("β οΈ Please provide both File ID and Question.")
else:
with st.spinner("Getting answer from LLM..."):
response = requests.get(
f"{API_BASE}/query_pdf/{file_id}",
params={"question": user_question},
)
if response.status_code == 200:
result = response.json()
answer = result["answer"]
st.session_state.chat_history.append(("user", user_question))
st.session_state.chat_history.append(("bot", answer))
else:
st.error(f"β Query failed: {response.text}")
# Display chat bubbles
for sender, msg in st.session_state.chat_history:
if sender == "user":
st.markdown(f'<div class="chat-user">π§ {msg}</div>', unsafe_allow_html=True)
else:
st.markdown(f'<div class="chat-bot">π€ {msg}</div>', unsafe_allow_html=True)