Spaces:
No application file
No application file
File size: 13,490 Bytes
5bb2330 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
import React, { useState, useEffect } from 'react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Progress } from '@/components/ui/progress';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { ArrowLeft, Play, Pause, Download, RefreshCw } from 'lucide-react';
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts';
import '../App.css';
const ProjectView = ({ projectId, onBack }) => {
const [project, setProject] = useState(null);
const [logs, setLogs] = useState([]);
const [loading, setLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false);
useEffect(() => {
fetchProject();
fetchLogs();
// Auto-refresh if project is running
const interval = setInterval(() => {
if (project?.status === 'running') {
fetchProject();
fetchLogs();
}
}, 5000);
return () => clearInterval(interval);
}, [projectId, project?.status]);
const fetchProject = async () => {
try {
const response = await fetch(`/api/projects/${projectId}`);
const data = await response.json();
setProject(data);
} catch (error) {
console.error('Erro ao buscar projeto:', error);
} finally {
setLoading(false);
}
};
const fetchLogs = async () => {
try {
const response = await fetch(`/api/projects/${projectId}/logs`);
const data = await response.json();
setLogs(data.logs || []);
} catch (error) {
console.error('Erro ao buscar logs:', error);
}
};
const handleRefresh = async () => {
setRefreshing(true);
await fetchProject();
await fetchLogs();
setRefreshing(false);
};
const handleStartTraining = async () => {
try {
await fetch(`/api/projects/${projectId}/start-training`, {
method: 'POST',
});
fetchProject();
} catch (error) {
console.error('Erro ao iniciar treinamento:', error);
}
};
const handleStopTraining = async () => {
try {
await fetch(`/api/projects/${projectId}/stop-training`, {
method: 'POST',
});
fetchProject();
} catch (error) {
console.error('Erro ao parar treinamento:', error);
}
};
const getStatusColor = (status) => {
switch (status) {
case 'completed':
return 'bg-green-500';
case 'running':
return 'bg-blue-500';
case 'failed':
return 'bg-red-500';
case 'pending':
return 'bg-yellow-500';
default:
return 'bg-gray-500';
}
};
const getStatusText = (status) => {
switch (status) {
case 'completed':
return 'Concluído';
case 'running':
return 'Treinando';
case 'failed':
return 'Falhou';
case 'pending':
return 'Pendente';
case 'cancelled':
return 'Cancelado';
default:
return 'Desconhecido';
}
};
// Generate mock training data for chart
const generateTrainingData = () => {
if (!project || !project.current_epoch) return [];
const data = [];
for (let i = 1; i <= project.current_epoch; i++) {
data.push({
epoch: i,
loss: Math.max(0.1, 1.0 - (i / project.num_epochs) * 0.8 + (Math.random() - 0.5) * 0.1)
});
}
return data;
};
if (loading) {
return (
<div className="flex items-center justify-center h-64">
<div className="text-lg">Carregando projeto...</div>
</div>
);
}
if (!project) {
return (
<div className="flex items-center justify-center h-64">
<div className="text-lg text-red-600">Projeto não encontrado</div>
</div>
);
}
return (
<div className="space-y-6">
{/* Header */}
<div className="flex items-center justify-between">
<div className="flex items-center gap-4">
<Button variant="outline" onClick={onBack} className="flex items-center gap-2">
<ArrowLeft className="w-4 h-4" />
Voltar
</Button>
<div>
<h1 className="text-3xl font-bold">{project.name}</h1>
<p className="text-gray-600 mt-2">
{project.description || 'Sem descrição'}
</p>
</div>
</div>
<div className="flex items-center gap-2">
<Button
variant="outline"
onClick={handleRefresh}
disabled={refreshing}
className="flex items-center gap-2"
>
<RefreshCw className={`w-4 h-4 ${refreshing ? 'animate-spin' : ''}`} />
Atualizar
</Button>
{project.status === 'pending' || project.status === 'failed' ? (
<Button onClick={handleStartTraining} className="flex items-center gap-2">
<Play className="w-4 h-4" />
Iniciar Treinamento
</Button>
) : project.status === 'running' ? (
<Button
variant="destructive"
onClick={handleStopTraining}
className="flex items-center gap-2"
>
<Pause className="w-4 h-4" />
Parar Treinamento
</Button>
) : project.status === 'completed' ? (
<Button variant="outline" className="flex items-center gap-2">
<Download className="w-4 h-4" />
Download Modelo
</Button>
) : null}
</div>
</div>
{/* Status Card */}
<Card>
<CardHeader>
<div className="flex justify-between items-center">
<CardTitle>Status do Projeto</CardTitle>
<Badge className={getStatusColor(project.status)}>
{getStatusText(project.status)}
</Badge>
</div>
</CardHeader>
<CardContent>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<div className="text-sm text-gray-600">Modelo Base</div>
<div className="font-medium">{project.base_model}</div>
</div>
<div>
<div className="text-sm text-gray-600">Progresso</div>
<div className="font-medium">
Época {project.current_epoch || 0} de {project.num_epochs}
</div>
</div>
<div>
<div className="text-sm text-gray-600">Criado em</div>
<div className="font-medium">
{new Date(project.created_at).toLocaleDateString('pt-BR')}
</div>
</div>
</div>
{project.status === 'running' && (
<div className="mt-4 space-y-2">
<div className="flex justify-between text-sm">
<span>Progresso do Treinamento</span>
<span>{Math.round((project.progress || 0) * 100)}%</span>
</div>
<Progress value={(project.progress || 0) * 100} />
{project.current_loss && (
<div className="text-sm text-gray-600">
Loss atual: {project.current_loss.toFixed(4)}
</div>
)}
</div>
)}
{project.error_message && (
<div className="mt-4 p-3 bg-red-50 border border-red-200 rounded-md">
<div className="text-sm text-red-800">
<strong>Erro:</strong> {project.error_message}
</div>
</div>
)}
</CardContent>
</Card>
{/* Tabs */}
<Tabs defaultValue="config" className="space-y-4">
<TabsList>
<TabsTrigger value="config">Configuração</TabsTrigger>
<TabsTrigger value="training">Treinamento</TabsTrigger>
<TabsTrigger value="logs">Logs</TabsTrigger>
</TabsList>
<TabsContent value="config" className="space-y-4">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<Card>
<CardHeader>
<CardTitle>Parâmetros LoRA</CardTitle>
</CardHeader>
<CardContent className="space-y-2">
<div className="flex justify-between">
<span className="text-gray-600">Rank:</span>
<span>{project.rank}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600">Alpha:</span>
<span>{project.alpha}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600">Dropout:</span>
<span>{project.dropout}</span>
</div>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Parâmetros de Treinamento</CardTitle>
</CardHeader>
<CardContent className="space-y-2">
<div className="flex justify-between">
<span className="text-gray-600">Taxa de Aprendizado:</span>
<span>{project.learning_rate}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600">Batch Size:</span>
<span>{project.batch_size}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600">Épocas:</span>
<span>{project.num_epochs}</span>
</div>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Otimizações</CardTitle>
</CardHeader>
<CardContent className="space-y-2">
<div className="flex justify-between">
<span className="text-gray-600">Otimizador 8-bit:</span>
<span>{project.use_8bit_optimizer ? 'Sim' : 'Não'}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600">Gradient Checkpointing:</span>
<span>{project.use_gradient_checkpointing ? 'Sim' : 'Não'}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600">Precisão Mista:</span>
<span>{project.mixed_precision}</span>
</div>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Dataset</CardTitle>
</CardHeader>
<CardContent className="space-y-2">
<div className="flex justify-between">
<span className="text-gray-600">Caminho:</span>
<span className="text-sm truncate">
{project.dataset_path || 'Não carregado'}
</span>
</div>
<div className="flex justify-between">
<span className="text-gray-600">Número de Imagens:</span>
<span>{project.num_images || 'N/A'}</span>
</div>
</CardContent>
</Card>
</div>
</TabsContent>
<TabsContent value="training" className="space-y-4">
<Card>
<CardHeader>
<CardTitle>Gráfico de Loss</CardTitle>
<CardDescription>
Acompanhe a evolução da loss durante o treinamento
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-64">
<ResponsiveContainer width="100%" height="100%">
<LineChart data={generateTrainingData()}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="epoch" />
<YAxis />
<Tooltip />
<Line
type="monotone"
dataKey="loss"
stroke="#3b82f6"
strokeWidth={2}
dot={{ fill: '#3b82f6' }}
/>
</LineChart>
</ResponsiveContainer>
</div>
</CardContent>
</Card>
</TabsContent>
<TabsContent value="logs" className="space-y-4">
<Card>
<CardHeader>
<CardTitle>Logs de Treinamento</CardTitle>
<CardDescription>
Logs em tempo real do processo de treinamento
</CardDescription>
</CardHeader>
<CardContent>
<div className="bg-gray-900 text-green-400 p-4 rounded-md font-mono text-sm h-64 overflow-y-auto">
{logs.length === 0 ? (
<div className="text-gray-500">Nenhum log disponível</div>
) : (
logs.map((log, index) => (
<div key={index} className="mb-1">
{log}
</div>
))
)}
</div>
</CardContent>
</Card>
</TabsContent>
</Tabs>
</div>
);
};
export default ProjectView;
|