Spaces:
Running
Running
Jatin Mehra
commited on
Commit
·
6c81e4d
1
Parent(s):
e3257b5
Implement PDF removal functionality and update UI with remove button
Browse files- app.py +36 -0
- static/css/styles.css +11 -2
- static/index.html +2 -1
- static/js/app.js +35 -0
app.py
CHANGED
|
@@ -109,6 +109,32 @@ def load_session(session_id, model_name="meta-llama/llama-4-scout-17b-16e-instru
|
|
| 109 |
print(f"Error loading session: {str(e)}")
|
| 110 |
return None, False
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
# Mount static files (we'll create these later)
|
| 113 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 114 |
|
|
@@ -249,6 +275,16 @@ async def clear_history(request: SessionRequest):
|
|
| 249 |
|
| 250 |
return {"status": "success", "message": "Chat history cleared"}
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
# Route to list available models
|
| 253 |
@app.get("/models")
|
| 254 |
async def get_models():
|
|
|
|
| 109 |
print(f"Error loading session: {str(e)}")
|
| 110 |
return None, False
|
| 111 |
|
| 112 |
+
# Function to remove PDF file
|
| 113 |
+
def remove_pdf_file(session_id):
|
| 114 |
+
try:
|
| 115 |
+
# Check if the session exists
|
| 116 |
+
session_path = f"{UPLOAD_DIR}/{session_id}_session.pkl"
|
| 117 |
+
if os.path.exists(session_path):
|
| 118 |
+
# Load session data
|
| 119 |
+
with open(session_path, "rb") as f:
|
| 120 |
+
data = pickle.load(f)
|
| 121 |
+
|
| 122 |
+
# Delete PDF file if it exists
|
| 123 |
+
if data.get("file_path") and os.path.exists(data["file_path"]):
|
| 124 |
+
os.remove(data["file_path"])
|
| 125 |
+
|
| 126 |
+
# Remove session file
|
| 127 |
+
os.remove(session_path)
|
| 128 |
+
|
| 129 |
+
# Remove from memory if exists
|
| 130 |
+
if session_id in sessions:
|
| 131 |
+
del sessions[session_id]
|
| 132 |
+
|
| 133 |
+
return True
|
| 134 |
+
except Exception as e:
|
| 135 |
+
print(f"Error removing PDF file: {str(e)}")
|
| 136 |
+
return False
|
| 137 |
+
|
| 138 |
# Mount static files (we'll create these later)
|
| 139 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 140 |
|
|
|
|
| 275 |
|
| 276 |
return {"status": "success", "message": "Chat history cleared"}
|
| 277 |
|
| 278 |
+
# Route to remove PDF from session
|
| 279 |
+
@app.post("/remove-pdf")
|
| 280 |
+
async def remove_pdf(request: SessionRequest):
|
| 281 |
+
success = remove_pdf_file(request.session_id)
|
| 282 |
+
|
| 283 |
+
if success:
|
| 284 |
+
return {"status": "success", "message": "PDF file and session removed successfully"}
|
| 285 |
+
else:
|
| 286 |
+
raise HTTPException(status_code=404, detail="Session not found or could not be removed")
|
| 287 |
+
|
| 288 |
# Route to list available models
|
| 289 |
@app.get("/models")
|
| 290 |
async def get_models():
|
static/css/styles.css
CHANGED
|
@@ -33,7 +33,7 @@ body {
|
|
| 33 |
/* App container layout */
|
| 34 |
.app-container {
|
| 35 |
display: grid;
|
| 36 |
-
grid-template-columns: 260px 1fr
|
| 37 |
height: 100vh;
|
| 38 |
overflow: hidden;
|
| 39 |
}
|
|
@@ -151,7 +151,7 @@ body {
|
|
| 151 |
white-space: nowrap;
|
| 152 |
}
|
| 153 |
|
| 154 |
-
.btn-clear, .btn-new {
|
| 155 |
padding: 0.75rem;
|
| 156 |
border-radius: var(--border-radius);
|
| 157 |
border: none;
|
|
@@ -170,6 +170,15 @@ body {
|
|
| 170 |
background-color: rgba(239, 68, 68, 0.2);
|
| 171 |
}
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
.btn-new {
|
| 174 |
background-color: rgba(37, 99, 235, 0.1);
|
| 175 |
color: var(--primary-color);
|
|
|
|
| 33 |
/* App container layout */
|
| 34 |
.app-container {
|
| 35 |
display: grid;
|
| 36 |
+
grid-template-columns: 260px 1fr;
|
| 37 |
height: 100vh;
|
| 38 |
overflow: hidden;
|
| 39 |
}
|
|
|
|
| 151 |
white-space: nowrap;
|
| 152 |
}
|
| 153 |
|
| 154 |
+
.btn-clear, .btn-new, .btn-remove {
|
| 155 |
padding: 0.75rem;
|
| 156 |
border-radius: var(--border-radius);
|
| 157 |
border: none;
|
|
|
|
| 170 |
background-color: rgba(239, 68, 68, 0.2);
|
| 171 |
}
|
| 172 |
|
| 173 |
+
.btn-remove {
|
| 174 |
+
background-color: rgba(255, 152, 0, 0.1);
|
| 175 |
+
color: #ff9800;
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
.btn-remove:hover {
|
| 179 |
+
background-color: rgba(255, 152, 0, 0.2);
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
.btn-new {
|
| 183 |
background-color: rgba(37, 99, 235, 0.1);
|
| 184 |
color: var(--primary-color);
|
static/index.html
CHANGED
|
@@ -44,6 +44,7 @@
|
|
| 44 |
<span id="current-file-name">No file loaded</span>
|
| 45 |
</div>
|
| 46 |
<button id="clear-history" class="btn-clear">Clear History</button>
|
|
|
|
| 47 |
<button id="new-chat" class="btn-new">New Document</button>
|
| 48 |
</div>
|
| 49 |
</div>
|
|
@@ -102,7 +103,7 @@
|
|
| 102 |
</div>
|
| 103 |
|
| 104 |
<!-- Context Sidebar -->
|
| 105 |
-
<div class="context-sidebar" id="context-sidebar">
|
| 106 |
<div class="context-header">
|
| 107 |
<h3>Context Used</h3>
|
| 108 |
<button id="toggle-context" class="btn-toggle">
|
|
|
|
| 44 |
<span id="current-file-name">No file loaded</span>
|
| 45 |
</div>
|
| 46 |
<button id="clear-history" class="btn-clear">Clear History</button>
|
| 47 |
+
<button id="remove-pdf" class="btn-remove">Remove PDF</button>
|
| 48 |
<button id="new-chat" class="btn-new">New Document</button>
|
| 49 |
</div>
|
| 50 |
</div>
|
|
|
|
| 103 |
</div>
|
| 104 |
|
| 105 |
<!-- Context Sidebar -->
|
| 106 |
+
<div class="context-sidebar hidden" id="context-sidebar">
|
| 107 |
<div class="context-header">
|
| 108 |
<h3>Context Used</h3>
|
| 109 |
<button id="toggle-context" class="btn-toggle">
|
static/js/app.js
CHANGED
|
@@ -11,6 +11,7 @@ const modelSelect = document.getElementById('model-select');
|
|
| 11 |
const sessionInfo = document.getElementById('session-info');
|
| 12 |
const currentFileName = document.getElementById('current-file-name');
|
| 13 |
const clearHistoryBtn = document.getElementById('clear-history');
|
|
|
|
| 14 |
const newChatBtn = document.getElementById('new-chat');
|
| 15 |
const loadingOverlay = document.getElementById('loading-overlay');
|
| 16 |
const loadingText = document.getElementById('loading-text');
|
|
@@ -40,6 +41,7 @@ chatInput.addEventListener('input', () => {
|
|
| 40 |
sendButton.disabled = chatInput.value.trim() === '';
|
| 41 |
});
|
| 42 |
clearHistoryBtn.addEventListener('click', clearChatHistory);
|
|
|
|
| 43 |
newChatBtn.addEventListener('click', resetApp);
|
| 44 |
getStartedBtn.addEventListener('click', () => {
|
| 45 |
uploadBox.click();
|
|
@@ -344,6 +346,39 @@ async function clearChatHistory() {
|
|
| 344 |
}
|
| 345 |
}
|
| 346 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
function resetApp() {
|
| 348 |
// Clear current session
|
| 349 |
currentSessionId = null;
|
|
|
|
| 11 |
const sessionInfo = document.getElementById('session-info');
|
| 12 |
const currentFileName = document.getElementById('current-file-name');
|
| 13 |
const clearHistoryBtn = document.getElementById('clear-history');
|
| 14 |
+
const removePdfBtn = document.getElementById('remove-pdf');
|
| 15 |
const newChatBtn = document.getElementById('new-chat');
|
| 16 |
const loadingOverlay = document.getElementById('loading-overlay');
|
| 17 |
const loadingText = document.getElementById('loading-text');
|
|
|
|
| 41 |
sendButton.disabled = chatInput.value.trim() === '';
|
| 42 |
});
|
| 43 |
clearHistoryBtn.addEventListener('click', clearChatHistory);
|
| 44 |
+
removePdfBtn.addEventListener('click', removePdf);
|
| 45 |
newChatBtn.addEventListener('click', resetApp);
|
| 46 |
getStartedBtn.addEventListener('click', () => {
|
| 47 |
uploadBox.click();
|
|
|
|
| 346 |
}
|
| 347 |
}
|
| 348 |
|
| 349 |
+
async function removePdf() {
|
| 350 |
+
if (!currentSessionId) return;
|
| 351 |
+
|
| 352 |
+
try {
|
| 353 |
+
showLoading('Removing PDF from the system...');
|
| 354 |
+
|
| 355 |
+
const response = await fetch('/remove-pdf', {
|
| 356 |
+
method: 'POST',
|
| 357 |
+
headers: {
|
| 358 |
+
'Content-Type': 'application/json'
|
| 359 |
+
},
|
| 360 |
+
body: JSON.stringify({
|
| 361 |
+
session_id: currentSessionId
|
| 362 |
+
})
|
| 363 |
+
});
|
| 364 |
+
|
| 365 |
+
const data = await response.json();
|
| 366 |
+
|
| 367 |
+
if (data.status === 'success') {
|
| 368 |
+
// Reset the app
|
| 369 |
+
resetApp();
|
| 370 |
+
showError('PDF file has been removed from the system.');
|
| 371 |
+
} else {
|
| 372 |
+
showError('Failed to remove PDF: ' + data.detail);
|
| 373 |
+
}
|
| 374 |
+
} catch (error) {
|
| 375 |
+
console.error('Error removing PDF:', error);
|
| 376 |
+
showError('Failed to remove PDF. Please try again.');
|
| 377 |
+
} finally {
|
| 378 |
+
hideLoading();
|
| 379 |
+
}
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
function resetApp() {
|
| 383 |
// Clear current session
|
| 384 |
currentSessionId = null;
|