Spaces:
Running on Zero
Running on Zero
fix: update device mapping and GPU duration, and add thinking UI state to chat interface
Browse files- app.py +2 -2
- index.html +24 -2
app.py
CHANGED
|
@@ -17,7 +17,7 @@ model = AutoModelForImageTextToText.from_pretrained(
|
|
| 17 |
model_id,
|
| 18 |
torch_dtype=torch.bfloat16,
|
| 19 |
trust_remote_code=True,
|
| 20 |
-
device_map="
|
| 21 |
)
|
| 22 |
|
| 23 |
# Utility for response normalization
|
|
@@ -33,8 +33,8 @@ def normalize_response_text(text: str) -> str:
|
|
| 33 |
|
| 34 |
app = Server()
|
| 35 |
|
| 36 |
-
@spaces.GPU
|
| 37 |
@app.api()
|
|
|
|
| 38 |
def predict(message: str, file: FileData = None, downsample_mode: str = "16x"):
|
| 39 |
"""
|
| 40 |
General inference endpoint for both image and video.
|
|
|
|
| 17 |
model_id,
|
| 18 |
torch_dtype=torch.bfloat16,
|
| 19 |
trust_remote_code=True,
|
| 20 |
+
device_map="cuda"
|
| 21 |
)
|
| 22 |
|
| 23 |
# Utility for response normalization
|
|
|
|
| 33 |
|
| 34 |
app = Server()
|
| 35 |
|
|
|
|
| 36 |
@app.api()
|
| 37 |
+
@spaces.GPU(duration=120)
|
| 38 |
def predict(message: str, file: FileData = None, downsample_mode: str = "16x"):
|
| 39 |
"""
|
| 40 |
General inference endpoint for both image and video.
|
index.html
CHANGED
|
@@ -294,27 +294,49 @@
|
|
| 294 |
|
| 295 |
addMessage('user', currentText, fileUrl, fileType);
|
| 296 |
|
| 297 |
-
// Show loading
|
| 298 |
sendIcon.classList.add('hidden');
|
| 299 |
loadingSpinner.classList.remove('hidden');
|
| 300 |
sendBtn.disabled = true;
|
| 301 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
try {
|
| 303 |
let fileData = null;
|
| 304 |
if (currentFile) {
|
| 305 |
fileData = handle_file(currentFile);
|
| 306 |
}
|
| 307 |
|
|
|
|
| 308 |
const result = await client.predict("/predict", {
|
| 309 |
message: currentText,
|
| 310 |
file: fileData,
|
| 311 |
downsample_mode: currentMode
|
| 312 |
});
|
| 313 |
|
|
|
|
|
|
|
| 314 |
addMessage('assistant', result.data);
|
| 315 |
} catch (error) {
|
| 316 |
console.error("Prediction failed:", error);
|
| 317 |
-
|
|
|
|
| 318 |
} finally {
|
| 319 |
sendIcon.classList.remove('hidden');
|
| 320 |
loadingSpinner.classList.add('hidden');
|
|
|
|
| 294 |
|
| 295 |
addMessage('user', currentText, fileUrl, fileType);
|
| 296 |
|
| 297 |
+
// Show loading state
|
| 298 |
sendIcon.classList.add('hidden');
|
| 299 |
loadingSpinner.classList.remove('hidden');
|
| 300 |
sendBtn.disabled = true;
|
| 301 |
|
| 302 |
+
// Add thinking placeholder
|
| 303 |
+
const thinkingDiv = document.createElement('div');
|
| 304 |
+
thinkingDiv.className = 'flex gap-4 max-w-3xl mx-auto items-start message-anim';
|
| 305 |
+
thinkingDiv.id = 'thinking-placeholder';
|
| 306 |
+
thinkingDiv.innerHTML = `
|
| 307 |
+
<div class="w-8 h-8 rounded-full bg-indigo-500/20 flex items-center justify-center shrink-0 border border-indigo-500/30">
|
| 308 |
+
<i data-lucide="bot" class="w-4 h-4 text-indigo-400"></i>
|
| 309 |
+
</div>
|
| 310 |
+
<div class="glass p-5 rounded-2xl rounded-tl-none border border-white/5">
|
| 311 |
+
<div class="flex items-center gap-2 text-gray-400 italic">
|
| 312 |
+
<i data-lucide="loader-2" class="w-3 h-3 animate-spin"></i> MiniCPM is thinking...
|
| 313 |
+
</div>
|
| 314 |
+
</div>
|
| 315 |
+
`;
|
| 316 |
+
chatMessages.appendChild(thinkingDiv);
|
| 317 |
+
lucide.createIcons();
|
| 318 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 319 |
+
|
| 320 |
try {
|
| 321 |
let fileData = null;
|
| 322 |
if (currentFile) {
|
| 323 |
fileData = handle_file(currentFile);
|
| 324 |
}
|
| 325 |
|
| 326 |
+
console.log("Sending request to MiniCPM-V 4.6 backend...");
|
| 327 |
const result = await client.predict("/predict", {
|
| 328 |
message: currentText,
|
| 329 |
file: fileData,
|
| 330 |
downsample_mode: currentMode
|
| 331 |
});
|
| 332 |
|
| 333 |
+
// Remove placeholder and add actual message
|
| 334 |
+
document.getElementById('thinking-placeholder')?.remove();
|
| 335 |
addMessage('assistant', result.data);
|
| 336 |
} catch (error) {
|
| 337 |
console.error("Prediction failed:", error);
|
| 338 |
+
document.getElementById('thinking-placeholder')?.remove();
|
| 339 |
+
addMessage('assistant', "Sorry, I encountered an error while processing your request. Please check the Space logs.");
|
| 340 |
} finally {
|
| 341 |
sendIcon.classList.remove('hidden');
|
| 342 |
loadingSpinner.classList.add('hidden');
|