Subham9126 commited on
Commit
3b15067
·
verified ·
1 Parent(s): 8740b8e

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +39 -27
index.html CHANGED
@@ -12,15 +12,21 @@
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">
25
  <input type="text" class="form-control border border-gray-300 p-2 rounded-md flex-grow" placeholder="Enter value">
26
  <button type="button" class="btn btn-danger delete-btn" onclick="deleteInput(this)" disabled>Delete</button>
@@ -97,45 +103,51 @@
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]) {
@@ -145,7 +157,7 @@
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>
 
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
+ <!-- Input for API key -->
23
+ <div class="mb-4">
24
+ <input type="password" id="apiKeyInput" class="form-control border border-gray-300 p-2 rounded-md w-full" placeholder="Enter your API key" />
25
+ </div>
26
+
27
  <form id="dynamicForm" class="space-y-4">
28
  <div id="inputContainer" class="space-y-4">
29
+ <!-- Initial Input Box -->
30
  <div class="flex items-center space-x-2">
31
  <input type="text" class="form-control border border-gray-300 p-2 rounded-md flex-grow" placeholder="Enter value">
32
  <button type="button" class="btn btn-danger delete-btn" onclick="deleteInput(this)" disabled>Delete</button>
 
103
  document.getElementById('output').classList.remove('hidden');
104
  });
105
 
106
+ // Fetch suggestion from the API and dynamically adjust the input boxes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  async function fetchSuggestion() {
108
  const queryValue = document.getElementById('queryInput').value;
109
+ const apiKey = document.getElementById('apiKeyInput').value;
110
  if (!queryValue) {
111
  alert("Please enter a query!");
112
  return;
113
  }
114
+ if (!apiKey) {
115
+ alert("Please enter your API key!");
116
+ return;
117
+ }
118
 
119
  const data = {
120
  question: queryValue
121
  };
122
 
123
  try {
124
+ const response = await fetch('https://subham9126-hfflow.hf.space/api/v1/prediction/447aeeb8-3505-47ac-84c5-6a1e72454f80', {
125
+ method: "POST",
126
+ headers: {
127
+ "Authorization": `Bearer ${apiKey}`,
128
+ "Content-Type": "application/json"
129
+ },
130
+ body: JSON.stringify(data)
131
+ });
132
+
133
+ if (!response.ok) {
134
+ throw new Error("Error fetching suggestion");
135
+ }
136
+
137
+ const result = await response.json();
138
  const attributes = result.json.attributes; // Get attributes from response
139
 
140
+ const inputContainer = document.getElementById('inputContainer');
141
+ const currentInputCount = document.querySelectorAll('#inputContainer input').length;
142
+
143
+ // If the number of attributes is greater than current input boxes, add more boxes
144
+ if (attributes.length > currentInputCount) {
145
+ for (let i = currentInputCount; i < attributes.length; i++) {
146
+ addInput();
147
+ }
148
+ }
149
+
150
+ // Update the values of the input boxes to match the attributes
151
  const inputs = document.querySelectorAll('#inputContainer input');
152
  inputs.forEach((input, index) => {
153
  if (attributes[index]) {
 
157
 
158
  } catch (error) {
159
  console.error("Error fetching suggestion:", error);
160
+ alert("Failed to fetch suggestions. Please check your API key or network.");
161
  }
162
  }
163
  </script>