import { ExternalLink } from 'lucide-react' import Modal from './Modal' import MarkdownRenderer from './MarkdownRenderer' import { useModel } from '@/contexts/ModelContext' import { useTextGeneration } from '@/contexts/TextGenerationContext' interface ModelCodeProps { isCodeModalOpen: boolean setIsCodeModalOpen: (isOpen: boolean) => void } const ModelCode = ({ isCodeModalOpen, setIsCodeModalOpen }: ModelCodeProps) => { const { modelInfo, pipeline, selectedQuantization } = useModel() if (!modelInfo) return null const title = (
{modelInfo.name}
) let classType = 'classifier' let exampleData = 'I love this product!' let config = {} switch (pipeline) { case 'text-classification': classType = 'classifier' exampleData = 'I love this product!' config = {} break case 'text-generation': classType = 'generator' exampleData = 'I love this product!' config = { max_length: 50, do_sample: true, temperature: 0.7, top_p: 0.9, top_k: 50 } break case 'zero-shot-classification': classType = 'classifier' exampleData = "I love this product!, ['positive', 'neutral', 'negative']" config = { threshold: 0.5 } break case 'feature-extraction': classType = 'generator' exampleData = 'I love this product!' config = { pooling: 'mean', normalize: true } break case 'image-classification': classType = 'classifier' exampleData = 'https://example.com/image.jpg' config = { top_k: 5 } break } const code = `import { pipeline } from '@huggingface/transformers'; const ${classType} = pipeline('${pipeline}', '${modelInfo.name}', { dtype: '${selectedQuantization}', device: 'webgpu' // 'wasm' }); const result = await ${classType}('${exampleData}', ${JSON.stringify(config, null, 2)}); console.log(result); ` console.log(modelInfo.widgetData) return ( <> setIsCodeModalOpen(false)} title={title} maxWidth="5xl" >
{/* More information link */}
Learn more about this model on Hugging Face
) } export default ModelCode