Spaces:
Runtime error
Runtime error
File size: 7,436 Bytes
c8e875f | 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 | /**
* Main application entry point for arXivCSRAG
*/
document.addEventListener('DOMContentLoaded', () => {
// Initialize UI and Chat managers
const uiManager = new UIManager();
const chatManager = new ChatManager(uiManager);
// API Keys configuration
const saveApiKeysBtn = document.getElementById('save-api-keys-btn');
saveApiKeysBtn.addEventListener('click', async () => {
const geminiApiKey = document.getElementById('gemini-api-key').value.trim();
const huggingfaceToken = document.getElementById('huggingface-token').value.trim();
if (!geminiApiKey || !huggingfaceToken) {
alert('Please enter both API keys.');
return;
}
uiManager.showLoading('Configuring API keys...');
try {
const response = await ApiService.configureApiKeys(geminiApiKey, huggingfaceToken);
if (response.status === 'success') {
uiManager.hideModal(uiManager.apiKeysModal);
alert('API keys configured successfully!');
} else {
alert('Failed to configure API keys. Please try again.');
}
} catch (error) {
console.error('Error saving API keys:', error);
alert('An unexpected error occurred. Please try again.');
} finally {
uiManager.hideLoading();
}
});
// Paper search functionality
const searchButton = document.getElementById('search-button');
searchButton.addEventListener('click', async () => {
// Get search parameters
const subjectTags = Array.from(document.getElementById('subject-tags-select').selectedOptions).map(option => option.value);
const startDate = document.getElementById('start-date').value;
const endDate = document.getElementById('end-date').value;
const maxResults = parseInt(document.getElementById('max-results').value) || 10;
const query = document.getElementById('search-query').value.trim();
// if (!query) {
// alert('Please enter a search query.');
// return;
// }
uiManager.showLoading('Searching for papers...');
try {
const response = await ApiService.fetchPapers({
subject_tags: subjectTags.length > 0 ? subjectTags : null,
start_date : startDate || null,
end_date : endDate || null,
max_results : maxResults,
query : query
});
if (response.status === 'success') {
uiManager.renderSearchResults(response.papers);
} else {
alert('Failed to fetch papers. Please try again.');
}
} catch (error) {
console.error('Error searching papers:', error);
alert('An unexpected error occurred. Please try again.');
} finally {
uiManager.hideLoading();
}
});
// Upload paper functionality
const uploadButton = document.getElementById('upload-button');
const fileInput = document.getElementById('pdf-upload');
const fileNameDisplay = document.querySelector('.file-name-display');
const selectedFileName = document.getElementById('selected-file-name');
// Handle file selection display
fileInput.addEventListener('change', (event) => {
const file = fileInput.files[0];
console.log('File selected:', file ? file.name : 'No file');
if (file) {
selectedFileName.textContent = file.name;
fileNameDisplay.classList.add('active');
uploadButton.classList.add('file-selected');
uploadButton.innerHTML = '<i class="bi bi-upload"></i> Upload "' + file.name.substring(0, 15) + (file.name.length > 15 ? '...' : '') + '"';
console.log('File name display should be visible now');
} else {
selectedFileName.textContent = 'No file selected';
fileNameDisplay.classList.remove('active');
uploadButton.classList.remove('file-selected');
uploadButton.innerHTML = '<i class="bi bi-upload"></i> Upload PDF';
}
});
uploadButton.addEventListener('click', async () => {
const file = fileInput.files[0];
if (!file) {
alert('Please select a PDF file to upload.');
return;
}
if (file.type !== 'application/pdf') {
alert('Please select a valid PDF file.');
return;
}
uiManager.showLoading('Uploading paper...');
try {
const response = await ApiService.uploadPaper(file);
if (response.status === 'success') {
await chatManager.processPaper(response.file_path);
} else {
alert('Failed to upload paper. Please try again.');
uiManager.hideLoading();
}
} catch (error) {
console.error('Error uploading paper:', error);
alert('An unexpected error occurred. Please try again.');
uiManager.hideLoading();
}
});
// Paper info modal buttons
document.getElementById('open-arxiv-btn').addEventListener('click', function() {
const url = this.dataset.url;
if (url) {
window.open(url, '_blank');
}
});
document.getElementById('view-pdf-btn').addEventListener('click', function() {
const url = this.dataset.url;
if (url) {
window.open(url, '_blank');
}
});
document.getElementById('download-pdf-btn').addEventListener('click', function() {
const paperId = this.dataset.paperId;
if (paperId) {
// Create a temporary link to download the file
uiManager.showLoading('Preparing download...');
ApiService.downloadPaper(paperId)
.then(response => {
if (response.status === 'success') {
// Create a link to download the file directly
const a = document.createElement('a');
a.href = response.file_path;
a.download = `${paperId.replace('/', '_')}.pdf`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
alert('Failed to download paper. Please try again.');
}
})
.catch(error => {
console.error('Error downloading paper:', error);
alert('An unexpected error occurred. Please try again.');
})
.finally(() => {
uiManager.hideLoading();
});
}
});
document.getElementById('chat-with-paper-btn').addEventListener('click', function() {
const paperId = this.dataset.paperId;
if (paperId) {
chatManager.downloadAndProcessPaper(paperId);
}
});
// Show API keys modal on first load
setTimeout(() => {
uiManager.showModal(uiManager.apiKeysModal);
}, 500);
}); |