Spaces:
Runtime error
Runtime error
File size: 6,232 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
/**
* API Service for the arXivCSRAG application
* Manages all API requests to the backend
*/
class ApiService {
/**
* Configure API keys
* @param {string} geminiApiKey - The Google Gemini API key
* @param {string} huggingfaceToken - The Hugging Face token
* @returns {Promise} - The API response
*/
static async configureApiKeys(geminiApiKey, huggingfaceToken) {
try {
const response = await fetch('/api/configure', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
gemini_api_key : geminiApiKey,
huggingface_token: huggingfaceToken
})
});
return await response.json();
} catch (error) {
console.error('Error configuring API keys:', error);
throw error;
}
}
/**
* Fetch papers from arXiv
* @param {Object} searchParams - The search parameters
* @returns {Promise} - The API response
*/
static async fetchPapers(searchParams) {
try {
const response = await fetch('/api/fetch-papers', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(searchParams)
});
return await response.json();
} catch (error) {
console.error('Error fetching papers:', error);
throw error;
}
}
/**
* Get paper metadata
* @param {string} arxivId - The arXiv ID of the paper
* @returns {Promise} - The API response
*/
static async getPaperMetadata(arxivId) {
try {
const response = await fetch('/api/paper-metadata', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
arxiv_id: arxivId
})
});
return await response.json();
} catch (error) {
console.error('Error getting paper metadata:', error);
throw error;
}
}
/**
* Download a paper
* @param {string} arxivId - The arXiv ID of the paper
* @returns {Promise} - The API response
*/
static async downloadPaper(arxivId) {
try {
const response = await fetch('/api/download-paper', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
arxiv_id: arxivId
})
});
return await response.json();
} catch (error) {
console.error('Error downloading paper:', error);
throw error;
}
}
/**
* Upload a paper
* @param {File} file - The PDF file to upload
* @returns {Promise} - The API response
*/
static async uploadPaper(file) {
try {
const formData = new FormData();
formData.append('file', file);
const response = await fetch('/api/upload-paper', {
method: 'POST',
body : formData
});
return await response.json();
} catch (error) {
console.error('Error uploading paper:', error);
throw error;
}
}
/**
* Process a paper for RAG
* @param {string} filePath - The path to the PDF file
* @returns {Promise} - The API response
*/
static async processPaper(filePath) {
try {
const formData = new FormData();
formData.append('file_path', filePath);
const response = await fetch('/api/process-paper', {
method: 'POST',
body : formData
});
return await response.json();
} catch (error) {
console.error('Error processing paper:', error);
throw error;
}
}
/**
* Chat with a processed paper
* @param {string} message - The user's message
* @returns {Promise} - The API response
*/
static async chatWithPaper(message) {
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: message
})
});
return await response.json();
} catch (error) {
console.error('Error chatting with paper:', error);
throw error;
}
}
/**
* Reset the chat
* @returns {Promise} - The API response
*/
static async resetChat() {
try {
const response = await fetch('/api/reset-chat', {
method: 'POST'
});
return await response.json();
} catch (error) {
console.error('Error resetting chat:', error);
throw error;
}
}
/**
* Fetch citations for a specific query
* @param {string} message - The query message
* @returns {Promise} - The API response with citations
*/
static async fetchCitations(message) {
try {
const response = await fetch('/api/fetch-citations', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: message
})
});
return await response.json();
} catch (error) {
console.error('Error fetching citations:', error);
throw error;
}
}
} |