1212use Exception ;
1313use Symfony \Component \Console \Attribute \AsCommand ;
1414use Symfony \Component \Console \Command \Command ;
15+ use Symfony \Component \Console \Helper \ProgressBar ;
1516use Symfony \Component \Console \Input \InputArgument ;
1617use Symfony \Component \Console \Input \InputInterface ;
1718use Symfony \Component \Console \Input \InputOption ;
2627)]
2728class DownloadModelCommand extends Command
2829{
30+ protected array $ progressBars = [];
31+
2932 protected function configure (): void
3033 {
3134 $ this ->setHelp ('This command downloads a pre-trained model from Hugging Face. ' );
@@ -67,14 +70,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6770 try {
6871 $ task = $ task ? Task::tryFrom ($ task ) : null ;
6972
73+ $ onProgress = function ($ type , $ filename , $ downloadSize , $ downloaded ) use ($ output ) {
74+ if ($ type === 'advance_download ' ) {
75+ $ progressBar = $ this ->getProgressBar ($ filename , $ output );
76+ $ percent = round (($ downloaded / $ downloadSize ) * 100 , 2 );
77+ $ progressBar ->setProgress ((int )$ percent );
78+ }
79+ elseif ($ type === 'complete_download ' ) {
80+ $ progressBar = $ this ->getProgressBar ($ filename , $ output );
81+ $ progressBar ->finish ();
82+ $ output ->writeln ('' );
83+ }
84+ };
85+
7086 if ($ task != null ) {
71- pipeline ($ task , $ model , quantized: $ quantized , output : $ output );
87+ pipeline ($ task , $ model , quantized: $ quantized , onProgress : $ onProgress );
7288 } else {
73- AutoTokenizer::fromPretrained ($ model , output : $ output );
74- AutoModel::fromPretrained ($ model , $ quantized , output : $ output );
89+ AutoTokenizer::fromPretrained ($ model , onProgress : $ onProgress );
90+ AutoModel::fromPretrained ($ model , $ quantized , onProgress : $ onProgress );
7591 }
7692
77-
7893 $ output ->writeln ('✔ Model files downloaded successfully. ' );
7994
8095 $ this ->askToStar ($ input , $ output );
@@ -86,6 +101,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
86101 }
87102 }
88103
104+ protected function getProgressBar (string $ filename , OutputInterface $ output ): ProgressBar
105+ {
106+ ProgressBar::setFormatDefinition ('hub ' , '%filename% : [%bar%] %percent:3s%% ' );
107+
108+ if (!isset ($ this ->progressBars [$ filename ])) {
109+ $ progressBar = new ProgressBar ($ output , 100 );
110+ $ progressBar ->setFormat ('hub ' );
111+ $ progressBar ->setBarCharacter ('<fg=green>•</> ' );
112+ $ progressBar ->setEmptyBarCharacter ("<fg=red>⚬</> " );
113+ $ progressBar ->setProgressCharacter ('<fg=green>➤</> ' );
114+ $ progressBar ->setMessage ("✔ Downloading $ filename " , 'filename ' );
115+ $ this ->progressBars [$ filename ] = $ progressBar ;
116+ }
117+
118+ return $ this ->progressBars [$ filename ];
119+ }
120+
89121 protected function askToStar (InputInterface $ input , OutputInterface $ output ): void
90122 {
91123 if ($ input ->getOption ('no-interaction ' )) {
0 commit comments