Spaces:
Sleeping
Sleeping
File size: 5,328 Bytes
f747401 | 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 | import { Client } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js";
// ==========================================
// CONFIGURATION
// ==========================================
// IMPORTANT: Update this variable after you deploy your Gradio app to Hugging Face Spaces.
// Example: "vishnu/ai-observability"
const HF_SPACE_ID = "VJ24/ai-observability";
// DOM Elements
const elements = {
queryInput: document.getElementById('queryInput'),
runBtn: document.getElementById('runBtn'),
btnText: document.querySelector('.btn-text'),
loader: document.querySelector('.loader'),
refreshTracesBtn: document.getElementById('refreshTracesBtn'),
tracesOutput: document.getElementById('tracesOutput'),
retrievedOutput: document.getElementById('retrievedOutput'),
answerOutput: document.getElementById('answerOutput'),
verdictOutput: document.getElementById('verdictOutput'),
latencyOutput: document.getElementById('latencyOutput'),
diagnosticsOutput: document.getElementById('diagnosticsOutput'),
setupAlert: document.getElementById('setupAlert')
};
// State
let client = null;
async function initClient() {
if (HF_SPACE_ID.includes("YOUR_HF_USERNAME")) {
// Just mock it until they actually hook it up
return null;
}
try {
client = await Client.connect(HF_SPACE_ID);
elements.setupAlert.style.display = 'none';
return client;
} catch (e) {
console.error("Failed to connect to Hugging Face Space:", e);
return null;
}
}
function setLoading(isLoading) {
if (isLoading) {
elements.btnText.textContent = "Processing...";
elements.loader.classList.remove('hidden');
elements.runBtn.disabled = true;
} else {
elements.btnText.textContent = "Execute Chain";
elements.loader.classList.add('hidden');
elements.runBtn.disabled = false;
}
}
async function runPipeline() {
const query = elements.queryInput.value.trim();
if (!query) return;
setLoading(true);
const startTime = performance.now();
try {
if (!client) {
// Mock response if backend is not linked yet
await simulateExecution();
return;
}
// Call our explicitly named API endpoint
const result = await client.predict("/process_query", {
query: query
});
const [retrievedText, answerText, verdictText, diagnosticsJSON] = result.data;
// Update UI
elements.retrievedOutput.textContent = retrievedText;
elements.answerOutput.textContent = answerText;
elements.diagnosticsOutput.textContent = diagnosticsJSON;
// Parse verdict
updateVerdictUI(verdictText);
} catch (e) {
console.error("Pipeline Error:", e);
elements.answerOutput.textContent = "Error: Failed to fetch from backend. Check console.";
} finally {
const latency = Math.round(performance.now() - startTime);
elements.latencyOutput.textContent = `${latency} ms`;
setLoading(false);
fetchTraces(); // Auto refresh traces
}
}
async function fetchTraces() {
elements.refreshTracesBtn.style.transform = "rotate(180deg)";
setTimeout(() => { elements.refreshTracesBtn.style.transform = "rotate(0deg)"; }, 300);
try {
if (!client) {
elements.tracesOutput.textContent = "// Traces mock (Backend not connected)";
return;
}
const result = await client.predict("/get_traces", {});
elements.tracesOutput.textContent = result.data[0];
} catch (e) {
console.error("Traces Error:", e);
elements.tracesOutput.textContent = "// Failed to load traces.";
}
}
function updateVerdictUI(verdictText) {
// Example format: "Verdict: pass (Score: 0.85)"
elements.verdictOutput.textContent = verdictText;
elements.verdictOutput.className = "value-badge";
if (verdictText.toLowerCase().includes('pass')) {
elements.verdictOutput.classList.add('pass');
} else if (verdictText.toLowerCase().includes('fail')) {
elements.verdictOutput.classList.add('fail');
}
}
async function simulateExecution() {
// A mock response to demonstrate UI if the user hasn't connected HF yet.
await new Promise(r => setTimeout(r, 1500));
elements.retrievedOutput.textContent = JSON.stringify([
{ "id": "mock_id", "text": "This is mock data because HF backend is not connected.", "score": 0.9 }
], null, 2);
elements.answerOutput.textContent = "This is a frontend demonstration. Setup the Hugging Face space pointing to your repository, then place the space ID in app.js to go live.";
updateVerdictUI("Verdict: pass (Score: 0.99) [MOCK]");
elements.diagnosticsOutput.textContent = JSON.stringify({
"status": "success",
"mock": true
}, null, 2);
}
// Event Listeners
elements.runBtn.addEventListener('click', runPipeline);
elements.refreshTracesBtn.addEventListener('click', fetchTraces);
elements.queryInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
runPipeline();
}
});
// Initialize
document.addEventListener('DOMContentLoaded', () => {
initClient();
});
|