forked from swift502/Sketchbook
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest2.html
More file actions
125 lines (117 loc) · 4.2 KB
/
test2.html
File metadata and controls
125 lines (117 loc) · 4.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenRouter Image Upload</title>
</head>
<body>
<div class="container">
<h2>Send Image and Message to OpenRouter</h2>
<form id="uploadForm">
<div>
<label for="image">Select Image:</label>
<input type="file" id="image" accept="image/*" required>
</div>
<div>
<label for="message">Message:</label>
<textarea id="message" rows="4" required></textarea>
</div>
<button type="submit">Send to OpenRouter</button>
</form>
<div id="result"></div>
</div>
<script>
document.getElementById('uploadForm').addEventListener('submit', async (e) => {
e.preventDefault();
const imageFile = document.getElementById('image').files[0];
const message = document.getElementById('message').value;
const resultDiv = document.getElementById('result');
// Convert image to base64
const base64Image = await convertToBase64(imageFile);
// Prepare the request body
const requestBody = {
messages: [
{
role: "user",
content: [
{
type: "text",
text: message
},
{
type: "image_url",
image_url: {
url: base64Image
}
}
]
}
],
model: "meta-llama/llama-3.2-90b-vision-instruct", // Replace with desired model
stream: false
};
try {
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer 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', // Replace with your API key
'Content-Type': 'application/json',
'HTTP-Referer': window.location.origin, // Required by OpenRouter
'X-Title': 'Image Upload Demo' // Optional identifier
},
body: JSON.stringify(requestBody)
});
const data = await response.json();
resultDiv.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
} catch (error) {
resultDiv.innerHTML = `Error: ${error.message}`;
}
});
// Helper function to convert image to base64
function convertToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
</script>
<style>
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
form > div {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
}
textarea {
width: 100%;
padding: 8px;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#result {
margin-top: 20px;
padding: 10px;
background-color: #f8f9fa;
border-radius: 4px;
}
</style>
</body>
</html>