// Paper Analysis JavaScript
// Store API key in session storage
let apiKey = sessionStorage.getItem('deepseekApiKey');
// DOM Elements
const apiKeyInput = document.getElementById('apiKey');
const saveApiKeyBtn = document.getElementById('saveApiKey');
const fileInput = document.getElementById('paperFile');
const fileInfo = document.getElementById('fileInfo');
const analyzeBtn = document.getElementById('analyzePaper');
const resultsSection = document.querySelector('.results-section');
const tabButtons = document.querySelectorAll('.tab-btn');
const tabPanes = document.querySelectorAll('.tab-pane');
const exportBtn = document.getElementById('exportResults');
// Initialize API key if exists
if (apiKey) {
apiKeyInput.value = apiKey;
}
// Save API Key and Test Connection
saveApiKeyBtn.addEventListener('click', async () => {
const newApiKey = apiKeyInput.value.trim();
if (!newApiKey) {
showNotification('Please enter a valid API key', 'error');
return;
}
try {
showLoading('Testing API connection...');
const response = await fetch('/paper-analysis/api/test-deepseek', {
method: 'POST',
headers: {
'X-API-Key': newApiKey,
'Content-Type': 'application/json'
}
});
const data = await response.json();
if (response.ok && data.success) {
sessionStorage.setItem('deepseekApiKey', newApiKey);
apiKey = newApiKey;
showNotification('API connection successful', 'success');
} else {
showNotification(`API Error: ${data.error || 'Unknown error'}`, 'error');
console.error('API Error Details:', data);
}
} catch (error) {
showNotification('Error testing API connection: ' + error.message, 'error');
console.error('API Test Error:', error);
} finally {
hideLoading();
}
});
// File Upload Handling
fileInput.addEventListener('change', handleFileSelect);
document.querySelector('.file-upload-container').addEventListener('dragover', handleDragOver);
document.querySelector('.file-upload-container').addEventListener('drop', handleFileDrop);
function handleFileSelect(event) {
const file = event.target.files[0];
if (file) {
updateFileInfo(file);
}
}
function handleDragOver(event) {
event.preventDefault();
event.stopPropagation();
event.currentTarget.classList.add('drag-over');
}
function handleFileDrop(event) {
event.preventDefault();
event.stopPropagation();
event.currentTarget.classList.remove('drag-over');
const file = event.dataTransfer.files[0];
if (file) {
fileInput.files = event.dataTransfer.files;
updateFileInfo(file);
}
}
function updateFileInfo(file) {
const sizeInMB = (file.size / (1024 * 1024)).toFixed(2);
fileInfo.innerHTML = `
File: ${file.name}
Size: ${sizeInMB} MB
Type: ${file.type || 'Unknown'}
`;
}
// Tab Navigation
tabButtons.forEach(button => {
button.addEventListener('click', () => {
const tabName = button.getAttribute('data-tab');
// Update active states
tabButtons.forEach(btn => btn.classList.remove('active'));
tabPanes.forEach(pane => pane.classList.remove('active'));
button.classList.add('active');
document.getElementById(`${tabName}Tab`).classList.add('active');
});
});
// Paper Analysis
analyzeBtn.addEventListener('click', async () => {
if (!apiKey) {
showNotification('Please configure your Deepseek API key first', 'error');
return;
}
if (!fileInput.files[0]) {
showNotification('Please select a file to analyze', 'error');
return;
}
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('extract_keywords', document.getElementById('extractKeywords').checked);
formData.append('generate_summary', document.getElementById('generateSummary').checked);
formData.append('find_related', document.getElementById('findRelatedWorks').checked);
try {
showLoading('Analyzing paper...');
const response = await fetch('/paper-analysis/api/analyze-paper', {
method: 'POST',
headers: {
'X-API-Key': apiKey
},
body: formData
});
const responseData = await response.json();
if (!response.ok) {
// Show detailed error message
const errorMessage = responseData.error || `HTTP error! status: ${response.status}`;
throw new Error(errorMessage);
}
displayResults(responseData);
hideLoading();
resultsSection.style.display = 'block';
showNotification('Analysis completed successfully', 'success');
} catch (error) {
hideLoading();
console.error('Analysis error:', error);
showNotification('Error analyzing paper: ' + error.message, 'error');
}
});
// Display Results
function displayResults(results) {
// Display Keywords
const keywordsContainer = document.querySelector('.keywords-container');
if (results.keywords) {
keywordsContainer.innerHTML = results.keywords.map(keyword =>
`