From 6c041923dd279865298360f02357bc5a5288c12e Mon Sep 17 00:00:00 2001 From: ARDA GUNSUREN Date: Sun, 12 Nov 2023 11:00:48 +0200 Subject: [PATCH] Init files --- LICENSE.md | 2 +- README.md | 23 +++--------- composer.json | 11 +++--- config/elevenlabs-laravel.php | 6 ---- config/elevenlabs.php | 5 +++ database/factories/ModelFactory.php | 19 ---------- .../create_elevenlabs_laravel_table.php.stub | 19 ---------- resources/views/.gitkeep | 0 src/Commands/ElevenLabsCommand.php | 19 ---------- src/ElevenLabs.php | 3 ++ src/ElevenLabsServiceProvider.php | 11 +----- src/Facades/ElevenLabs.php | 4 +++ src/Responses/TextToSpeechResponse.php | 26 ++++++++++++++ src/Traits/ModelsTrait.php | 16 +++++++++ src/Traits/TextToSpeechTrait.php | 29 +++++++++++++++ src/Utils/API.php | 36 +++++++++++++++++++ tests/ExampleTest.php | 15 ++++++-- tests/TestCase.php | 2 +- 18 files changed, 145 insertions(+), 101 deletions(-) delete mode 100644 config/elevenlabs-laravel.php create mode 100644 config/elevenlabs.php delete mode 100644 database/factories/ModelFactory.php delete mode 100644 database/migrations/create_elevenlabs_laravel_table.php.stub delete mode 100644 resources/views/.gitkeep delete mode 100644 src/Commands/ElevenLabsCommand.php create mode 100644 src/Responses/TextToSpeechResponse.php create mode 100644 src/Traits/ModelsTrait.php create mode 100644 src/Traits/TextToSpeechTrait.php create mode 100644 src/Utils/API.php diff --git a/LICENSE.md b/LICENSE.md index 403eb8b..3957432 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) ArdaGnsrn +Copyright (c) Arda Günsüren Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 37f1d56..1ee8072 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,12 @@ This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. -## Support us +## Buy me a coffee -[](https://spatie.be/github-ad-click/elevenlabs-laravel) +Whether you use this project, have learned something from it, or just like it, please consider supporting it by buying +me a coffee, so I can dedicate more time on open-source projects like this :) -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). - -We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). +Buy Me A Coffee ## Installation @@ -23,13 +22,6 @@ You can install the package via composer: composer require ardagnsrn/elevenlabs-laravel ``` -You can publish and run the migrations with: - -```bash -php artisan vendor:publish --tag="elevenlabs-laravel-migrations" -php artisan migrate -``` - You can publish the config file with: ```bash @@ -40,15 +32,10 @@ This is the contents of the published config file: ```php return [ + 'api_key' => env('ELEVENLABS_API_KEY'), ]; ``` -Optionally, you can publish the views using - -```bash -php artisan vendor:publish --tag="elevenlabs-laravel-views" -``` - ## Usage ```php diff --git a/composer.json b/composer.json index 46e3083..aec2cfb 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,9 @@ ], "require": { "php": "^8.1", - "spatie/laravel-package-tools": "^1.14.0", - "illuminate/contracts": "^10.0" + "guzzlehttp/guzzle": "^7.8", + "illuminate/contracts": "^10.0", + "spatie/laravel-package-tools": "^1.14.0" }, "require-dev": { "laravel/pint": "^1.0", @@ -35,14 +36,12 @@ }, "autoload": { "psr-4": { - "ArdaGnsrn\\ElevenLabs\\": "src/", - "ArdaGnsrn\\ElevenLabs\\Database\\Factories\\": "database/factories/" + "ArdaGnsrn\\ElevenLabs\\": "src/" } }, "autoload-dev": { "psr-4": { - "ArdaGnsrn\\ElevenLabs\\Tests\\": "tests/", - "Workbench\\App\\": "workbench/app/" + "ArdaGnsrn\\ElevenLabs\\Tests\\": "tests/" } }, "scripts": { diff --git a/config/elevenlabs-laravel.php b/config/elevenlabs-laravel.php deleted file mode 100644 index ad09e81..0000000 --- a/config/elevenlabs-laravel.php +++ /dev/null @@ -1,6 +0,0 @@ - env('ELEVENLABS_API_KEY'), +]; diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php deleted file mode 100644 index 1989eec..0000000 --- a/database/factories/ModelFactory.php +++ /dev/null @@ -1,19 +0,0 @@ -id(); - - // add fields - - $table->timestamps(); - }); - } -}; diff --git a/resources/views/.gitkeep b/resources/views/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/Commands/ElevenLabsCommand.php b/src/Commands/ElevenLabsCommand.php deleted file mode 100644 index 3809d40..0000000 --- a/src/Commands/ElevenLabsCommand.php +++ /dev/null @@ -1,19 +0,0 @@ -comment('All done'); - - return self::SUCCESS; - } -} diff --git a/src/ElevenLabs.php b/src/ElevenLabs.php index a101342..1620949 100644 --- a/src/ElevenLabs.php +++ b/src/ElevenLabs.php @@ -2,6 +2,9 @@ namespace ArdaGnsrn\ElevenLabs; +use ArdaGnsrn\ElevenLabs\Traits\TextToSpeechTrait; + class ElevenLabs { + use TextToSpeechTrait, Traits\ModelsTrait; } diff --git a/src/ElevenLabsServiceProvider.php b/src/ElevenLabsServiceProvider.php index 03637f1..5fe65e1 100644 --- a/src/ElevenLabsServiceProvider.php +++ b/src/ElevenLabsServiceProvider.php @@ -4,22 +4,13 @@ use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; -use ArdaGnsrn\ElevenLabs\Commands\ElevenLabsCommand; class ElevenLabsServiceProvider extends PackageServiceProvider { public function configurePackage(Package $package): void { - /* - * This class is a Package Service Provider - * - * More info: https://github.com/spatie/laravel-package-tools - */ $package ->name('elevenlabs-laravel') - ->hasConfigFile() - ->hasViews() - ->hasMigration('create_elevenlabs-laravel_table') - ->hasCommand(ElevenLabsCommand::class); + ->hasConfigFile('elevenlabs'); } } diff --git a/src/Facades/ElevenLabs.php b/src/Facades/ElevenLabs.php index 8e44a6e..319d7de 100644 --- a/src/Facades/ElevenLabs.php +++ b/src/Facades/ElevenLabs.php @@ -2,9 +2,13 @@ namespace ArdaGnsrn\ElevenLabs\Facades; +use ArdaGnsrn\ElevenLabs\Responses\TextToSpeechResponse; use Illuminate\Support\Facades\Facade; /** + * @method static array getModels() + * @method static TextToSpeechResponse textToSpeech(string $voiceId, string $text, string $modelId = "eleven_multilingual_v2", array $voiceSettings = ["stability" => 0.95, "similarity_boost" => 0.75, "style" => 0.06, "use_speaker_boost" => true]) + * * @see \ArdaGnsrn\ElevenLabs\ElevenLabs */ class ElevenLabs extends Facade diff --git a/src/Responses/TextToSpeechResponse.php b/src/Responses/TextToSpeechResponse.php new file mode 100644 index 0000000..f742c40 --- /dev/null +++ b/src/Responses/TextToSpeechResponse.php @@ -0,0 +1,26 @@ +response = $response; + } + + public function getResponse() + { + return $this->response; + } + + public function saveFile($file): void + { + Storage::put($file, $this->response->getBody()->getContents()); + } +} diff --git a/src/Traits/ModelsTrait.php b/src/Traits/ModelsTrait.php new file mode 100644 index 0000000..ae3c346 --- /dev/null +++ b/src/Traits/ModelsTrait.php @@ -0,0 +1,16 @@ + 0.95, 'similarity_boost' => 0.75, 'style' => 0.06, 'use_speaker_boost' => true]): TextToSpeechResponse + { + $response = API::request('POST', "text-to-speech/$voiceId", [ + 'json' => [ + 'text' => $text, + 'model_id' => $modelId, + 'voice_settings' => $voiceSettings, + ], + 'headers' => [ + 'Accept' => 'audio/mpeg', + ], + 'stream' => true, + ], false); + + return new TextToSpeechResponse($response); + } +} diff --git a/src/Utils/API.php b/src/Utils/API.php new file mode 100644 index 0000000..9dd2fbb --- /dev/null +++ b/src/Utils/API.php @@ -0,0 +1,36 @@ + self::BASE_URL, + 'headers' => [ + 'xi-api-key' => Config::get('elevenlabs.api_key'), + 'Content-Type' => 'application/json', + ], + ]); + $response = $client->request($method, 'v1/'.$endpoint, $options); + if ($isJson) { + return json_decode($response->getBody()->getContents(), true); + } + + return $response; + } catch (\GuzzleHttp\Exception\ClientException $exception) { + if ($exception->getCode() === 401) { + throw new \Exception('Invalid API key'); + } + throw $exception; + } + } +} diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 5d36321..6d8d409 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -1,5 +1,16 @@ toBeTrue(); +describe('Methods Unit Test', function () { + test('getModels() function', function () { + $models = \ArdaGnsrn\ElevenLabs\Facades\ElevenLabs::getModels(); + expect($models)->toBeArray(); + expect($models)->not->toBeEmpty(); + }); + test('textToSpeech() function', function () { + $response = \ArdaGnsrn\ElevenLabs\Facades\ElevenLabs::textToSpeech('2EiwWnXFnvU5JabPnv8n', 'H'); + expect($response)->toBeInstanceOf(\ArdaGnsrn\ElevenLabs\Responses\TextToSpeechResponse::class); + + $response->saveFile('test.mp3'); + expect(\Illuminate\Support\Facades\Storage::exists('test.mp3'))->toBeTrue(); + }); }); diff --git a/tests/TestCase.php b/tests/TestCase.php index 51f35c5..31530aa 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,9 @@ namespace ArdaGnsrn\ElevenLabs\Tests; +use ArdaGnsrn\ElevenLabs\ElevenLabsServiceProvider; use Illuminate\Database\Eloquent\Factories\Factory; use Orchestra\Testbench\TestCase as Orchestra; -use ArdaGnsrn\ElevenLabs\ElevenLabsServiceProvider; class TestCase extends Orchestra {