satyakimitra commited on
Commit
b5e4f5a
·
1 Parent(s): 6c8f7a8

Fix: Frontend url handling changed

Browse files
Files changed (1) hide show
  1. frontend/index.html +116 -15
frontend/index.html CHANGED
@@ -1134,7 +1134,22 @@
1134
  // API Service for backend communication
1135
  class APIService {
1136
  constructor() {
1137
- this.baseURL = 'http://localhost:8000'; // Backend server URL
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1138
  }
1139
 
1140
  async cleanupSession(sessionId) {
@@ -1164,17 +1179,42 @@
1164
  formData.append('files', file);
1165
  }
1166
 
1167
- const response = await fetch(`${this.baseURL}/api/upload`, {
1168
- method: 'POST',
1169
- body: formData
1170
- });
1171
 
1172
- if (!response.ok) {
1173
- const errorData = await response.json();
1174
- throw new Error(errorData.detail || `Upload failed: ${response.statusText}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1175
  }
1176
-
1177
- return await response.json();
1178
  }
1179
 
1180
  async startProcessing() {
@@ -1201,7 +1241,7 @@
1201
  }
1202
 
1203
  async sendChatMessage(message, sessionId = null) {
1204
- console.log('Sending chat message to backend:', message);
1205
 
1206
  try {
1207
  const response = await fetch(`${this.baseURL}/api/chat`, {
@@ -1215,9 +1255,19 @@
1215
  })
1216
  });
1217
 
 
 
1218
  if (!response.ok) {
1219
- const errorData = await response.json();
1220
- throw new Error(errorData.detail || `Chat failed: ${response.statusText}`);
 
 
 
 
 
 
 
 
1221
  }
1222
 
1223
  const result = await response.json();
@@ -1226,7 +1276,13 @@
1226
  return result;
1227
 
1228
  } catch (error) {
1229
- console.error('Chat API error details:', error);
 
 
 
 
 
 
1230
  throw error;
1231
  }
1232
  }
@@ -1418,6 +1474,22 @@
1418
  // Main app functions
1419
  async function initializeApp() {
1420
  setupEventListeners();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1421
  await checkSystemStatus();
1422
  startStatusPolling();
1423
  }
@@ -1560,6 +1632,27 @@
1560
  try {
1561
  showNotification('Uploading files...', 'info');
1562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1563
  // Update UI immediately
1564
  for (let file of files) {
1565
  addFileToList(file, 'uploading');
@@ -1576,7 +1669,15 @@
1576
 
1577
  } catch (error) {
1578
  console.error('Upload error:', error);
1579
- showNotification(`Upload failed: ${error.message}`, 'error');
 
 
 
 
 
 
 
 
1580
 
1581
  // Remove failed files from list
1582
  updateFileList();
 
1134
  // API Service for backend communication
1135
  class APIService {
1136
  constructor() {
1137
+ // AUTO-DETECT the correct base URL
1138
+ const currentOrigin = window.location.origin;
1139
+
1140
+ // Check if we're in HuggingFace Spaces or localhost
1141
+ if (currentOrigin.includes('hf.space') || currentOrigin.includes('huggingface.co')) {
1142
+ // HuggingFace Spaces
1143
+ this.baseURL = currentOrigin;
1144
+ } else if (currentOrigin.includes('localhost') || currentOrigin.includes('127.0.0.1')) {
1145
+ // Local development
1146
+ this.baseURL = 'http://localhost:8000';
1147
+ } else {
1148
+ // Any other deployment
1149
+ this.baseURL = currentOrigin;
1150
+ }
1151
+
1152
+ console.log('API Service initialized with baseURL:', this.baseURL);
1153
  }
1154
 
1155
  async cleanupSession(sessionId) {
 
1179
  formData.append('files', file);
1180
  }
1181
 
1182
+ console.log('Uploading files to:', `${this.baseURL}/api/upload`);
 
 
 
1183
 
1184
+ try {
1185
+ const response = await fetch(`${this.baseURL}/api/upload`, {
1186
+ method: 'POST',
1187
+ body: formData
1188
+ });
1189
+
1190
+ console.log('Upload response status:', response.status);
1191
+
1192
+ if (!response.ok) {
1193
+ let errorMessage = `Upload failed: ${response.status} ${response.statusText}`;
1194
+ try {
1195
+ const errorData = await response.json();
1196
+ errorMessage = errorData.detail || errorData.message || errorMessage;
1197
+ } catch (e) {
1198
+ const text = await response.text();
1199
+ errorMessage = `${errorMessage} - ${text.substring(0, 100)}`;
1200
+ }
1201
+ throw new Error(errorMessage);
1202
+ }
1203
+
1204
+ const result = await response.json();
1205
+ console.log('Upload successful:', result);
1206
+ return result;
1207
+
1208
+ } catch (error) {
1209
+ console.error('Upload error:', error);
1210
+
1211
+ // Better error message for network issues
1212
+ if (error.message.includes('Failed to fetch')) {
1213
+ throw new Error(`Cannot connect to server at ${this.baseURL}. Check if backend is running.`);
1214
+ }
1215
+
1216
+ throw error;
1217
  }
 
 
1218
  }
1219
 
1220
  async startProcessing() {
 
1241
  }
1242
 
1243
  async sendChatMessage(message, sessionId = null) {
1244
+ console.log('Sending chat message to backend:', this.baseURL);
1245
 
1246
  try {
1247
  const response = await fetch(`${this.baseURL}/api/chat`, {
 
1255
  })
1256
  });
1257
 
1258
+ console.log('Response status:', response.status);
1259
+
1260
  if (!response.ok) {
1261
+ let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
1262
+ try {
1263
+ const errorData = await response.json();
1264
+ errorMessage = errorData.detail || errorData.message || errorMessage;
1265
+ } catch (e) {
1266
+ // If response is not JSON, get text
1267
+ const text = await response.text();
1268
+ errorMessage = `${errorMessage} - ${text.substring(0, 100)}`;
1269
+ }
1270
+ throw new Error(errorMessage);
1271
  }
1272
 
1273
  const result = await response.json();
 
1276
  return result;
1277
 
1278
  } catch (error) {
1279
+ console.error('Chat API error:', error);
1280
+
1281
+ // Better error messages for common issues
1282
+ if (error.message.includes('Failed to fetch')) {
1283
+ throw new Error(`Cannot connect to server at ${this.baseURL}. Please make sure the backend is running.`);
1284
+ }
1285
+
1286
  throw error;
1287
  }
1288
  }
 
1474
  // Main app functions
1475
  async function initializeApp() {
1476
  setupEventListeners();
1477
+
1478
+ // Show current API endpoint in console
1479
+ console.log('🔧 QuerySphere initialized');
1480
+ console.log('🔧 Frontend URL:', window.location.origin);
1481
+ console.log('🔧 API Base URL:', apiService.baseURL);
1482
+
1483
+ // Test backend connection
1484
+ try {
1485
+ const health = await apiService.getHealth();
1486
+ console.log('✅ Backend connection successful:', health);
1487
+ showNotification('Connected to backend', 'success');
1488
+ } catch (error) {
1489
+ console.error('❌ Backend connection failed:', error);
1490
+ showNotification(`Cannot connect to backend at ${apiService.baseURL}`, 'error');
1491
+ }
1492
+
1493
  await checkSystemStatus();
1494
  startStatusPolling();
1495
  }
 
1632
  try {
1633
  showNotification('Uploading files...', 'info');
1634
 
1635
+ // First, check if we can connect to the backend
1636
+ try {
1637
+ console.log('Testing connection to:', apiService.baseURL);
1638
+ await fetch(`${apiService.baseURL}/api/health`);
1639
+ } catch (connectionError) {
1640
+ console.error('Connection test failed:', connectionError);
1641
+ showNotification(`Cannot connect to backend at ${apiService.baseURL}. Please check if the server is running.`, 'error');
1642
+ return;
1643
+ }
1644
+
1645
+ // Check file size limits for HuggingFace Spaces
1646
+ const MAX_SIZE_MB = 50;
1647
+ const MAX_SIZE_BYTES = MAX_SIZE_MB * 1024 * 1024;
1648
+
1649
+ for (let file of files) {
1650
+ if (file.size > MAX_SIZE_BYTES) {
1651
+ showNotification(`File ${file.name} is too large (max ${MAX_SIZE_MB}MB for HuggingFace Spaces)`, 'error');
1652
+ return;
1653
+ }
1654
+ }
1655
+
1656
  // Update UI immediately
1657
  for (let file of files) {
1658
  addFileToList(file, 'uploading');
 
1669
 
1670
  } catch (error) {
1671
  console.error('Upload error:', error);
1672
+
1673
+ // More specific error messages
1674
+ if (error.message.includes('Cannot connect to server')) {
1675
+ showNotification(error.message, 'error');
1676
+ } else if (error.message.includes('Failed to fetch')) {
1677
+ showNotification(`Network error: Cannot connect to ${apiService.baseURL}`, 'error');
1678
+ } else {
1679
+ showNotification(`Upload failed: ${error.message}`, 'error');
1680
+ }
1681
 
1682
  // Remove failed files from list
1683
  updateFileList();