-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsse_test.html
More file actions
191 lines (167 loc) · 6.93 KB
/
sse_test.html
File metadata and controls
191 lines (167 loc) · 6.93 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
<!DOCTYPE html>
<html>
<head>
<title>Image Conversion SSE Test</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.log {
background: #f5f5f5;
border: 1px solid #ddd;
padding: 10px;
height: 300px;
overflow-y: auto;
margin: 10px 0;
}
.event { margin: 5px 0; padding: 5px; }
.connection { background: #e3f2fd; }
.progress { background: #fff3e0; }
.result { background: #e8f5e8; }
.error { background: #ffebee; }
input, select { margin: 5px; padding: 5px; }
button { padding: 10px 20px; margin: 5px; }
</style>
</head>
<body>
<h1>Image Conversion with SSE</h1>
<form id="uploadForm">
<div>
<input type="file" id="fileInput" accept="image/*" required>
</div>
<div>
<input type="text" id="azureEndpoint" placeholder="Azure OpenAI Endpoint" required>
</div>
<div>
<input type="password" id="apiKey" placeholder="API Key" required>
</div>
<div>
<input type="text" id="deploymentName" placeholder="Deployment Name" required>
</div>
<div>
<input type="text" id="apiVersion" placeholder="API Version" value="2024-02-01">
</div>
<div>
<label>
<input type="checkbox" id="enhanceMarkdown"> Enhance Markdown
</label>
</div>
<div>
<button type="button" onclick="convertWithSSE()">Convert with SSE</button>
<button type="button" onclick="convertTraditional()">Convert Traditional</button>
</div>
</form>
<h2>Event Stream Log</h2>
<div id="log" class="log"></div>
<h2>Result</h2>
<textarea id="result" style="width: 100%; height: 200px;"></textarea>
<script>
let eventSource = null;
function log(message, eventType = 'info') {
const logDiv = document.getElementById('log');
const eventDiv = document.createElement('div');
eventDiv.className = `event ${eventType}`;
eventDiv.innerHTML = `<strong>${new Date().toLocaleTimeString()}</strong> [${eventType}] ${message}`;
logDiv.appendChild(eventDiv);
logDiv.scrollTop = logDiv.scrollHeight;
}
function convertWithSSE() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
if (!file) {
alert('Please select a file');
return;
}
// Close existing connection
if (eventSource) {
eventSource.close();
}
// Clear log and result
document.getElementById('log').innerHTML = '';
document.getElementById('result').value = '';
const formData = new FormData();
formData.append('file', file);
formData.append('azure_endpoint', document.getElementById('azureEndpoint').value);
formData.append('api_key', document.getElementById('apiKey').value);
formData.append('deployment_name', document.getElementById('deploymentName').value);
formData.append('api_version', document.getElementById('apiVersion').value);
formData.append('enhance_markdown', document.getElementById('enhanceMarkdown').checked);
// Start SSE connection
fetch('/convert-image/stream', {
method: 'POST',
body: formData
}).then(response => {
const reader = response.body.getReader();
const decoder = new TextDecoder();
function readStream() {
reader.read().then(({ done, value }) => {
if (done) {
log('Stream ended', 'info');
return;
}
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('event:')) {
// Event type line
continue;
} else if (line.startsWith('data:')) {
try {
const data = JSON.parse(line.substring(5));
handleSSEEvent(data);
} catch (e) {
log(`Failed to parse data: ${line}`, 'error');
}
}
}
readStream();
}).catch(error => {
log(`Stream error: ${error.message}`, 'error');
});
}
readStream();
}).catch(error => {
log(`Connection error: ${error.message}`, 'error');
});
}
function handleSSEEvent(data) {
const status = data.status;
const message = data.message;
log(message, status);
if (status === 'completed' && data.result) {
document.getElementById('result').value = data.result.markdown;
}
}
function convertTraditional() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
if (!file) {
alert('Please select a file');
return;
}
const formData = new FormData();
formData.append('file', file);
formData.append('azure_endpoint', document.getElementById('azureEndpoint').value);
formData.append('api_key', document.getElementById('apiKey').value);
formData.append('deployment_name', document.getElementById('deploymentName').value);
formData.append('api_version', document.getElementById('apiVersion').value);
formData.append('enhance_markdown', document.getElementById('enhanceMarkdown').checked);
log('Starting traditional conversion...', 'info');
fetch('/convert-image', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
log('Conversion completed successfully', 'result');
document.getElementById('result').value = data.markdown;
} else {
log(`Conversion failed: ${data.error}`, 'error');
}
})
.catch(error => {
log(`Request failed: ${error.message}`, 'error');
});
}
</script>
</body>
</html>