<!DOCTYPE html>
<html>
<head>
<title>Model List App</title>
<style>
body {
font-family: sans-serif;
margin: 20px;
}
#model-list {
list-style-type: none;
padding: 0;
}
#model-list li {
margin-bottom: 5px;
}
#loading-message {
text-align: center;
font-style: italic;
color: gray;
}
</style>
</head>
<body>
<h1>Available Models</h1>
<div id="loading-message">Loading models...</div>
<ul id="model-list"></ul>
<script>
async function getModels() {
try {
const response = await fetch('https://puter.com/puterai/chat/models');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.text();
// Simple parsing - adjust based on the actual structure of the HTML content
const modelList = data.match(/<li.*?class="model-item.*?>(.*?)</li>/g);
const models = [];
if (modelList) {
modelList.forEach(match => {
models.push(match.trim());
});
}
return models;
} catch (error) {
console.error('Error fetching models:', error);
document.getElementById('loading-message').textContent = 'Error loading models. Please try again later.';
}
}
async function displayModels() {
const models = await getModels();
const modelListElement = document.getElementById('model-list');
if (models.length > 0) {
models.forEach(modelName => {
const listItem = document.createElement('li');
listItem.textContent = modelName;
modelListElement.appendChild(listItem);
});
} else {
document.getElementById('model-list').innerHTML = '<li style="color:gray;">No models found.</li>';
}
}
// Call the function to display models when the page loads
displayModels();
</script>
</body>
</html>
Explanation:
-
HTML Structure:
- Sets up a basic HTML page with a title and a heading.
- Includes a
div with the ID loading-message to display a loading indicator.
- Creates an unordered list (
ul) with the ID model-list where the model names will be added.
-
CSS Styling:
- Provides basic styling for the body, list, and loading message to make the app presentable.
-
JavaScript Logic:
getModels() (async function):
- Uses the
fetch() API to make an asynchronous request to the provided URL (https://puter.com/puterai/chat/models).
- Includes error handling using a
try...catch block to catch potential network errors or issues with the response.
- Checks if the response is successful (
response.ok).
- Parses the HTML content of the response. Note: The parsing logic (
data.match(/<li.*?class="model-item.*?>(.*?)</li>/g)) is based on the assumption that the model names are within <li> elements with the class model-item. You might need to adjust this regular expression if the HTML structure of the website changes.
- Returns an array of model names.
displayModels() (async function):
- Calls
getModels() to fetch the list of models.
- Gets a reference to the
model-list element.
- If models are found:
- Iterates through the
models array.
- For each model name, creates a new
<li> element.
- Sets the text content of the
<li> to the model name.
- Appends the
<li> to the model-list <ul>.
- If no models are found, it displays a "No models found" message in the list.
displayModels(); (at the end):
- Calls the
displayModels() function when the page loads to initiate the process of fetching and displaying the model names.
How to Run:
- Save the code as an HTML file (e.g.,
models.html).
- Open the HTML file in your web browser.
The app will first display "Loading models..." and then, once the data is fetched and parsed, it will list the available model names.
Important Note on Parsing:
The regular expression used in getModels() (/<li.*?class="model-item.*?>(.*?)</li>/g) is a simple way to extract the model names. However, if the HTML structure of the website changes, this regex might break. For more robust parsing, you might consider using a more advanced HTML parsing library like cheerio (for server-side or more complex client-side parsing) or carefully inspecting the website's HTML structure to find a more reliable way to extract the data.