Spaces:
Running
Running
Update index.html
Browse files- index.html +69 -4
index.html
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title>Dynamic Input Form</title>
|
| 7 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
|
| 9 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
|
|
@@ -12,6 +12,13 @@
|
|
| 12 |
<div class="container mx-auto px-4">
|
| 13 |
<div class="bg-white rounded-lg shadow-md p-6 max-w-lg mx-auto">
|
| 14 |
<h1 class="text-2xl font-bold mb-6 text-center">Dynamic Input Form</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
<form id="dynamicForm" class="space-y-4">
|
| 16 |
<div id="inputContainer" class="space-y-4">
|
| 17 |
<div class="flex items-center space-x-2">
|
|
@@ -25,6 +32,8 @@
|
|
| 25 |
<button type="button" class="btn btn-secondary" onclick="resetForm()">Reset</button>
|
| 26 |
</div>
|
| 27 |
</form>
|
|
|
|
|
|
|
| 28 |
<div id="output" class="mt-6 p-4 bg-gray-200 rounded-md hidden">
|
| 29 |
<h2 class="text-lg font-semibold mb-2">Output:</h2>
|
| 30 |
<pre id="formattedOutput" class="text-gray-800 font-mono whitespace-pre-wrap"></pre>
|
|
@@ -33,6 +42,7 @@
|
|
| 33 |
</div>
|
| 34 |
|
| 35 |
<script>
|
|
|
|
| 36 |
function addInput() {
|
| 37 |
const container = document.getElementById('inputContainer');
|
| 38 |
const newInput = document.createElement('div');
|
|
@@ -45,11 +55,13 @@
|
|
| 45 |
updateDeleteButtons();
|
| 46 |
}
|
| 47 |
|
|
|
|
| 48 |
function deleteInput(button) {
|
| 49 |
button.parentElement.remove();
|
| 50 |
updateDeleteButtons();
|
| 51 |
}
|
| 52 |
|
|
|
|
| 53 |
function updateDeleteButtons() {
|
| 54 |
const deleteButtons = document.querySelectorAll('.delete-btn');
|
| 55 |
deleteButtons.forEach(btn => {
|
|
@@ -57,6 +69,7 @@
|
|
| 57 |
});
|
| 58 |
}
|
| 59 |
|
|
|
|
| 60 |
function resetForm() {
|
| 61 |
const container = document.getElementById('inputContainer');
|
| 62 |
container.innerHTML = `
|
|
@@ -69,20 +82,72 @@
|
|
| 69 |
document.getElementById('formattedOutput').textContent = '';
|
| 70 |
}
|
| 71 |
|
|
|
|
| 72 |
function processInputValue(value) {
|
| 73 |
-
// Trim leading and trailing spaces, convert to lowercase, and replace internal spaces with underscores
|
| 74 |
return value.trim().toLowerCase().replace(/\s+/g, '_');
|
| 75 |
}
|
| 76 |
|
|
|
|
| 77 |
document.getElementById('dynamicForm').addEventListener('submit', function(e) {
|
| 78 |
e.preventDefault();
|
| 79 |
const inputs = document.querySelectorAll('#inputContainer input');
|
| 80 |
const values = Array.from(inputs).map(input => processInputValue(input.value));
|
| 81 |
const formattedOutput = '[' + values.map(v => `'${v}'`).join(', ') + ']';
|
| 82 |
-
|
| 83 |
document.getElementById('formattedOutput').textContent = formattedOutput;
|
| 84 |
document.getElementById('output').classList.remove('hidden');
|
| 85 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
</script>
|
| 87 |
</body>
|
| 88 |
-
</html>
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Dynamic Input Form with API Suggestion</title>
|
| 7 |
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
|
| 9 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
|
|
|
|
| 12 |
<div class="container mx-auto px-4">
|
| 13 |
<div class="bg-white rounded-lg shadow-md p-6 max-w-lg mx-auto">
|
| 14 |
<h1 class="text-2xl font-bold mb-6 text-center">Dynamic Input Form</h1>
|
| 15 |
+
|
| 16 |
+
<!-- Input for API query -->
|
| 17 |
+
<div class="mb-4">
|
| 18 |
+
<input type="text" id="queryInput" class="form-control border border-gray-300 p-2 rounded-md w-full" placeholder="Enter your query" />
|
| 19 |
+
<button type="button" class="btn btn-info mt-2 w-full" onclick="fetchSuggestion()">Suggestion</button>
|
| 20 |
+
</div>
|
| 21 |
+
|
| 22 |
<form id="dynamicForm" class="space-y-4">
|
| 23 |
<div id="inputContainer" class="space-y-4">
|
| 24 |
<div class="flex items-center space-x-2">
|
|
|
|
| 32 |
<button type="button" class="btn btn-secondary" onclick="resetForm()">Reset</button>
|
| 33 |
</div>
|
| 34 |
</form>
|
| 35 |
+
|
| 36 |
+
<!-- Output container -->
|
| 37 |
<div id="output" class="mt-6 p-4 bg-gray-200 rounded-md hidden">
|
| 38 |
<h2 class="text-lg font-semibold mb-2">Output:</h2>
|
| 39 |
<pre id="formattedOutput" class="text-gray-800 font-mono whitespace-pre-wrap"></pre>
|
|
|
|
| 42 |
</div>
|
| 43 |
|
| 44 |
<script>
|
| 45 |
+
// Add input field
|
| 46 |
function addInput() {
|
| 47 |
const container = document.getElementById('inputContainer');
|
| 48 |
const newInput = document.createElement('div');
|
|
|
|
| 55 |
updateDeleteButtons();
|
| 56 |
}
|
| 57 |
|
| 58 |
+
// Delete input field
|
| 59 |
function deleteInput(button) {
|
| 60 |
button.parentElement.remove();
|
| 61 |
updateDeleteButtons();
|
| 62 |
}
|
| 63 |
|
| 64 |
+
// Update delete button states
|
| 65 |
function updateDeleteButtons() {
|
| 66 |
const deleteButtons = document.querySelectorAll('.delete-btn');
|
| 67 |
deleteButtons.forEach(btn => {
|
|
|
|
| 69 |
});
|
| 70 |
}
|
| 71 |
|
| 72 |
+
// Reset form
|
| 73 |
function resetForm() {
|
| 74 |
const container = document.getElementById('inputContainer');
|
| 75 |
container.innerHTML = `
|
|
|
|
| 82 |
document.getElementById('formattedOutput').textContent = '';
|
| 83 |
}
|
| 84 |
|
| 85 |
+
// Process input value (trim, lowercase, replace spaces with underscores)
|
| 86 |
function processInputValue(value) {
|
|
|
|
| 87 |
return value.trim().toLowerCase().replace(/\s+/g, '_');
|
| 88 |
}
|
| 89 |
|
| 90 |
+
// Submit form event
|
| 91 |
document.getElementById('dynamicForm').addEventListener('submit', function(e) {
|
| 92 |
e.preventDefault();
|
| 93 |
const inputs = document.querySelectorAll('#inputContainer input');
|
| 94 |
const values = Array.from(inputs).map(input => processInputValue(input.value));
|
| 95 |
const formattedOutput = '[' + values.map(v => `'${v}'`).join(', ') + ']';
|
|
|
|
| 96 |
document.getElementById('formattedOutput').textContent = formattedOutput;
|
| 97 |
document.getElementById('output').classList.remove('hidden');
|
| 98 |
});
|
| 99 |
+
|
| 100 |
+
// Refactored query function in JavaScript (API Call)
|
| 101 |
+
async function query(data) {
|
| 102 |
+
const API_URL = "https://subham9126-hfflow.hf.space/api/v1/prediction/447aeeb8-3505-47ac-84c5-6a1e72454f80";
|
| 103 |
+
const headers = {
|
| 104 |
+
"Authorization": "Bearer" $hftoken,
|
| 105 |
+
"Content-Type": "application/json"
|
| 106 |
+
};
|
| 107 |
+
|
| 108 |
+
const response = await fetch(API_URL, {
|
| 109 |
+
method: "POST",
|
| 110 |
+
headers: headers,
|
| 111 |
+
body: JSON.stringify(data)
|
| 112 |
+
});
|
| 113 |
+
|
| 114 |
+
if (!response.ok) {
|
| 115 |
+
throw new Error("Error fetching API data");
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
const result = await response.json();
|
| 119 |
+
return result;
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// Fetch suggestion from the API
|
| 123 |
+
async function fetchSuggestion() {
|
| 124 |
+
const queryValue = document.getElementById('queryInput').value;
|
| 125 |
+
if (!queryValue) {
|
| 126 |
+
alert("Please enter a query!");
|
| 127 |
+
return;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
const data = {
|
| 131 |
+
question: queryValue
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
try {
|
| 135 |
+
const result = await query(data);
|
| 136 |
+
const attributes = result.json.attributes; // Get attributes from response
|
| 137 |
+
|
| 138 |
+
// Now map attributes to input values
|
| 139 |
+
const inputs = document.querySelectorAll('#inputContainer input');
|
| 140 |
+
inputs.forEach((input, index) => {
|
| 141 |
+
if (attributes[index]) {
|
| 142 |
+
input.value = attributes[index]; // Align attribute with input box
|
| 143 |
+
}
|
| 144 |
+
});
|
| 145 |
+
|
| 146 |
+
} catch (error) {
|
| 147 |
+
console.error("Error fetching suggestion:", error);
|
| 148 |
+
alert("Failed to fetch suggestions. Please check your token or network.");
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
</script>
|
| 152 |
</body>
|
| 153 |
+
</html>
|