Skip to content

Commit

Permalink
feat: phpcs workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Royal Simpson Pinto <[email protected]>
  • Loading branch information
royalpinto007 committed Aug 12, 2024
1 parent fc675a3 commit 2fc751c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: PHPCS Lint

on: [push, pull_request]

jobs:
phpcs:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP Action
uses: shivammathur/[email protected]
with:
php-version: '8.2.19'
tools: composer

- name: Configure Composer
run: composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true

- name: Install PHPCS and coding standards
run: |
composer global require "squizlabs/php_codesniffer=*"
composer global require drupal/coder
composer global require slevomat/coding-standard
- name: Configure PHPCS
run: ~/.composer/vendor/bin/phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer,$HOME/.composer/vendor/slevomat/coding-standard

- name: Run PHPCS
run: ~/.composer/vendor/bin/phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme .
43 changes: 29 additions & 14 deletions src/Controller/TextGenerationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,39 @@

namespace Drupal\text_generation\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Controller\ControllerBase;
use function Codewithkyrian\Transformers\Pipelines\pipeline;

/**
* Defines the Text Generation Controller.
*/
class TextGenerationController extends ControllerBase {
public function generateText(Request $request) {
// Decode JSON from the request body
$data = json_decode($request->getContent(), true);
$text = $data['text'] ?? null; // Use null coalescing operator to handle absent key

if (empty($text)) {
return new JsonResponse(['error' => 'No text provided'], 400);
}

$generator = pipeline('text-generation', 'Xenova/gpt2');
$output = $generator($text);

return new JsonResponse(['generated_text' => $output[0]['generated_text']]);

/**
* Generates text using a machine learning model.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object, containing JSON data.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* A JSON response with the generated text or an error message.
*/
public function generateText(Request $request) {
// Decode JSON from the request body.
$data = json_decode($request->getContent(), TRUE);
// Use null coalescing operator to handle absent key.
$text = $data['text'] ?? NULL;

if (empty($text)) {
return new JsonResponse(['error' => 'No text provided'], 400);
}

$generator = pipeline('text-generation', 'Xenova/gpt2');
$output = $generator($text);

return new JsonResponse(['generated_text' => $output[0]['generated_text']]);
}

}

0 comments on commit 2fc751c

Please sign in to comment.