ranbac commited on
Commit
8a2b207
·
verified ·
1 Parent(s): 7cff29c

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +11 -13
index.html CHANGED
@@ -94,7 +94,6 @@
94
 
95
  let selectedFile = null;
96
 
97
- // 1. Xử lý khi người dùng chọn ảnh
98
  fileInput.addEventListener('change', function(e) {
99
  const file = e.target.files[0];
100
  if (file) {
@@ -104,52 +103,51 @@
104
  imagePreview.src = event.target.result;
105
  imagePreview.style.display = 'block';
106
  uploadText.style.display = 'none';
107
- submitBtn.disabled = false; // Mở khóa nút bấm
108
  }
109
  reader.readAsDataURL(file);
110
  }
111
  });
112
 
113
- // 2. Xử lý khi bấm nút "Trích xuất văn bản"
114
  submitBtn.addEventListener('click', async function() {
115
  if (!selectedFile) return;
116
 
117
- // Chuẩn bị giao diện chờ
118
  submitBtn.disabled = true;
119
  loading.style.display = 'block';
120
  resultBox.style.display = 'none';
121
  resultBox.innerHTML = '';
122
 
123
- // Đóng gói ảnh vào FormData
124
  const formData = new FormData();
125
  formData.append('file', selectedFile);
126
 
127
  try {
128
- // Thay "YOUR_API_URL_HERE" bằng đường dẫn API backend thật của bạn
129
- // dụ: "https://ranbac-ocr.hf.space/predict"
130
- const response = await fetch('/predict', {
 
 
131
  method: 'POST',
132
  body: formData
133
  });
134
 
 
135
  if (!response.ok) {
136
- throw new Error("Lỗi kết nối đến máy chủ API");
 
137
  }
138
 
139
  const data = await response.json();
140
-
141
- // Giả sử backend trả về JSON có cấu trúc: { "text": "Nội dung nhận diện được..." }
142
- // Bạn cần điều chỉnh 'data.text' theo đúng key mà Python trả về
143
  resultBox.innerText = data.text || "Không tìm thấy chữ trong ảnh.";
144
  resultBox.style.display = 'block';
 
145
 
146
  } catch (error) {
147
  console.error(error);
 
148
  resultBox.innerText = "Đã xảy ra lỗi: " + error.message;
149
  resultBox.style.display = 'block';
150
  resultBox.style.color = "red";
151
  } finally {
152
- // Hoàn tất, tắt trạng thái chờ
153
  loading.style.display = 'none';
154
  submitBtn.disabled = false;
155
  }
 
94
 
95
  let selectedFile = null;
96
 
 
97
  fileInput.addEventListener('change', function(e) {
98
  const file = e.target.files[0];
99
  if (file) {
 
103
  imagePreview.src = event.target.result;
104
  imagePreview.style.display = 'block';
105
  uploadText.style.display = 'none';
106
+ submitBtn.disabled = false;
107
  }
108
  reader.readAsDataURL(file);
109
  }
110
  });
111
 
 
112
  submitBtn.addEventListener('click', async function() {
113
  if (!selectedFile) return;
114
 
 
115
  submitBtn.disabled = true;
116
  loading.style.display = 'block';
117
  resultBox.style.display = 'none';
118
  resultBox.innerHTML = '';
119
 
 
120
  const formData = new FormData();
121
  formData.append('file', selectedFile);
122
 
123
  try {
124
+ // Tự động lấy đúng tên miền của Space hiện tại để gọi API
125
+ const currentUrl = window.location.href.split('?')[0].replace(/\/$/, "");
126
+ const apiUrl = currentUrl + "/predict";
127
+
128
+ const response = await fetch(apiUrl, {
129
  method: 'POST',
130
  body: formData
131
  });
132
 
133
+ // Nếu Backend báo lỗi, bóc tách lỗi chi tiết từ Python gửi về
134
  if (!response.ok) {
135
+ const errorData = await response.json();
136
+ throw new Error(errorData.detail || `Lỗi máy chủ (Mã: ${response.status})`);
137
  }
138
 
139
  const data = await response.json();
 
 
 
140
  resultBox.innerText = data.text || "Không tìm thấy chữ trong ảnh.";
141
  resultBox.style.display = 'block';
142
+ resultBox.style.color = "#333"; // Màu chữ bình thường
143
 
144
  } catch (error) {
145
  console.error(error);
146
+ // In lỗi chính xác ra màn hình
147
  resultBox.innerText = "Đã xảy ra lỗi: " + error.message;
148
  resultBox.style.display = 'block';
149
  resultBox.style.color = "red";
150
  } finally {
 
151
  loading.style.display = 'none';
152
  submitBtn.disabled = false;
153
  }