Skip to content

Latest commit

 

History

History
110 lines (86 loc) · 3.98 KB

File metadata and controls

110 lines (86 loc) · 3.98 KB

Model Selection Feature - Implementation Summary

Overview

Added the ability for users to select an image generation model from a dropdown list. The selected model is persisted using the browser's localStorage and automatically restored on subsequent visits.

Changes Made

1. Server-Side (server.py)

  • Existing /models endpoint (lines 34-82): Already implemented to fetch available models from Ollama API and filter them based on IMAGE_GEN_MODEL_LIST
  • Added /test.html route (lines 34-46): Serves a test page for verifying the model selection functionality

2. Frontend (index.html)

CSS Styling (lines 195-227)

Added comprehensive styling for the <select> element:

  • Custom dropdown with gradient border on focus
  • Custom SVG arrow icon (embedded as data URI)
  • Dark theme styling for options
  • Smooth transitions and hover effects
  • Consistent with existing design system

HTML Structure (lines 589-594)

Added a new form group before the prompt input:

<div class="form-group">
    <label for="modelSelect">Select Model</label>
    <select id="modelSelect" required>
        <option value="">Loading models...</option>
    </select>
</div>

JavaScript Implementation (lines 661-701)

Constants and Variables:

  • MODEL_STORAGE_KEY = 'ollama_selected_model' - localStorage key for persisting model selection

loadModels() Function:

  1. Fetches available models from /models API endpoint
  2. Populates the dropdown with available models
  3. Loads previously selected model from localStorage
  4. Falls back to first model if no saved preference exists
  5. Handles errors gracefully with user-friendly messages

Event Listener:

  • Saves selected model to localStorage whenever the user changes the selection

Integration:

  • Modified the image generation request (line 783) to use modelSelect.value instead of hardcoded model name

How It Works

Initial Page Load

  1. Page loads and loadModels() is called automatically
  2. Function fetches models from /models API endpoint
  3. Dropdown is populated with available models
  4. If a model was previously selected (stored in localStorage), it's automatically selected
  5. Otherwise, the first available model is selected by default

User Interaction

  1. User selects a model from the dropdown
  2. Selection is immediately saved to localStorage via the change event listener
  3. When user generates an image, the selected model is used in the API request

Persistence

  • Model selection is stored in browser's localStorage with key 'ollama_selected_model'
  • Persists across browser sessions
  • Automatically restored when user revisits the page
  • Scoped to the domain (localhost:8080)

API Endpoint

GET /models

Response: JSON array of available model names

["x/z-image-turbo:bf16", "x/flux2-klein"]

Example:

curl http://localhost:8080/models

Testing

A test page (test.html) has been created to verify the implementation:

  • Access at: http://localhost:8080/test.html
  • Tests include:
    1. API endpoint verification
    2. localStorage save/load/clear operations
    3. Full integration test with dropdown population

Files Modified

  1. /Users/nick/AntigravityProjects/OllamaImageGenerator/index.html - Added model selection UI and logic
  2. /Users/nick/AntigravityProjects/OllamaImageGenerator/server.py - Added test.html route
  3. /Users/nick/AntigravityProjects/OllamaImageGenerator/README.md - Updated documentation

Files Created

  1. /Users/nick/AntigravityProjects/OllamaImageGenerator/test.html - Test page for verification

Browser Compatibility

  • Uses standard Web Storage API (localStorage)
  • Compatible with all modern browsers (Chrome, Firefox, Safari, Edge)
  • Gracefully handles cases where localStorage is unavailable

Error Handling

  • API fetch errors are caught and displayed to the user
  • Empty model list is handled with appropriate message
  • Invalid saved model (not in current list) falls back to first available model
  • Console logging for debugging purposes