CodeWithKyrian / transformers-php

Transformers PHP is a toolkit for PHP developers to add machine learning magic to their projects easily.
https://codewithkyrian.github.io/transformers-php/
Apache License 2.0
530 stars 28 forks source link

Problem with CURL Request in Downloader.php #40

Closed BlackyDrum closed 4 months ago

BlackyDrum commented 4 months ago

System Info

Windows 10, PHP built-in Webserver, Laravel 11

PHP Version

8.3.1

Environment/Platform

Description

If the config.json file for a model does not yet exist, I encounter the following error:

Codewithkyrian\Transformers\Utils\AutoConfig::__construct(): Argument #1 ($config) must be of type array, null given, called in C:\Users\Gani\Desktop\transformers\vendor\codewithkyrian\transformers\src\Utils\AutoConfig.php on line 53

However, if the config.json file for a model does exist, I get this error:

Error 0 occurred while trying to load file from https://huggingface.co\Xenova/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/onnx\model_quantized.onnx

The issue seems to stem from this line in the code for me:

// Downloader.php
curl_setopt($curlHandle, CURLOPT_URL, $url);

When I dump the value of $url, it appears correct:

https://huggingface.co\Xenova/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json

However, when I dump the URL using curl_getinfo($curlHandle), the URL value is:

https://huggingface.co\\Xenova/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json

It seems the backslash character is being escaped for me. The line $error = curl_error($curlHandle); also indicates a hostname error:

Log::info($error); -> URL rejected: Bad hostname

For me it worked when I changed this:

// Downloader.php
curl_setopt($curlHandle, CURLOPT_URL, $url);

to this:

curl_setopt($curlHandle, CURLOPT_URL, str_replace('\\', '/', $url));

This fixed both errors for me. However, this issue with the backslash being escaped might be specific to my configuration (although I don't know how), and I'm not certain if it occurs for others aswell.

Reproduction

  1. $classifier = pipeline('sentiment-analysis');
  2. $result = $classifier('I love everything!');
CodeWithKyrian commented 4 months ago

Fixed in https://github.com/CodeWithKyrian/transformers-php/releases/tag/0.4.2