├── .devcontainer ├── Dockerfile └── devcontainer.json ├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── composer.lock └── src └── NLPCloud.php /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.158.0/containers/php/.devcontainer/base.Dockerfile 2 | 3 | # [Choice] PHP version: 8, 8.0, 7, 7.4, 7.3 4 | ARG VARIANT="8" 5 | FROM mcr.microsoft.com/vscode/devcontainers/php:0-${VARIANT} 6 | 7 | # [Option] Install Node.js 8 | ARG INSTALL_NODE="true" 9 | ARG NODE_VERSION="lts/*" 10 | RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi 11 | 12 | # [Optional] Uncomment this section to install additional OS packages. 13 | # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ 14 | # && apt-get -y install --no-install-recommends 15 | 16 | # [Optional] Uncomment this line to install global node packages. 17 | # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.158.0/containers/php 3 | { 4 | "name": "PHP", 5 | "build": { 6 | "dockerfile": "Dockerfile", 7 | "args": { 8 | // Update VARIANT to pick a PHP version: 8, 8.0, 7, 7.4, 7.3 9 | "VARIANT": "8", 10 | "INSTALL_NODE": "true", 11 | "NODE_VERSION": "lts/*" 12 | } 13 | }, 14 | 15 | // Set *default* container specific settings.json values on container create. 16 | "settings": { 17 | "terminal.integrated.shell.linux": "/bin/bash", 18 | "php.validate.executablePath": "/usr/local/bin/php" 19 | }, 20 | 21 | // Add the IDs of extensions you want installed when the container is created. 22 | "extensions": [ 23 | "felixfbecker.php-debug", 24 | "bmewburn.vscode-intelephense-client", 25 | "mrmlnc.vscode-apache" 26 | ], 27 | 28 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 29 | "forwardPorts": [8080], 30 | 31 | // Use 'postCreateCommand' to run commands after the container is created. 32 | // "postCreateCommand": "sudo chmod a+x \"$(pwd)\" && sudo rm -rf /var/www/html && sudo ln -s \"$(pwd)\" /var/www/html" 33 | 34 | // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. 35 | "remoteUser": "vscode" 36 | } 37 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 NLP Cloud 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP Client For NLP Cloud 2 | 3 | This is the PHP client for the [NLP Cloud](https://nlpcloud.com) API. See the [documentation](https://docs.nlpcloud.com) for more details. 4 | 5 | NLP Cloud serves high performance pre-trained or custom models for NER, sentiment-analysis, classification, summarization, dialogue summarization, paraphrasing, intent classification, product description and ad generation, chatbot, grammar and spelling correction, keywords and keyphrases extraction, text generation, image generation, code generation, question answering, automatic speech recognition, machine translation, language detection, semantic search, semantic similarity, tokenization, POS tagging, embeddings, and dependency parsing. It is ready for production, served through a REST API. 6 | 7 | You can either use the NLP Cloud pre-trained models, fine-tune your own models, or deploy your own models. 8 | 9 | If you face an issue, don't hesitate to raise it as a Github issue. Thanks! 10 | 11 | ## Installation 12 | 13 | Install via composer. 14 | 15 | Create a `composer.json` file containing at least the following: 16 | 17 | ```json 18 | { 19 | "require": { 20 | "nlpcloud/nlpcloud-client": "*" 21 | } 22 | } 23 | ``` 24 | 25 | Then launch the following: 26 | 27 | ```shell 28 | composer install 29 | ``` 30 | 31 | ## Examples 32 | 33 | Here is a full example that summarizes a text using Facebook's Bart Large CNN model, with a fake token: 34 | 35 | ```php 36 | require 'vendor/autoload.php'; 37 | 38 | use NLPCloud\NLPCloud; 39 | 40 | $client = new NLPCloud('bart-large-cnn', ''); 41 | echo json_encode($client->summarization('One month after the United States began what has become a 42 | troubled rollout of a national COVID vaccination campaign, the effort is finally 43 | gathering real steam. Close to a million doses -- over 951,000, to be more exact -- 44 | made their way into the arms of Americans in the past 24 hours, the U.S. Centers 45 | for Disease Control and Prevention reported Wednesday. That s the largest number 46 | of shots given in one day since the rollout began and a big jump from the 47 | previous day, when just under 340,000 doses were given, CBS News reported. 48 | That number is likely to jump quickly after the federal government on Tuesday 49 | gave states the OK to vaccinate anyone over 65 and said it would release all 50 | the doses of vaccine it has available for distribution. Meanwhile, a number 51 | of states have now opened mass vaccination sites in an effort to get larger 52 | numbers of people inoculated, CBS News reported.')); 53 | ``` 54 | 55 | Here is a full example that does the same thing, but on a GPU: 56 | 57 | ```php 58 | require 'vendor/autoload.php'; 59 | 60 | use NLPCloud\NLPCloud; 61 | 62 | $client = new NLPCloud('bart-large-cnn', '', True); 63 | echo json_encode($client->summarization('One month after the United States began what has become a 64 | troubled rollout of a national COVID vaccination campaign, the effort is finally 65 | gathering real steam. Close to a million doses -- over 951,000, to be more exact -- 66 | made their way into the arms of Americans in the past 24 hours, the U.S. Centers 67 | for Disease Control and Prevention reported Wednesday. That s the largest number 68 | of shots given in one day since the rollout began and a big jump from the 69 | previous day, when just under 340,000 doses were given, CBS News reported. 70 | That number is likely to jump quickly after the federal government on Tuesday 71 | gave states the OK to vaccinate anyone over 65 and said it would release all 72 | the doses of vaccine it has available for distribution. Meanwhile, a number 73 | of states have now opened mass vaccination sites in an effort to get larger 74 | numbers of people inoculated, CBS News reported.')); 75 | ``` 76 | 77 | Here is a full example that does the same thing, but on a French text: 78 | 79 | ```php 80 | require 'vendor/autoload.php'; 81 | 82 | use NLPCloud\NLPCloud; 83 | 84 | $client = new NLPCloud('bart-large-cnn', '', True, 'fra_Latn'); 85 | echo json_encode($client->summarization('Sur des images aériennes, prises la veille par un vol de surveillance 86 | de la Nouvelle-Zélande, la côte d’une île est bordée d’arbres passés du vert 87 | au gris sous l’effet des retombées volcaniques. On y voit aussi des immeubles 88 | endommagés côtoyer des bâtiments intacts. « D’après le peu d’informations 89 | dont nous disposons, l’échelle de la dévastation pourrait être immense, 90 | spécialement pour les îles les plus isolées », avait déclaré plus tôt 91 | Katie Greenwood, de la Fédération internationale des sociétés de la Croix-Rouge. 92 | Selon l’Organisation mondiale de la santé (OMS), une centaine de maisons ont 93 | été endommagées, dont cinquante ont été détruites sur l’île principale de 94 | Tonga, Tongatapu. La police locale, citée par les autorités néo-zélandaises, 95 | a également fait état de deux morts, dont une Britannique âgée de 50 ans, 96 | Angela Glover, emportée par le tsunami après avoir essayé de sauver les chiens 97 | de son refuge, selon sa famille.')); 98 | ``` 99 | 100 | A json object is returned: 101 | 102 | ```json 103 | { 104 | "summary_text": "Over 951,000 doses were given in the past 24 hours. That's the largest number of shots given in one day since the rollout began. That number is likely to jump quickly after the federal government gave states the OK to vaccinate anyone over 65. A number of states have now opened mass vaccination sites." 105 | } 106 | ``` 107 | 108 | ## Usage 109 | 110 | ### Client Initialization 111 | 112 | Pass the model you want to use and the NLP Cloud token to the client during initialization. 113 | 114 | The model can either be a pretrained model like `en_core_web_lg`, `bart-large-mnli`... but also one of your custom models, using `custom_model/` (e.g. `custom_model/2568`). 115 | 116 | Your token can be retrieved from your [NLP Cloud dashboard](https://nlpcloud.com/home/token). 117 | 118 | ```php 119 | use NLPCloud\NLPCloud; 120 | 121 | $client = new NLPCloud('',''); 122 | ``` 123 | 124 | If you want to use a GPU, pass `true` as a 3rd argument. 125 | 126 | ```php 127 | use NLPCloud\NLPCloud; 128 | 129 | $client = new NLPCloud('','', true); 130 | ``` 131 | 132 | If you want to use the multilingual add-on in order to process non-English texts, set `''` as a 4th argument. For example, if you want to process French text, you should set `'fra_Latn'`. 133 | 134 | ```php 135 | use NLPCloud\NLPCloud; 136 | 137 | $client = new NLPCloud('','', false, ''); 138 | ``` 139 | 140 | If you want to make asynchronous requests, pass `true` as a 4th argument. 141 | 142 | ```php 143 | use NLPCloud\NLPCloud; 144 | 145 | $client = new NLPCloud('', '', false, '', true); 146 | ``` 147 | 148 | If you are making asynchronous requests, you will always receive a quick response containing a URL. You should then poll this URL with `asyncResult()` on a regular basis (every 10 seconds for example) in order to check if the result is available. Here is an example: 149 | 150 | ```php 151 | $client->asyncResult('https://api.nlpcloud.io/v1/get-async-result/21718218-42e8-4be9-a67f-b7e18e03b436'); 152 | ``` 153 | 154 | The above command returns an object if the response is availble. It returns nothing otherwise (`NULL`). 155 | 156 | ### Automatic Speech Recognition (Speech to Text) Endpoint 157 | 158 | Call the `asr()` method and pass the following arguments: 159 | 160 | 1. (Optional: either this or the encoded file should be set) `url`: a URL where your audio or video file is hosted 161 | 1. (Optional: either this or the url should be set) `encodedFile`: a base 64 encoded version of your file 162 | 1. (Optional) `inputLanguage`: the language of your file as ISO code 163 | 164 | ```php 165 | echo json_encode($client->asr('')); 166 | ``` 167 | 168 | The above command returns an object. 169 | 170 | ### Chatbot Endpoint 171 | 172 | Call the `chatbot()` method and pass your input. As an option, you can also pass a context and a conversation history that is an array of named arrays. Each named array is made of an `input` and a `response` from the chatbot. 173 | 174 | ```php 175 | echo json_encode($client->chatbot('', '', array(array('input'=>'input 1','response'=>'response 1'), array('input'=>'input 2','response'=>'response 2'), ...))); 176 | ``` 177 | 178 | The above command returns an object. 179 | 180 | ### Classification Endpoint 181 | 182 | Call the `classification()` method and pass 3 arguments: 183 | 184 | 1. The text you want to classify, as a string 185 | 1. The candidate labels for your text, as an array of strings 186 | 1. Whether the classification should be multi-class or not, as a boolean 187 | 188 | ```php 189 | echo json_encode($client->classification('', array('label 1', 'label 2', ...), True|False)); 190 | ``` 191 | 192 | The above command returns an object. 193 | 194 | ### Code Generation Endpoint 195 | 196 | Call the `codeGeneration()` method and pass the description of your program: 197 | 198 | ```php 199 | echo json_encode($client->codeGeneration('')); 200 | ``` 201 | 202 | The above command returns an object. 203 | 204 | ### Dependencies Endpoint 205 | 206 | Call the `dependencies()` method and pass the text you want to perform part of speech tagging (POS) + arcs on. 207 | 208 | ```php 209 | echo json_encode($client->dependencies('')); 210 | ``` 211 | 212 | The above command returns an object. 213 | 214 | ### Embeddings Endpoint 215 | 216 | Call the `embeddings()` method and pass an array of blocks of text that you want to extract embeddings from. 217 | 218 | ```php 219 | echo json_encode($client->embeddings(array('', '', '', ...))); 220 | ``` 221 | 222 | The above command returns an object. 223 | 224 | ### Entities Endpoint 225 | 226 | Call the `entities()` method and pass the text you want to perform named entity recognition (NER) on. 227 | 228 | ```php 229 | echo json_encode($client->entities('')); 230 | ``` 231 | 232 | The above command returns an object. 233 | 234 | ### Generation Endpoint 235 | 236 | Call the `generation()` method and pass the following arguments: 237 | 238 | 1. The block of text that starts the generated text. 256 tokens maximum for GPT-J on CPU, 1024 tokens maximum for GPT-J and GPT-NeoX 20B on GPU, and 2048 tokens maximum for Fast GPT-J and Finetuned GPT-NeoX 20B on GPU. 239 | 1. (Optional) `max_length`: Optional. The maximum number of tokens that the generated text should contain. 256 tokens maximum for GPT-J on CPU, 1024 tokens maximum for GPT-J and GPT-NeoX 20B on GPU, and 2048 tokens maximum for Fast GPT-J and Finetuned GPT-NeoX 20B on GPU. If `length_no_input` is false, the size of the generated text is the difference between `max_length` and the length of your input text. If `length_no_input` is true, the size of the generated text simply is `max_length`. Defaults to 50. 240 | 1. (Optional) `length_no_input`: Whether `min_length` and `max_length` should not include the length of the input text, as a boolean. If false, `min_length` and `max_length` include the length of the input text. If true, min_length and `max_length` don't include the length of the input text. Defaults to false. 241 | 1. (Optional) `end_sequence`: A specific token that should be the end of the generated sequence, as a string. For example if could be `.` or `\n` or `###` or anything else below 10 characters. 242 | 1. (Optional) `remove_input`: Whether you want to remove the input text form the result, as a boolean. Defaults to false. 243 | 1. (Optional) `num_beams`: Number of beams for beam search. 1 means no beam search. This is an integer. Defaults to 1. 244 | 1. (Optional) `num_return_sequences`: The number of independently computed returned sequences for each element in the batch, as an integer. Defaults to 1. 245 | 1. (Optional) `top_k`: The number of highest probability vocabulary tokens to keep for top-k-filtering, as an integer. Maximum 1000 tokens. Defaults to 0. 246 | 1. (Optional) `top_p`: If set to float < 1, only the most probable tokens with probabilities that add up to top_p or higher are kept for generation. This is a float. Should be between 0 and 1. Defaults to 0.7. 247 | 1. (Optional) `temperature`: The value used to module the next token probabilities, as a float. Should be between 0 and 1. Defaults to 1. 248 | 1. (Optional) `repetition_penalty`: The parameter for repetition penalty, as a float. 1.0 means no penalty. Defaults to 1.0. 249 | 1. (Optional) `bad_words`: List of tokens that are not allowed to be generated, as a list of strings. Defaults to null. 250 | 1. (Optional) `remove_end_sequence`: Optional. Whether you want to remove the `end_sequence` string from the result. Defaults to false. 251 | 252 | ```php 253 | echo json_encode($client->generation('')); 254 | ``` 255 | 256 | ### Grammar and Spelling Correction Endpoint 257 | 258 | Call the `gsCorrection()` method and pass the text you want correct: 259 | 260 | ```php 261 | echo json_encode($client->gsCorrection('')); 262 | ``` 263 | 264 | The above command returns an object. 265 | 266 | ### Image Generation Endpoint 267 | 268 | Call the `imageGeneration()` method and pass the text you want to use to generate your image: 269 | 270 | ```php 271 | echo json_encode($client->imageGeneration('')); 272 | ``` 273 | 274 | The above command returns an object. 275 | 276 | ### Intent Classification Endpoint 277 | 278 | Call the `intentClassification()` method and pass the text you want to extract intents from: 279 | 280 | ```php 281 | echo json_encode($client->intentClassification('')); 282 | ``` 283 | 284 | The above command returns an object. 285 | 286 | ### Keywords and Keyphrases Extraction Endpoint 287 | 288 | Call the `kwKpExtraction()` method and pass the text you want to extract keywords and keyphrases from: 289 | 290 | ```php 291 | echo json_encode($client->kwKpExtraction('')); 292 | ``` 293 | 294 | The above command returns an object. 295 | 296 | ### Language Detection Endpoint 297 | 298 | Call the `langdetection()` method and pass the text you want to analyze. 299 | 300 | ```php 301 | echo json_encode($client->langdetection('')); 302 | ``` 303 | 304 | The above command returns an object. 305 | 306 | ### Paraphrasing Endpoint 307 | 308 | Call the `paraphrasing()` method and pass the text you want to paraphrase. 309 | 310 | ```php 311 | echo json_encode($client->paraphrasing('')); 312 | ``` 313 | 314 | The above command returns an object. 315 | 316 | ### Question Answering Endpoint 317 | 318 | Call the `question()` method and pass the following: 319 | 320 | 1. Your question 321 | 1. (Optional) A context that the model will use to try to answer your question 322 | 323 | ```php 324 | echo json_encode($client->question('','')); 325 | ``` 326 | 327 | The above command returns an object. 328 | 329 | ### Semantic Search Endpoint 330 | 331 | Call the `semanticSearch()` method and pass your search query: 332 | 333 | ```php 334 | echo json_encode($client->semanticSearch('')); 335 | ``` 336 | 337 | The above command returns an object. 338 | 339 | ### Semantic Similarity Endpoint 340 | 341 | Call the `semanticSimilarity()` method and pass an array made up of 2 blocks of text that you want to compare. 342 | 343 | ```php 344 | echo json_encode($client->semanticSimilarity(array('', ''))); 345 | ``` 346 | 347 | The above command returns an object. 348 | 349 | ### Sentence Dependencies Endpoint 350 | 351 | Call the `sentenceDependencies()` method and pass a block of text made up of several sentencies you want to perform POS + arcs on. 352 | 353 | ```php 354 | echo json_encode($client->sentenceDependencies('')); 355 | ``` 356 | 357 | The above command returns an object. 358 | 359 | ### Sentiment Analysis Endpoint 360 | 361 | Call the `sentiment()` method and pass the following: 362 | 363 | 1. The text you want to analyze and get the sentiment of 364 | 1. (Optional) The target element that the sentiment should apply to 365 | 366 | 367 | ```php 368 | echo json_encode($client->sentiment('', '')); 369 | ``` 370 | 371 | The above command returns an object. 372 | 373 | ### Speech Synthesis Endpoint 374 | 375 | Call the `speechSynthesis()` method and pass the text you want to convert to audio: 376 | 377 | ```php 378 | echo json_encode($client->speechSynthesis('')); 379 | ``` 380 | 381 | The above command returns a JSON object. 382 | 383 | ### Summarization Endpoint 384 | 385 | Call the `summarization()` method and pass the text you want to summarize. 386 | 387 | ```php 388 | echo json_encode($client->summarization('')); 389 | ``` 390 | 391 | The above command returns an object. 392 | 393 | ### Tokenization Endpoint 394 | 395 | Call the `tokens()` method and pass the text you want to tokenize. 396 | 397 | ```php 398 | echo json_encode($client->tokens('')); 399 | ``` 400 | 401 | The above command returns an object. 402 | 403 | ### Translation Endpoint 404 | 405 | Call the `translation()` method and pass the text you want to translate. 406 | 407 | ```php 408 | echo json_encode($client->translation('')); 409 | ``` 410 | 411 | The above command returns an object. 412 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nlpcloud/nlpcloud-client", 3 | "description": "NLP Cloud serves high performance pre-trained or custom models for NER, sentiment-analysis, classification, summarization, paraphrasing, grammar and spelling correction, keywords and keyphrases extraction, chatbot, product description and ad generation, intent classification, text generation, image generation, code generation, question answering, automatic speech recognition, machine translation, language detection, semantic search, semantic similarity, tokenization, POS tagging, speech synthesis, embeddings, and dependency parsing. It is ready for production, served through a REST API. This is the PHP client for the API. More details here: https://nlpcloud.com. Documentation: https://docs.nlpcloud.com. Github: https://github.com/nlpcloud/nlpcloud-php", 4 | "homepage": "http://github.com/nlpcloud/nlpcloud-php", 5 | "license": "MIT", 6 | "keywords": [ 7 | "api", 8 | "NLP", 9 | "ai", 10 | "deep learning", 11 | "machine learning", 12 | "data science", 13 | "nlpcloud" 14 | ], 15 | "authors": [ 16 | { 17 | "name": "Julien Salinas", 18 | "email": "all@juliensalinas.com" 19 | } 20 | ], 21 | "autoload": { 22 | "psr-4": { 23 | "NLPCloud\\": "src/" 24 | } 25 | }, 26 | "require": { 27 | "php": ">=7.2", 28 | "nategood/httpful": "*" 29 | } 30 | } -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "5606ba18aa8c08a20bab2f2219d8feee", 8 | "packages": [ 9 | { 10 | "name": "nategood/httpful", 11 | "version": "0.3.2", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/nategood/httpful.git", 15 | "reference": "0cded3ea97ba905600de9ceb9ef13f3ab681587c" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/nategood/httpful/zipball/0cded3ea97ba905600de9ceb9ef13f3ab681587c", 20 | "reference": "0cded3ea97ba905600de9ceb9ef13f3ab681587c", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "ext-curl": "*", 25 | "php": ">=7.2" 26 | }, 27 | "require-dev": { 28 | "phpunit/phpunit": "*" 29 | }, 30 | "type": "library", 31 | "autoload": { 32 | "psr-0": { 33 | "Httpful": "src/" 34 | } 35 | }, 36 | "notification-url": "https://packagist.org/downloads/", 37 | "license": [ 38 | "MIT" 39 | ], 40 | "authors": [ 41 | { 42 | "name": "Nate Good", 43 | "email": "me@nategood.com", 44 | "homepage": "http://nategood.com" 45 | } 46 | ], 47 | "description": "A Readable, Chainable, REST friendly, PHP HTTP Client", 48 | "homepage": "http://github.com/nategood/httpful", 49 | "keywords": [ 50 | "api", 51 | "curl", 52 | "http", 53 | "requests", 54 | "rest", 55 | "restful" 56 | ], 57 | "support": { 58 | "issues": "https://github.com/nategood/httpful/issues", 59 | "source": "https://github.com/nategood/httpful/tree/v0.3.2" 60 | }, 61 | "time": "2020-01-25T01:13:13+00:00" 62 | } 63 | ], 64 | "packages-dev": [], 65 | "aliases": [], 66 | "minimum-stability": "stable", 67 | "stability-flags": [], 68 | "prefer-stable": false, 69 | "prefer-lowest": false, 70 | "platform": { 71 | "php": ">=7.2" 72 | }, 73 | "platform-dev": [], 74 | "plugin-api-version": "2.0.0" 75 | } 76 | -------------------------------------------------------------------------------- /src/NLPCloud.php: -------------------------------------------------------------------------------- 1 | headers = array( 14 | 'Authorization' => 'Token ' . $token, 15 | 'User-Agent' => 'nlpcloud-php-client', 16 | ); 17 | 18 | $this->rootURL = self::BASE_URL . '/' . self::API_VERSION . '/'; 19 | 20 | if ($lang == 'en') { 21 | $lang = ''; 22 | } 23 | if ($lang == 'eng_Latn') { 24 | $lang = ''; 25 | } 26 | 27 | 28 | if ($gpu) { 29 | $this->rootURL = $this->rootURL . 'gpu/'; 30 | } 31 | 32 | if ($asynchronous) { 33 | $this->rootURL = $this->rootURL . 'async/'; 34 | } 35 | 36 | if ($lang != '') { 37 | $this->rootURL = $this->rootURL . $lang . '/'; 38 | } 39 | 40 | $this->rootURL = $this->rootURL . $model; 41 | } 42 | 43 | public function adGeneration($keywords) 44 | { 45 | $payload = array( 46 | 'keywords' => $keywords 47 | ); 48 | $response = \Httpful\Request::post($this->rootURL . '/' . 'ad-generation', $payload) 49 | ->expectsJson() 50 | ->sendsJson() 51 | ->addHeaders($this->headers) 52 | ->send(); 53 | 54 | if ($response->code >= 400) { 55 | throw new \Exception($response->code . ': ' . $response->body->detail); 56 | } 57 | 58 | return $response->body; 59 | } 60 | 61 | public function asr($url = NULL, $encodedFile = NULL, $inputLanguage = NULL) 62 | { 63 | $payload = array( 64 | 'url' => $url, 65 | 'encoded_file' => $encodedFile, 66 | 'input_language' => $inputLanguage 67 | ); 68 | $response = \Httpful\Request::post($this->rootURL . '/' . 'asr', $payload) 69 | ->expectsJson() 70 | ->sendsJson() 71 | ->addHeaders($this->headers) 72 | ->send(); 73 | 74 | if ($response->code >= 400) { 75 | throw new \Exception($response->code . ': ' . $response->body->detail); 76 | } 77 | 78 | return $response->body; 79 | } 80 | 81 | public function asyncResult($url) 82 | { 83 | $response = \Httpful\Request::get($url) 84 | ->expectsJson() 85 | ->sendsJson() 86 | ->addHeaders($this->headers) 87 | ->send(); 88 | 89 | if ($response->code >= 400) { 90 | throw new \Exception($response->code . ': ' . $response->body->detail); 91 | } 92 | 93 | return $response->body; 94 | } 95 | 96 | public function chatbot($input, $context = NULL, $history = NULL) 97 | { 98 | $payload = array( 99 | 'input' => $input, 100 | 'context' => $context, 101 | 'history' => $history 102 | ); 103 | $response = \Httpful\Request::post($this->rootURL . '/' . 'chatbot', $payload) 104 | ->expectsJson() 105 | ->sendsJson() 106 | ->addHeaders($this->headers) 107 | ->send(); 108 | 109 | if ($response->code >= 400) { 110 | throw new \Exception($response->code . ': ' . $response->body->detail); 111 | } 112 | 113 | return $response->body; 114 | } 115 | 116 | public function classification($text, $labels = NULL, $multiClass = True) 117 | { 118 | $payload = array( 119 | 'text' => $text, 120 | 'labels' => $labels, 121 | 'multi_class' => $multiClass 122 | ); 123 | $response = \Httpful\Request::post($this->rootURL . '/' . 'classification', $payload) 124 | ->expectsJson() 125 | ->sendsJson() 126 | ->addHeaders($this->headers) 127 | ->send(); 128 | 129 | if ($response->code >= 400) { 130 | throw new \Exception($response->code . ': ' . $response->body->detail); 131 | } 132 | 133 | return $response->body; 134 | } 135 | 136 | public function codeGeneration($instruction) 137 | { 138 | $payload = array( 139 | 'instruction' => $instruction 140 | ); 141 | $response = \Httpful\Request::post($this->rootURL . '/' . 'code-generation', $payload) 142 | ->expectsJson() 143 | ->sendsJson() 144 | ->addHeaders($this->headers) 145 | ->send(); 146 | 147 | if ($response->code >= 400) { 148 | throw new \Exception($response->code . ': ' . $response->body->detail); 149 | } 150 | 151 | return $response->body; 152 | } 153 | 154 | public function dependencies($text) 155 | { 156 | $payload = array( 157 | 'text' => $text, 158 | ); 159 | $response = \Httpful\Request::post($this->rootURL . '/' . 'dependencies', $payload) 160 | ->expectsJson() 161 | ->sendsJson() 162 | ->addHeaders($this->headers) 163 | ->send(); 164 | 165 | if ($response->code >= 400) { 166 | throw new \Exception($response->code . ': ' . $response->body->detail); 167 | } 168 | 169 | return $response->body; 170 | } 171 | 172 | public function embeddings($sentences) 173 | { 174 | $payload = array( 175 | 'sentences' => $sentences, 176 | ); 177 | $response = \Httpful\Request::post($this->rootURL . '/' . 'embeddings', $payload) 178 | ->expectsJson() 179 | ->sendsJson() 180 | ->addHeaders($this->headers) 181 | ->send(); 182 | 183 | if ($response->code >= 400) { 184 | throw new \Exception($response->code . ': ' . $response->body->detail); 185 | } 186 | 187 | return $response->body; 188 | } 189 | 190 | public function entities($text, $searchedEntity = NULL) 191 | { 192 | $payload = array( 193 | 'text' => $text, 194 | 'searched_entity' => $searchedEntity 195 | ); 196 | $response = \Httpful\Request::post($this->rootURL . '/' . 'entities', $payload) 197 | ->expectsJson() 198 | ->sendsJson() 199 | ->addHeaders($this->headers) 200 | ->send(); 201 | 202 | if ($response->code >= 400) { 203 | throw new \Exception($response->code . ': ' . $response->body->detail); 204 | } 205 | 206 | return $response->body; 207 | } 208 | 209 | public function generation( 210 | $text, 211 | $maxLength = NULL, 212 | $lengthNoInput = NULL, 213 | $endSequence = NULL, 214 | $removeInput = NULL, 215 | $numBeams = NULL, 216 | $numReturnSequences = NULL, 217 | $topK = NULL, 218 | $topP = NULL, 219 | $temperature = NULL, 220 | $repetitionPenalty = NULL, 221 | $badWords = NULL, 222 | $removeEndSequence = NULL 223 | ) { 224 | $payload = array( 225 | 'text' => $text, 226 | 'max_length' => $maxLength, 227 | 'length_no_input' => $lengthNoInput, 228 | 'end_sequence' => $endSequence, 229 | 'remove_input' => $removeInput, 230 | 'num_beams' => $numBeams, 231 | 'num_return_sequences' => $numReturnSequences, 232 | 'top_k' => $topK, 233 | 'top_p' => $topP, 234 | 'temperature' => $temperature, 235 | 'repetition_penalty' => $repetitionPenalty, 236 | 'bad_words' => $badWords, 237 | 'remove_end_sequence' => $removeEndSequence 238 | ); 239 | $response = \Httpful\Request::post($this->rootURL . '/' . 'generation', $payload) 240 | ->expectsJson() 241 | ->sendsJson() 242 | ->addHeaders($this->headers) 243 | ->send(); 244 | 245 | if ($response->code >= 400) { 246 | throw new \Exception($response->code . ': ' . $response->body->detail); 247 | } 248 | 249 | return $response->body; 250 | } 251 | 252 | public function gsCorrection($text) 253 | { 254 | $payload = array( 255 | 'text' => $text 256 | ); 257 | $response = \Httpful\Request::post($this->rootURL . '/' . 'gs-correction', $payload) 258 | ->expectsJson() 259 | ->sendsJson() 260 | ->addHeaders($this->headers) 261 | ->send(); 262 | 263 | if ($response->code >= 400) { 264 | throw new \Exception($response->code . ': ' . $response->body->detail); 265 | } 266 | 267 | return $response->body; 268 | } 269 | 270 | public function imageGeneration($text) 271 | { 272 | $payload = array( 273 | 'text' => $text 274 | ); 275 | $response = \Httpful\Request::post($this->rootURL . '/' . 'image-generation', $payload) 276 | ->expectsJson() 277 | ->sendsJson() 278 | ->addHeaders($this->headers) 279 | ->send(); 280 | 281 | if ($response->code >= 400) { 282 | throw new \Exception($response->code . ': ' . $response->body->detail); 283 | } 284 | 285 | return $response->body; 286 | } 287 | 288 | public function intentClassification($text) 289 | { 290 | $payload = array( 291 | 'text' => $text 292 | ); 293 | $response = \Httpful\Request::post($this->rootURL . '/' . 'intent-classification', $payload) 294 | ->expectsJson() 295 | ->sendsJson() 296 | ->addHeaders($this->headers) 297 | ->send(); 298 | 299 | if ($response->code >= 400) { 300 | throw new \Exception($response->code . ': ' . $response->body->detail); 301 | } 302 | 303 | return $response->body; 304 | } 305 | 306 | public function kwKpExtraction($text) 307 | { 308 | $payload = array( 309 | 'text' => $text 310 | ); 311 | $response = \Httpful\Request::post($this->rootURL . '/' . 'kw-kp-extraction', $payload) 312 | ->expectsJson() 313 | ->sendsJson() 314 | ->addHeaders($this->headers) 315 | ->send(); 316 | 317 | if ($response->code >= 400) { 318 | throw new \Exception($response->code . ': ' . $response->body->detail); 319 | } 320 | 321 | return $response->body; 322 | } 323 | 324 | public function langdetection($text) 325 | { 326 | $payload = array( 327 | 'text' => $text 328 | ); 329 | $response = \Httpful\Request::post($this->rootURL . '/' . 'langdetection', $payload) 330 | ->expectsJson() 331 | ->sendsJson() 332 | ->addHeaders($this->headers) 333 | ->send(); 334 | 335 | if ($response->code >= 400) { 336 | throw new \Exception($response->code . ': ' . $response->body->detail); 337 | } 338 | 339 | return $response->body; 340 | } 341 | 342 | public function paraphrasing($text) 343 | { 344 | $payload = array( 345 | 'text' => $text 346 | ); 347 | $response = \Httpful\Request::post($this->rootURL . '/' . 'paraphrasing', $payload) 348 | ->expectsJson() 349 | ->sendsJson() 350 | ->addHeaders($this->headers) 351 | ->send(); 352 | 353 | if ($response->code >= 400) { 354 | throw new \Exception($response->code . ': ' . $response->body->detail); 355 | } 356 | 357 | return $response->body; 358 | } 359 | 360 | public function question($question, $context = NULL) 361 | { 362 | $payload = array( 363 | 'question' => $question, 364 | 'context' => $context 365 | ); 366 | $response = \Httpful\Request::post($this->rootURL . '/' . 'question', $payload) 367 | ->expectsJson() 368 | ->sendsJson() 369 | ->addHeaders($this->headers) 370 | ->send(); 371 | 372 | if ($response->code >= 400) { 373 | throw new \Exception($response->code . ': ' . $response->body->detail); 374 | } 375 | 376 | return $response->body; 377 | } 378 | 379 | public function semanticSearch($text, $numResults = NULL) 380 | { 381 | $payload = array( 382 | 'text' => $text, 383 | 'num_results' => $numResults, 384 | ); 385 | $response = \Httpful\Request::post($this->rootURL . '/' . 'semantic-search', $payload) 386 | ->expectsJson() 387 | ->sendsJson() 388 | ->addHeaders($this->headers) 389 | ->send(); 390 | 391 | if ($response->code >= 400) { 392 | throw new \Exception($response->code . ': ' . $response->body->detail); 393 | } 394 | 395 | return $response->body; 396 | } 397 | 398 | public function semanticSimilarity($sentences) 399 | { 400 | $payload = array( 401 | 'sentences' => $sentences, 402 | ); 403 | $response = \Httpful\Request::post($this->rootURL . '/' . 'semantic-similarity', $payload) 404 | ->expectsJson() 405 | ->sendsJson() 406 | ->addHeaders($this->headers) 407 | ->send(); 408 | 409 | if ($response->code >= 400) { 410 | throw new \Exception($response->code . ': ' . $response->body->detail); 411 | } 412 | 413 | return $response->body; 414 | } 415 | 416 | 417 | public function sentenceDependencies($text) 418 | { 419 | $payload = array( 420 | 'text' => $text, 421 | ); 422 | $response = \Httpful\Request::post($this->rootURL . '/' . 'sentence-dependencies', $payload) 423 | ->expectsJson() 424 | ->sendsJson() 425 | ->addHeaders($this->headers) 426 | ->send(); 427 | 428 | if ($response->code >= 400) { 429 | throw new \Exception($response->code . ': ' . $response->body->detail); 430 | } 431 | 432 | return $response->body; 433 | } 434 | 435 | public function sentiment($text, $target = NULL) 436 | { 437 | $payload = array( 438 | 'text' => $text, 439 | 'target' => $target 440 | ); 441 | $response = \Httpful\Request::post($this->rootURL . '/' . 'sentiment', $payload) 442 | ->expectsJson() 443 | ->sendsJson() 444 | ->addHeaders($this->headers) 445 | ->send(); 446 | 447 | if ($response->code >= 400) { 448 | throw new \Exception($response->code . ': ' . $response->body->detail); 449 | } 450 | 451 | return $response->body; 452 | } 453 | 454 | public function speechSynthesis($text, $voice = NULL) 455 | { 456 | $payload = array( 457 | 'text' => $text, 458 | 'voice' => $voice 459 | ); 460 | $response = \Httpful\Request::post($this->rootURL . '/' . 'speech-synthesis', $payload) 461 | ->expectsJson() 462 | ->sendsJson() 463 | ->addHeaders($this->headers) 464 | ->send(); 465 | 466 | if ($response->code >= 400) { 467 | throw new \Exception($response->code . ': ' . $response->body->detail); 468 | } 469 | 470 | return $response->body; 471 | } 472 | 473 | public function summarization($text, $size = NULL) 474 | { 475 | $payload = array( 476 | 'text' => $text, 477 | 'size' => $size 478 | ); 479 | $response = \Httpful\Request::post($this->rootURL . '/' . 'summarization', $payload) 480 | ->expectsJson() 481 | ->sendsJson() 482 | ->addHeaders($this->headers) 483 | ->send(); 484 | 485 | if ($response->code >= 400) { 486 | throw new \Exception($response->code . ': ' . $response->body->detail); 487 | } 488 | 489 | return $response->body; 490 | } 491 | 492 | public function tokens($text) 493 | { 494 | $payload = array( 495 | 'text' => $text 496 | ); 497 | $response = \Httpful\Request::post($this->rootURL . '/' . 'tokens', $payload) 498 | ->expectsJson() 499 | ->sendsJson() 500 | ->addHeaders($this->headers) 501 | ->send(); 502 | 503 | if ($response->code >= 400) { 504 | throw new \Exception($response->code . ': ' . $response->body->detail); 505 | } 506 | 507 | return $response->body; 508 | } 509 | 510 | public function translation($text, $source, $target) 511 | { 512 | $payload = array( 513 | 'text' => $text, 514 | 'source' => $source, 515 | 'target' => $target 516 | ); 517 | $response = \Httpful\Request::post($this->rootURL . '/' . 'translation', $payload) 518 | ->expectsJson() 519 | ->sendsJson() 520 | ->addHeaders($this->headers) 521 | ->send(); 522 | 523 | if ($response->code >= 400) { 524 | throw new \Exception($response->code . ': ' . $response->body->detail); 525 | } 526 | 527 | return $response->body; 528 | } 529 | } 530 | --------------------------------------------------------------------------------