interstellar_ai/app/components/Models.tsx

323 lines
9.7 KiB
TypeScript
Raw Normal View History

2024-09-30 08:41:06 +02:00
"use client";
2024-09-26 09:45:31 +02:00
import React, { useState, useEffect } from 'react';
2024-09-18 10:03:36 +02:00
2024-09-30 10:43:22 +02:00
// Define all models that should be available.
const modelList = {
'Offline Fast': {
'model_type': 'local',
2024-09-30 10:43:22 +02:00
'Math': 'qwen2-math:1.5b',
'Code': 'starcoder2',
'Language': 'llama3.2',
'Character': 'dolphin-phi',
'Finance': 'qwen2-math:1.5b',
'Weather': 'llama3.2',
'Time': 'llama3.2',
'Image': 'llava-phi3'
},
'Offline Slow': {
'model_type': 'local',
2024-09-30 10:43:22 +02:00
'Math': 'wizard-math',
'Code': 'starcoder2:7b',
'Language': 'llama3.1',
'Character': 'dolphin-llama3',
'Finance': 'wizard-math',
'Weather': 'llama3.1',
'Time': 'llama3.1',
'Image': 'llava'
},
'Offline Fast (FOSS)': {
'model_type': 'local',
2024-09-30 10:43:22 +02:00
'Math': 'qwen2-math:1.5b',
'Code': 'qwen2.5-coder:1.5b',
'Language': 'phi3.5',
'Character': 'dolphin-mistral',
'Finance': 'qwen2-math:1.5b',
'Weather': 'phi3.5',
'Time': 'phi3.5',
'Image': 'llava'
},
'Offline Slow (FOSS)': {
'model_type': 'local',
2024-09-30 10:43:22 +02:00
'Math': 'mathstral',
'Code': 'qwen2.5-coder',
'Language': 'qwen2.5',
'Character': 'dolphin-mistral',
'Finance': 'mathstral',
'Weather': 'qwen2.5',
'Time': 'qwen2.5',
'Image': 'llava'
},
'Online Cheap (OpenAI)': {
'model_type': 'openai',
2024-09-30 10:43:22 +02:00
'Math': 'gpt-4o-mini',
'Code': 'gpt-4o-mini',
'Language': 'gpt-4o-mini',
'Character': 'gpt-4o-mini',
'Finance': 'gpt-4o-mini',
'Weather': 'gpt-4o-mini',
'Time': 'gpt-4o-mini',
'Image': 'gpt-4o-mini'
},
'Online Expensive (OpenAI)': {
'model_type': 'openai',
2024-09-30 10:43:22 +02:00
'Math': 'gpt-4o',
'Code': 'gpt-4o',
'Language': 'gpt-4o',
'Character': 'gpt-4o',
'Finance': 'gpt-4o',
'Weather': 'gpt-4o',
'Time': 'gpt-4o',
'Image': 'gpt-4o'
},
'Online Cheap (Anthropic)': {
'model_type': 'anthropic',
2024-09-30 10:43:22 +02:00
'Math': 'claude-3-haiku',
'Code': 'claude-3-haiku',
'Language': 'claude-3-haiku',
'Character': 'claude-3-haiku',
'Finance': 'claude-3-haiku',
'Weather': 'claude-3-haiku',
'Time': 'claude-3-haiku',
'Image': 'claude-3-haiku'
},
'Online Expensive (Anthropic)': {
'model_type': 'anthropic',
2024-09-30 10:43:22 +02:00
'Math': 'claude-3-5-sonnet',
'Code': 'claude-3-5-sonnet',
'Language': 'claude-3-5-sonnet',
'Character': 'claude-3-5-sonnet',
'Finance': 'claude-3-5-sonnet',
'Weather': 'claude-3-5-sonnet',
'Time': 'claude-3-5-sonnet',
'Image': 'claude-3-5-sonnet'
},
'Online Cheap (Google)': {
'model_type': 'google',
2024-09-30 10:43:22 +02:00
'Math': 'gemini-1.5-flash-latest',
'Code': 'gemini-1.5-flash-latest',
'Language': 'gemini-1.5-flash-latest',
'Character': 'gemini-1.5-flash-latest',
'Finance': 'gemini-1.5-flash-latest',
'Weather': 'gemini-1.5-flash-latest',
'Time': 'gemini-1.5-flash-latest',
'Image': 'gemini-1.5-flash-latest'
},
'Online Expensive (Google)': {
'model_type': 'google',
2024-09-30 10:43:22 +02:00
'Math': 'gemini-1.5-pro-latest',
'Code': 'gemini-1.5-pro-latest',
'Language': 'gemini-1.5-pro-latest',
'Character': 'gemini-1.5-pro-latest',
'Finance': 'gemini-1.5-pro-latest',
'Weather': 'gemini-1.5-pro-latest',
'Time': 'gemini-1.5-pro-latest',
'Image': 'gemini-1.5-pro-latest'
},
'Online (La Plateforme)': {
'model_type': 'mistral',
2024-09-30 10:43:22 +02:00
'Math': 'open-mistral-nemo',
'Code': 'codestral-latest',
'Language': 'mistral-small-latest',
'Character': 'mistral-large-latest',
'Finance': 'open-mistral-nemo',
'Weather': 'mistral-small-latest',
'Time': 'mistral-small-latest',
'Image': 'pixtral-12b-2409'
},
'Online (FOSS) (La Plateforme)': {
'model_type': 'mistral',
2024-09-30 10:43:22 +02:00
'Math': 'open-mistral-nemo',
'Code': 'open-codestral-mamba',
'Language': 'open-mistral-nemo',
'Character': 'open-mixtral-8x22b',
'Finance': 'open-mixtral-8x22b',
'Weather': 'open-mistral-nemo',
'Time': 'open-mistral-nemo',
'Image': 'pixtral-12b-2409'
}
}
// Define the available category options
2024-09-26 12:31:56 +02:00
const modelDropdown = {
offlineWithoutFoss: ['Offline Fast', 'Offline Slow'],
offlineFoss: ['Offline Fast (FOSS)', 'Offline Slow (FOSS)'],
onlineWithoutFoss: [
2024-09-26 12:31:56 +02:00
'Online Cheap (OpenAI)',
'Online Expensive (OpenAI)',
'Online Cheap (Anthropic)',
'Online Expensive (Anthropic)',
'Online Cheap (Google)',
'Online Expensive (Google)',
2024-09-30 10:43:22 +02:00
'Online (La Plateforme)'
2024-09-26 12:31:56 +02:00
],
onlineFoss: ['Online (FOSS) (La Plateforme)'],
2024-09-26 12:31:56 +02:00
};
const Category = [
'Code',
'Math',
'Language',
'Character',
'Finance',
'Weather',
'Time',
'Image',
'Custom1',
'Custom2'
]
2024-09-26 12:31:56 +02:00
const Models: React.FC = () => {
// Initialize state with value from localStorage or default to ''
const [selectedModel, setSelectedModel] = useState('');
2024-09-30 08:41:06 +02:00
const [radioSelection, setRadioSelection] = useState<string | null>("")
const [activeCategory, setActiveCategory] = useState('');
const [currentCategory, setCurrentCategory] = useState(localStorage.getItem("activeCategory"));
2024-09-30 10:43:22 +02:00
2024-09-26 12:31:56 +02:00
useEffect(() => {
2024-10-02 08:24:01 +02:00
if (!localStorage.getItem('selectedModel')) {
localStorage.setItem("selectedModel", "Offline Fast" )
}
if (!localStorage.getItem("activeCategory")) {
setActiveCategory('Code')
localStorage.setItem('activeCategory' ,'Code')
}
if (!localStorage.getItem("model")) {
localStorage.setItem("model" ,'starcoder2')
}
2024-09-26 12:31:56 +02:00
const handleStorageChange = () => {
setSelectedModel(localStorage.getItem('selectedModel') || '');
2024-09-26 12:31:56 +02:00
};
2024-09-25 12:53:54 +02:00
// Update immediately when localStorage changes
2024-09-26 12:31:56 +02:00
window.addEventListener('storage', handleStorageChange);
2024-10-02 08:24:01 +02:00
setRadioSelection(localStorage.getItem('radioSelection') || '');
setSelectedModel(localStorage.getItem('selectedModel') || '');
2024-09-26 12:31:56 +02:00
// Cleanup listener on component unmount
return () => {
window.removeEventListener('storage', handleStorageChange);
};
}, []); // Dependency array can remain empty if you only want this to run on mount
useEffect(() => {
const storedCategory = localStorage.getItem("activeCategory");
if (storedCategory !== currentCategory) {
setCurrentCategory(storedCategory);
}
}, [activeCategory]);
2024-09-25 13:33:51 +02:00
2024-09-26 09:45:31 +02:00
const handleModelChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const newModel = event.target.value;
setSelectedModel(newModel);
2024-10-01 08:18:14 +02:00
localStorage.setItem('selectedModel', newModel); // Update localStorage directly
2024-09-26 09:45:31 +02:00
};
2024-09-26 09:29:04 +02:00
2024-09-26 12:31:56 +02:00
// Determine the filtered models based on current radioSelection
2024-09-26 09:45:31 +02:00
const filteredModels = (() => {
2024-09-27 09:54:38 +02:00
let models = [];
2024-09-26 12:31:56 +02:00
switch (radioSelection) {
2024-09-26 09:45:31 +02:00
case 'Offline':
models = [
...modelDropdown.onlineWithoutFoss,
...modelDropdown.onlineFoss,
]; // Show only offline models without FOSS
2024-09-27 09:54:38 +02:00
break;
case 'Offline (FOSS)':
models = [
...modelDropdown.onlineFoss,
]; // Show only offline models with FOSS
2024-09-27 09:54:38 +02:00
break;
case 'Online':
models = [
...modelDropdown.offlineWithoutFoss,
...modelDropdown.offlineFoss,
]; // Show only online models without FOSS
break;
case 'Online (FOSS)':
models = [
...modelDropdown.offlineFoss,
]; // Show only online models with FOSS
break;
case 'None':
models = [
...modelDropdown.offlineWithoutFoss,
...modelDropdown.offlineFoss,
...modelDropdown.onlineWithoutFoss,
...modelDropdown.onlineFoss,
]; // Show all models if nothing matches
break;
case 'None (FOSS)':
models = [
...modelDropdown.offlineFoss,
...modelDropdown.onlineFoss,
]; // Show all models if nothing matches
2024-09-27 09:54:38 +02:00
break;
2024-09-26 09:45:31 +02:00
default:
models = [
...modelDropdown.offlineWithoutFoss,
...modelDropdown.offlineFoss,
...modelDropdown.onlineWithoutFoss,
...modelDropdown.onlineFoss,
]; // Show all models if nothing matches
2024-09-27 09:54:38 +02:00
break;
2024-09-26 09:45:31 +02:00
}
2024-09-27 09:54:38 +02:00
return Array.from(new Set(models)); // Remove duplicates using Set
2024-09-26 09:45:31 +02:00
})();
2024-09-26 09:29:04 +02:00
const isOfflineModel = (model: string) =>
modelDropdown.offlineWithoutFoss.includes(model) || modelDropdown.offlineFoss.includes(model);
2024-09-26 12:31:56 +02:00
2024-09-30 10:43:22 +02:00
const modelClicked = (model: string) => {
localStorage.setItem('activeCategory' , model)
setActiveCategory(model)
2024-09-30 10:43:22 +02:00
const category = selectedModel as keyof typeof modelList;
2024-09-30 15:26:31 +02:00
localStorage.setItem("model", modelList[category][model as keyof typeof modelList[typeof category]])
localStorage.setItem("type", modelList[category]['model_type' as keyof typeof modelList[typeof category]])
2024-09-30 10:43:22 +02:00
}
2024-09-26 09:45:31 +02:00
return (
<div className="model-background">
2024-09-27 09:54:38 +02:00
<div className="models">
<div className="title">
<h3>Different AI Models</h3>
</div>
2024-09-26 09:29:04 +02:00
2024-09-27 09:54:38 +02:00
{/* Model Selection Dropdown */}
<div className="model-dropdown">
<label htmlFor="model-select">Select AI Model:</label>
<select id="model-select" value={selectedModel} onChange={handleModelChange}>
2024-09-27 09:54:38 +02:00
{filteredModels.map((model) => (
<option key={model} value={model}>
{model}
</option>
))}
</select>
</div>
2024-09-26 09:45:31 +02:00
2024-09-27 09:54:38 +02:00
{/* Model Grid with Cards */}
<div className="grid">
{Category.map(
(displayedCategory) => (
<button
key={displayedCategory}
className={`${displayedCategory.toLowerCase()}-model model-box ${currentCategory === displayedCategory ? 'selected' : ''}`}
onClick={() => modelClicked(displayedCategory)}
>
<div className="overlay">
<h3>{displayedCategory}</h3>
{isOfflineModel(selectedModel) && <img src="/img/nowifi.svg" alt="No Wi-Fi" />}
</div>
</button>
)
)}
2024-09-27 09:54:38 +02:00
</div>
2024-09-18 10:03:36 +02:00
</div>
2024-09-26 09:45:31 +02:00
</div>
);
};
2024-09-18 10:03:36 +02:00
2024-09-26 09:45:31 +02:00
export default Models;