// HuggingFaceTab.js 组件 import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { Typography, Box, TextField, Button, FormControlLabel, Checkbox, Alert, CircularProgress, Divider, Paper, Grid, Tooltip, IconButton, Link } from '@mui/material'; import InfoIcon from '@mui/icons-material/Info'; import HelpOutlineIcon from '@mui/icons-material/HelpOutline'; import CheckCircleOutlineIcon from '@mui/icons-material/CheckCircleOutline'; const HuggingFaceTab = ({ projectId, systemPrompt, reasoningLanguage, confirmedOnly, includeCOT, formatType, fileFormat, customFields, handleSystemPromptChange, handleReasoningLanguageChange, handleConfirmedOnlyChange, handleIncludeCOTChange }) => { const { t } = useTranslation(); const [token, setToken] = useState(''); const [datasetName, setDatasetName] = useState(''); const [isPrivate, setIsPrivate] = useState(false); const [uploading, setUploading] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(false); const [datasetUrl, setDatasetUrl] = useState(''); const [hasToken, setHasToken] = useState(false); const [loading, setLoading] = useState(true); // 从配置中获取 huggingfaceToken useEffect(() => { if (projectId) { setLoading(true); fetch(`/api/projects/${projectId}/config`) .then(res => res.json()) .then(data => { if (data.huggingfaceToken) { setToken(data.huggingfaceToken); setHasToken(true); } setLoading(false); }) .catch(err => { console.error('获取 HuggingFace Token 失败:', err); setLoading(false); }); } }, [projectId]); // 处理上传数据集到 HuggingFace const handleUpload = async () => { if (!hasToken) { return; } if (!datasetName) { setError('请输入数据集名称'); return; } try { setUploading(true); setError(''); setSuccess(false); const response = await fetch(`/api/projects/${projectId}/huggingface/upload`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token, datasetName, isPrivate, formatType, systemPrompt, reasoningLanguage, confirmedOnly, includeCOT, fileFormat, customFields: formatType === 'custom' ? customFields : undefined }) }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || '上传失败'); } setSuccess(true); setDatasetUrl(data.url); } catch (err) { setError(err.message); } finally { setUploading(false); } }; return ( {error && ( {error} )} {success && ( }> {t('export.uploadSuccess')} {datasetUrl && ( {t('export.viewOnHuggingFace')} )} )} {!hasToken ? ( {t('export.noTokenWarning')} ) : null} {t('export.datasetSettings')} setDatasetName(e.target.value)} helperText={t('export.datasetNameHelp')} sx={{ mb: 2 }} /> setIsPrivate(e.target.checked)} />} label={t('export.privateDataset')} /> {t('export.exportOptions')} {t('export.systemPrompt')} {/* Reasoning language – only for multilingual‑thinking */} {formatType === 'multilingualthinking' && ( {t('export.reasoningLanguage')} )} } label={t('export.onlyConfirmed')} /> } label={t('export.includeCOT')} /> ); }; export default HuggingFaceTab;