Models.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { useState, memo } from 'react'
  2. import api, { ModelInfo } from '../api/client'
  3. const ModelRow = memo(function ModelRow({ m, onTest, onDelete }: {
  4. m: ModelInfo
  5. onTest: (id: string) => void
  6. onDelete: (id: string, name: string) => void
  7. }) {
  8. return (
  9. <tr style={{
  10. borderBottom: '1px solid #f0f0f0',
  11. transition: 'background 0.15s ease',
  12. }}
  13. onMouseEnter={e => { e.currentTarget.style.background = '#fafbfc' }}
  14. onMouseLeave={e => { e.currentTarget.style.background = 'transparent' }}
  15. >
  16. <td style={{ padding: '12px 12px', fontFamily: 'monospace', fontSize: 12, color: '#666' }}>{m.id}</td>
  17. <td style={{ padding: '12px 12px', fontWeight: 500, fontSize: 13 }}>{m.name}</td>
  18. <td style={{ padding: '12px 12px', fontSize: 13, color: '#666' }}>{m.model_type}</td>
  19. <td style={{ padding: '12px 12px' }}>
  20. <span style={{
  21. display: 'inline-block', padding: '3px 10px', borderRadius: 12, fontSize: 12, fontWeight: 500,
  22. background: m.is_downloaded ? '#f0fff4' : '#fff5f7',
  23. color: m.is_downloaded ? '#22863a' : '#e94560',
  24. border: `1px solid ${m.is_downloaded ? '#d1f0d8' : '#ffdce0'}`,
  25. }}>
  26. {m.is_downloaded ? '已缓存' : '未下载'}
  27. </span>
  28. </td>
  29. <td style={{ padding: '12px 12px', fontSize: 13, color: '#666' }}>{m.supported_peft_methods.join(', ') || '-'}</td>
  30. <td style={{ padding: '12px 12px' }}>
  31. {m.is_downloaded && (
  32. <button onClick={() => onTest(m.id)} style={{
  33. marginRight: 8, padding: '4px 12px', color: '#2196f3',
  34. border: '1px solid #2196f3', borderRadius: 6, background: 'transparent',
  35. cursor: 'pointer', fontSize: 12, fontWeight: 500, transition: 'all 0.15s ease',
  36. }}
  37. onMouseEnter={e => { e.currentTarget.style.background = '#2196f3'; e.currentTarget.style.color = '#fff' }}
  38. onMouseLeave={e => { e.currentTarget.style.background = 'transparent'; e.currentTarget.style.color = '#2196f3' }}
  39. >测试</button>
  40. )}
  41. <button onClick={() => onDelete(m.id, m.name)} style={{
  42. padding: '4px 12px', color: '#e94560', border: '1px solid #e94560',
  43. borderRadius: 6, background: 'transparent', cursor: 'pointer',
  44. fontSize: 12, fontWeight: 500, transition: 'all 0.15s ease',
  45. }}
  46. onMouseEnter={e => { e.currentTarget.style.background = '#e94560'; e.currentTarget.style.color = '#fff' }}
  47. onMouseLeave={e => { e.currentTarget.style.background = 'transparent'; e.currentTarget.style.color = '#e94560' }}
  48. >删除</button>
  49. </td>
  50. </tr>
  51. )
  52. })
  53. export function Models() {
  54. const [modelId, setModelId] = useState('')
  55. const [useModelscope, setUseModelscope] = useState(false)
  56. const [downloading, setDownloading] = useState(false)
  57. const [models, setModels] = useState<ModelInfo[]>([])
  58. const [loading, setLoading] = useState(false)
  59. const [statusMsg, setStatusMsg] = useState('')
  60. // Test state
  61. const [testModelId, setTestModelId] = useState('')
  62. const [testPrompt, setTestPrompt] = useState('')
  63. const [testResult, setTestResult] = useState('')
  64. const [testError, setTestError] = useState('')
  65. const [testing, setTesting] = useState(false)
  66. const fetchModels = () => {
  67. setLoading(true)
  68. api.models.list()
  69. .then(setModels)
  70. .catch(() => setModels([]))
  71. .finally(() => setLoading(false))
  72. }
  73. const handleDownload = () => {
  74. if (!modelId.trim()) return
  75. setDownloading(true)
  76. setStatusMsg('正在下载...')
  77. api.models.download(modelId, useModelscope)
  78. .then(res => setStatusMsg(`✅ ${res.model_id}: ${res.status}`))
  79. .catch(err => setStatusMsg(`❌ 下载失败: ${err.message}`))
  80. .finally(() => setDownloading(false))
  81. }
  82. const handleDelete = async (id: string, name: string) => {
  83. if (!confirm(`确定删除模型 "${name}"?这将删除本地所有相关文件。`)) return
  84. try {
  85. await api.models.delete(id)
  86. fetchModels()
  87. } catch (err) {
  88. const msg = err instanceof Error ? err.message : '删除失败'
  89. setStatusMsg(`❌ ${msg}`)
  90. }
  91. }
  92. const handleTest = async (id: string) => {
  93. setTestModelId(id)
  94. setTestPrompt('')
  95. setTestResult('')
  96. setTestError('')
  97. // Show test panel
  98. setTestPrompt('你好,请简单介绍一下自己。')
  99. }
  100. const handleTestSubmit = async () => {
  101. if (!testModelId.trim() || !testPrompt.trim()) return
  102. setTesting(true)
  103. setTestResult('')
  104. setTestError('')
  105. try {
  106. const res = await api.models.test({
  107. model_id: testModelId,
  108. prompt: testPrompt,
  109. max_new_tokens: 128,
  110. temperature: 0.8,
  111. top_p: 0.95,
  112. })
  113. setTestResult(res.generated_text)
  114. } catch (err) {
  115. const msg = err instanceof Error ? err.message : '测试失败'
  116. setTestError(msg)
  117. } finally {
  118. setTesting(false)
  119. }
  120. }
  121. return (
  122. <div>
  123. <h1 style={{ margin: 0, fontSize: 22, fontWeight: 700 }}>模型注册</h1>
  124. <p style={{ color: '#888', fontSize: 13, margin: '4px 0 16px' }}>下载和管理预训练模型</p>
  125. {/* Download form */}
  126. <div style={{
  127. marginTop: 16, background: '#fff', borderRadius: 10, padding: 20,
  128. boxShadow: '0 1px 3px rgba(0,0,0,0.06)', border: '1px solid rgba(0,0,0,0.04)',
  129. }}>
  130. <h2 style={{ margin: '0 0 12px', fontSize: 15, fontWeight: 600 }}>下载模型</h2>
  131. <div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
  132. <input
  133. type="text"
  134. placeholder="输入模型 ID (如 meta-llama/Llama-3.1-8B)"
  135. value={modelId}
  136. onChange={e => setModelId(e.target.value)}
  137. style={{
  138. padding: '10px 14px', flex: 1, maxWidth: 400, borderRadius: 8,
  139. border: '1px solid #d0d0d0', fontSize: 14, outline: 'none',
  140. transition: 'border-color 0.2s',
  141. }}
  142. onFocus={e => { e.currentTarget.style.borderColor = '#e94560' }}
  143. onBlur={e => { e.currentTarget.style.borderColor = '#d0d0d0' }}
  144. />
  145. <label style={{ fontSize: 13, color: '#666', whiteSpace: 'nowrap', display: 'flex', alignItems: 'center', gap: 4, cursor: 'pointer' }}>
  146. <input type="checkbox" checked={useModelscope} onChange={e => setUseModelscope(e.target.checked)} />
  147. {' '}ModelScope
  148. </label>
  149. <button
  150. onClick={handleDownload}
  151. disabled={downloading}
  152. style={{
  153. padding: '10px 20px', borderRadius: 8, border: 'none',
  154. background: '#e94560', color: '#fff', cursor: 'pointer',
  155. opacity: downloading ? 0.6 : 1, fontSize: 14, fontWeight: 600,
  156. transition: 'all 0.2s ease',
  157. }}
  158. >
  159. {downloading ? '下载中...' : '下载模型'}
  160. </button>
  161. </div>
  162. {statusMsg && (
  163. <p style={{
  164. marginTop: 10, padding: '8px 12px', borderRadius: 6, fontSize: 13, margin: '10px 0 0',
  165. background: statusMsg.includes('❌') ? '#fff2f0' : '#f0fff4',
  166. color: statusMsg.includes('❌') ? '#cf1322' : '#22863a',
  167. border: `1px solid ${statusMsg.includes('❌') ? '#ffccc7' : '#d1f0d8'}`,
  168. }}>
  169. {statusMsg}
  170. </p>
  171. )}
  172. </div>
  173. {/* Model list */}
  174. <div style={{ marginTop: 24 }}>
  175. <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 12 }}>
  176. <h2 style={{ margin: 0, fontSize: 15, fontWeight: 600 }}>已缓存模型</h2>
  177. <button onClick={fetchModels} style={{
  178. padding: '6px 14px', borderRadius: 6, border: '1px solid #d0d0d0',
  179. background: '#fff', cursor: 'pointer', fontSize: 13, fontWeight: 500,
  180. transition: 'all 0.15s ease',
  181. }}
  182. onMouseEnter={e => { e.currentTarget.style.background = '#f5f5f5' }}
  183. onMouseLeave={e => { e.currentTarget.style.background = '#fff' }}
  184. >
  185. 刷新
  186. </button>
  187. </div>
  188. {loading && <p style={{ color: '#999', fontSize: 13 }}>加载中...</p>}
  189. {!loading && models.length === 0 && (
  190. <div style={{
  191. padding: 40, textAlign: 'center', color: '#999', fontSize: 14,
  192. background: '#fff', borderRadius: 10, boxShadow: '0 1px 3px rgba(0,0,0,0.06)',
  193. }}>
  194. <div style={{ fontSize: 32, marginBottom: 8 }}>🧠</div>
  195. 暂无已缓存模型,请在上方下载模型
  196. </div>
  197. )}
  198. {!loading && models.length > 0 && (
  199. <div style={{
  200. background: '#fff', borderRadius: 10, overflow: 'hidden',
  201. boxShadow: '0 1px 3px rgba(0,0,0,0.06)', border: '1px solid rgba(0,0,0,0.04)',
  202. }}>
  203. <table style={{ width: '100%', borderCollapse: 'collapse', fontSize: 14 }}>
  204. <thead>
  205. <tr style={{ background: '#fafbfc', borderBottom: '2px solid #f0f0f0', textAlign: 'left' }}>
  206. <th style={{ padding: '10px 12px', fontSize: 12, color: '#666', fontWeight: 600 }}>ID</th>
  207. <th style={{ padding: '10px 12px', fontSize: 12, color: '#666', fontWeight: 600 }}>名称</th>
  208. <th style={{ padding: '10px 12px', fontSize: 12, color: '#666', fontWeight: 600 }}>类型</th>
  209. <th style={{ padding: '10px 12px', fontSize: 12, color: '#666', fontWeight: 600 }}>状态</th>
  210. <th style={{ padding: '10px 12px', fontSize: 12, color: '#666', fontWeight: 600 }}>PEFT 支持</th>
  211. <th style={{ padding: '10px 12px', fontSize: 12, color: '#666', fontWeight: 600 }}>操作</th>
  212. </tr>
  213. </thead>
  214. <tbody>
  215. {models.map(m => (
  216. <ModelRow key={m.id} m={m} onTest={handleTest} onDelete={handleDelete} />
  217. ))}
  218. </tbody>
  219. </table>
  220. </div>
  221. )}
  222. </div>
  223. {/* Test Panel */}
  224. {testModelId && (
  225. <div style={{
  226. marginTop: 24, background: '#fff', borderRadius: 12, padding: 24,
  227. boxShadow: '0 2px 8px rgba(0,0,0,0.08)', border: '1px solid rgba(0,0,0,0.04)',
  228. }}>
  229. <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 20 }}>
  230. <h2 style={{ margin: 0, fontSize: 16, fontWeight: 600 }}>模型测试 — <code style={{ background: '#f5f5f5', padding: '2px 8px', borderRadius: 4, fontSize: 13 }}>{testModelId}</code></h2>
  231. <button onClick={() => { setTestModelId(''); setTestResult(''); setTestError(''); setTestPrompt('') }} style={{
  232. padding: '6px 14px', borderRadius: 6, border: '1px solid #d0d0d0',
  233. background: '#fff', cursor: 'pointer', fontSize: 13,
  234. }}>关闭</button>
  235. </div>
  236. {/* Chat-like input */}
  237. <div style={{ display: 'flex', gap: 8 }}>
  238. <input
  239. value={testPrompt}
  240. onChange={e => setTestPrompt(e.target.value)}
  241. onKeyDown={e => { if (e.key === 'Enter') handleTestSubmit() }}
  242. placeholder="输入提示词,按 Enter 发送..."
  243. style={{
  244. flex: 1, padding: '10px 14px', borderRadius: 8,
  245. border: '1px solid #d0d0d0', fontSize: 14, outline: 'none',
  246. transition: 'border-color 0.2s',
  247. }}
  248. onFocus={e => { e.currentTarget.style.borderColor = '#2196f3' }}
  249. onBlur={e => { e.currentTarget.style.borderColor = '#d0d0d0' }}
  250. />
  251. <button
  252. onClick={handleTestSubmit}
  253. disabled={testing}
  254. style={{
  255. padding: '10px 24px', borderRadius: 8, border: 'none',
  256. background: '#2196f3', color: '#fff', cursor: 'pointer',
  257. opacity: testing ? 0.6 : 1, whiteSpace: 'nowrap', fontSize: 14, fontWeight: 600,
  258. transition: 'all 0.2s ease',
  259. }}
  260. >
  261. {testing ? '生成中...' : '发送'}
  262. </button>
  263. </div>
  264. {/* Error */}
  265. {testError && (
  266. <div style={{ marginTop: 16, padding: 12, background: '#fff2f0', borderRadius: 8, color: '#cf1322', fontSize: 13, border: '1px solid #ffccc7' }}>
  267. {testError}
  268. </div>
  269. )}
  270. {/* Result */}
  271. {testResult && (
  272. <div style={{ marginTop: 16 }}>
  273. <div style={{ display: 'flex', gap: 8, marginBottom: 8 }}>
  274. <span style={{ fontSize: 12, color: '#666', fontWeight: 600 }}>Prompt</span>
  275. </div>
  276. <div style={{ padding: 14, background: '#f0f7ff', borderRadius: 8, fontSize: 14, lineHeight: 1.6, marginBottom: 16, border: '1px solid #d6e8fa' }}>
  277. {testPrompt}
  278. </div>
  279. <div style={{ display: 'flex', gap: 8, marginBottom: 8 }}>
  280. <span style={{ fontSize: 12, color: '#666', fontWeight: 600 }}>Response</span>
  281. </div>
  282. <div style={{ padding: 14, background: '#f0fff4', borderRadius: 8, fontSize: 14, lineHeight: 1.6, whiteSpace: 'pre-wrap', wordBreak: 'break-word', border: '1px solid #d1f0d8' }}>
  283. {testResult}
  284. </div>
  285. </div>
  286. )}
  287. </div>
  288. )}
  289. </div>
  290. )
  291. }