-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAI_Web_Controller.html
More file actions
396 lines (354 loc) · 18 KB
/
AI_Web_Controller.html
File metadata and controls
396 lines (354 loc) · 18 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Streamlit Web Runner</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', sans-serif;
background-color: #111827; /* Dark background */
color: #d1d5db; /* Light gray text */
}
.log-table th, .log-table td {
word-break: break-word;
}
.status-success { color: #22c55e; } /* Green */
.status-client-error { color: #f97316; } /* Orange */
.status-server-error { color: #ef4444; } /* Red */
.status-pending { color: #eab308; } /* Yellow */
.card {
background-color: #1f2937;
border: 1px solid #374151;
}
.btn-primary {
background-color: #3b82f6;
transition: background-color 0.3s;
}
.btn-primary:hover {
background-color: #2563eb;
}
.btn-secondary {
background-color: #4b5563;
transition: background-color 0.3s;
}
.btn-secondary:hover {
background-color: #374151;
}
input[type="text"], input[type="number"], textarea {
background-color: #374151;
border-color: #4b5563;
}
.details-row {
display: none;
background-color: #1f2937;
}
.details-cell {
padding: 1rem;
border-top: 1px solid #374151;
}
.details-cell pre {
background-color: #111827;
padding: 0.75rem;
border-radius: 0.5rem;
white-space: pre-wrap;
word-break: break-all;
color: #d1d5db;
}
.toggle-btn {
cursor: pointer;
font-family: monospace;
font-weight: bold;
color: #9ca3af;
}
</style>
</head>
<body class="antialiased">
<div class="container mx-auto p-4 md:p-8 max-w-7xl">
<header class="text-center mb-8">
<h1 class="text-4xl font-bold text-white">Remote Control & Web Runner</h1>
<p class="text-lg text-gray-400 mt-2">Send HTTP requests to a web service endpoint and log the I/O.
</p>
</header>
<!-- Instructions Section -->
<div class="card p-6 rounded-lg shadow-lg mb-8 border-l-4 border-blue-500">
<h2 class="text-2xl font-semibold text-white mb-4">How to Interact with a Streamlit App</h2>
<div class="p-4 rounded-md bg-yellow-900/50 border border-yellow-700 mb-4">
<h3 class="font-bold text-yellow-300">Why You Are Seeing a 404 Error</h3>
<p class="text-yellow-300 text-sm mt-1">
The URL you provided is for the Streamlit application's user interface. This UI is loaded into your browser and communicates with the server using a WebSocket, not a standard HTTP POST endpoint. Sending a POST request directly to the app's main URL will almost always result in a <strong>404 Not Found</strong> error because no such API endpoint exists at that address. This is the expected behavior.
</p>
</div>
<p class="mb-3 text-gray-300">
This tool is designed for testing a <strong>custom REST API endpoint</strong> that you run alongside your Streamlit app. For example, if you use a framework like FastAPI or Flask in your Python code to create an endpoint (e.g., `/api/process_data`) that accepts POST requests, you would enter that specific URL below to test it.
</p>
<p class="mt-4 text-sm text-gray-500">
<strong>SSL/HTTPS vs. non-SSL/HTTP:</strong> Since you are running this from a plain `http` page, the "Use CORS Proxy" option is essential for sending requests to a secure `https` URL like the one on Hugging Face. It resolves browser security errors related to cross-origin requests.
</p>
</div>
<!-- Configuration Section -->
<div class="card p-6 rounded-lg shadow-lg mb-8">
<h2 class="text-2xl font-semibold text-white mb-4">Configuration</h2>
<div class="space-y-4">
<div class="flex flex-col md:flex-row items-center gap-4">
<label for="endpointUrl" class="font-medium text-white flex-shrink-0">Endpoint URL:</label>
<input type="text" id="endpointUrl" class="flex-grow w-full p-2 rounded-md text-gray-200 focus:ring-2 focus:ring-blue-500 focus:outline-none" placeholder="https://your-app-url.com/YOUR_CUSTOM_API_ENDPOINT" value="https://huggingface.co/spaces/awacke1/GPT-4o-omni-text-audio-image-video">
</div>
<div class="flex items-center pt-2">
<input id="useCorsProxy" type="checkbox" checked class="h-4 w-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500 bg-gray-700 border-gray-600">
<label for="useCorsProxy" class="ml-2 block text-sm text-gray-300">Use CORS Proxy</label>
<p class="ml-4 text-xs text-gray-500">Use if you get a "Failed to fetch" error calling a remote API.</p>
</div>
</div>
</div>
<!-- Controls Section -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
<!-- Simple POST Request -->
<div class="card p-6 rounded-lg shadow-lg">
<h2 class="text-2xl font-semibold text-white mb-4">Simple POST Request</h2>
<div class="space-y-4">
<div>
<label for="simplePromptInput" class="block text-sm font-medium text-gray-300 mb-1">Message</label>
<textarea id="simplePromptInput" rows="5" class="w-full p-2 rounded-md bg-gray-700 border border-gray-600 text-gray-200 focus:ring-2 focus:ring-blue-500 focus:outline-none">Summarize the top 3 python libraries for PDF processing.</textarea>
</div>
<button id="sendSimpleBtn" class="w-full btn-primary text-white font-bold py-2 px-4 rounded-md">Send as {"prompt": "..."}</button>
</div>
</div>
<!-- Custom Request -->
<div class="card p-6 rounded-lg shadow-lg">
<h2 class="text-2xl font-semibold text-white mb-4">Full Custom Request</h2>
<div class="mt-6">
<h3 class="text-xl font-semibold text-white mb-3">Request Body (JSON)</h3>
<textarea id="customRequest" class="w-full p-2 rounded-md bg-gray-700 border border-gray-600 text-gray-200 focus:ring-2 focus:ring-blue-500 focus:outline-none" rows="8"></textarea>
<button id="sendCustomBtn" class="mt-3 w-full btn-secondary text-white font-bold py-2 px-4 rounded-md">Send Custom JSON</button>
</div>
</div>
</div>
<!-- Logging Section -->
<div class="card p-6 rounded-lg shadow-lg mt-8">
<div class="flex justify-between items-center mb-4">
<h2 class="text-2xl font-semibold text-white">Interaction Log</h2>
<button id="clearLogBtn" class="btn-secondary text-white font-bold py-2 px-4 rounded-md">Clear Log</button>
</div>
<div class="overflow-x-auto">
<table class="w-full text-sm text-left text-gray-400 log-table">
<thead class="text-xs text-gray-300 uppercase bg-gray-700">
<tr>
<th scope="col" class="px-6 py-3 rounded-l-lg w-8"></th>
<th scope="col" class="px-6 py-3">Timestamp</th>
<th scope="col" class="px-6 py-3">Method</th>
<th scope="col" class="px-6 py-3">URL</th>
<th scope="col" class="px-6 py-3">Status</th>
<th scope="col" class="px-6 py-3">Duration</th>
</tr>
</thead>
<tbody id="logBody">
<!-- Log rows will be inserted here -->
</tbody>
</table>
</div>
</div>
</div>
<!-- Modal container -->
<div id="notificationModal" class="fixed inset-0 bg-black bg-opacity-50 z-50 hidden items-center justify-center p-4">
<div class="card p-6 rounded-lg shadow-lg max-w-sm w-full">
<h3 id="modalTitle" class="text-xl font-semibold text-white mb-4">Notification</h3>
<p id="modalMessage" class="text-gray-300 mb-6"></p>
<button id="modalCloseBtn" class="btn-primary text-white font-bold py-2 px-4 rounded-md w-full">Close</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const endpointUrlInput = document.getElementById('endpointUrl');
const logBody = document.getElementById('logBody');
const clearLogBtn = document.getElementById('clearLogBtn');
// Simple controls
const simplePromptInput = document.getElementById('simplePromptInput');
const sendSimpleBtn = document.getElementById('sendSimpleBtn');
// Custom controls
const customRequestTextarea = document.getElementById('customRequest');
const sendCustomBtn = document.getElementById('sendCustomBtn');
const useCorsProxyCheckbox = document.getElementById('useCorsProxy');
const notificationModal = document.getElementById('notificationModal');
const modalMessage = document.getElementById('modalMessage');
const modalCloseBtn = document.getElementById('modalCloseBtn');
const PROXY_URL = 'https://corsproxy.io/?';
// Set initial example JSON
customRequestTextarea.value = JSON.stringify({
"query": "What are the key findings?",
"document_ids": [101, 102, 105],
"max_results": 5
}, null, 2);
function showNotification(message) {
modalMessage.textContent = message;
notificationModal.classList.remove('hidden');
notificationModal.classList.add('flex');
}
modalCloseBtn.addEventListener('click', () => {
notificationModal.classList.add('hidden');
notificationModal.classList.remove('flex');
});
sendSimpleBtn.addEventListener('click', () => {
const prompt = simplePromptInput.value;
if(!prompt.trim()) {
showNotification('Message cannot be empty.');
return;
}
const payload = {
prompt: prompt
};
sendRequest(payload);
});
sendCustomBtn.addEventListener('click', () => {
const commandString = customRequestTextarea.value;
if (!commandString.trim()) {
showNotification('Custom request cannot be empty.');
return;
}
try {
const command = JSON.parse(commandString);
sendRequest(command);
} catch (e) {
showNotification('Invalid JSON in custom request.');
console.error('JSON Parse Error:', e);
}
});
clearLogBtn.addEventListener('click', () => {
logBody.innerHTML = '';
});
function formatHeaders(headers) {
let headerString = '';
for (const [key, value] of headers.entries()) {
headerString += `${key}: ${value}\n`;
}
return headerString;
}
function getStatusClass(statusCode) {
if (statusCode >= 500) return 'status-server-error';
if (statusCode >= 400) return 'status-client-error';
if (statusCode >= 200 && statusCode < 300) return 'status-success';
return 'status-pending';
}
function logInteraction(logData) {
const { request, response, duration, url, method } = logData;
const row = logBody.insertRow(0);
const detailsRow = logBody.insertRow(1);
detailsRow.classList.add('details-row');
const statusClass = getStatusClass(response.status);
// Main Row
row.innerHTML = `
<td class="px-6 py-4 align-top"><span class="toggle-btn">[+]</span></td>
<td class="px-6 py-4 align-top">${new Date().toLocaleString()}</td>
<td class="px-6 py-4 align-top">${method}</td>
<td class="px-6 py-4 align-top">${url}</td>
<td class="px-6 py-4 align-top font-semibold"><span class="${statusClass}">${response.status} ${response.statusText}</span></td>
<td class="px-6 py-4 align-top">${duration} ms</td>
`;
// Details Row
const detailsCell = detailsRow.insertCell(0);
detailsCell.colSpan = 6;
detailsCell.classList.add('details-cell');
const formatJson = (data) => JSON.stringify(data, null, 2);
const bodyContent = (data) => {
if (typeof data === 'string') return data;
if (data) return formatJson(data);
return 'N/A';
};
detailsCell.innerHTML = `
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<h4 class="font-semibold text-white mb-2">Request Headers</h4>
<pre>${formatHeaders(request.headers)}</pre>
<h4 class="font-semibold text-white mt-4 mb-2">Request Body</h4>
<pre>${bodyContent(request.body)}</pre>
</div>
<div>
<h4 class="font-semibold text-white mb-2">Response Headers</h4>
<pre>${response.headers ? formatHeaders(response.headers) : 'N/A'}</pre>
<h4 class="font-semibold text-white mt-4 mb-2">Response Body</h4>
<pre>${bodyContent(response.body)}</pre>
</div>
</div>
`;
// Toggle functionality
const toggleBtn = row.querySelector('.toggle-btn');
toggleBtn.addEventListener('click', () => {
if (detailsRow.style.display === 'table-row') {
detailsRow.style.display = 'none';
toggleBtn.textContent = '[+]';
} else {
detailsRow.style.display = 'table-row';
toggleBtn.textContent = '[-]';
}
});
}
function getUrl() {
let url = endpointUrlInput.value;
if(useCorsProxyCheckbox.checked) {
return `${PROXY_URL}${encodeURIComponent(url)}`;
}
return url;
}
async function sendRequest(command) {
if (!endpointUrlInput.value) {
showNotification('Please set the endpoint URL first.');
return;
}
const url = getUrl();
const startTime = Date.now();
const method = 'POST';
const requestHeaders = new Headers({
'Content-Type': 'application/json'
});
try {
const response = await fetch(url, {
method: method,
headers: requestHeaders,
body: JSON.stringify(command),
});
const duration = Date.now() - startTime;
let responseBody;
if (response.status === 204) {
responseBody = "[204 No Content]";
} else {
const responseText = await response.text();
try {
responseBody = JSON.parse(responseText);
} catch (e) {
responseBody = responseText; // Fallback to raw text if not JSON
}
}
logInteraction({
request: { headers: requestHeaders, body: command },
response: {
status: response.status,
statusText: response.statusText,
headers: response.headers,
body: responseBody,
},
duration,
url: endpointUrlInput.value,
method,
});
} catch (error) {
const duration = Date.now() - startTime;
logInteraction({
request: { headers: requestHeaders, body: command },
response: {
status: 'Error',
statusText: 'Failed to Fetch',
body: error.message
},
duration,
url: endpointUrlInput.value,
method
});
console.error('Fetch Error:', error);
}
}
});
</script>
</body>
</html>