Skip to content

Commit

Permalink
Init files
Browse files Browse the repository at this point in the history
  • Loading branch information
ArdaGnsrn committed Nov 12, 2023
1 parent 1234c53 commit 6c04192
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 101 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) ArdaGnsrn <[email protected]>
Copyright (c) Arda Günsüren <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
23 changes: 5 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/elevenlabs-laravel.jpg?t=1" width="419px" />](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).
<a href="https://www.buymeacoffee.com/ardagnsrn" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>

## Installation

Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
6 changes: 0 additions & 6 deletions config/elevenlabs-laravel.php

This file was deleted.

5 changes: 5 additions & 0 deletions config/elevenlabs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'api_key' => env('ELEVENLABS_API_KEY'),
];
19 changes: 0 additions & 19 deletions database/factories/ModelFactory.php

This file was deleted.

19 changes: 0 additions & 19 deletions database/migrations/create_elevenlabs_laravel_table.php.stub

This file was deleted.

Empty file removed resources/views/.gitkeep
Empty file.
19 changes: 0 additions & 19 deletions src/Commands/ElevenLabsCommand.php

This file was deleted.

3 changes: 3 additions & 0 deletions src/ElevenLabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace ArdaGnsrn\ElevenLabs;

use ArdaGnsrn\ElevenLabs\Traits\TextToSpeechTrait;

class ElevenLabs
{
use TextToSpeechTrait, Traits\ModelsTrait;
}
11 changes: 1 addition & 10 deletions src/ElevenLabsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
4 changes: 4 additions & 0 deletions src/Facades/ElevenLabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/Responses/TextToSpeechResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ArdaGnsrn\ElevenLabs\Responses;

use Illuminate\Support\Facades\Storage;
use Psr\Http\Message\ResponseInterface;

class TextToSpeechResponse
{
private ResponseInterface $response;

public function __construct(ResponseInterface $response)
{
$this->response = $response;
}

public function getResponse()
{
return $this->response;
}

public function saveFile($file): void
{
Storage::put($file, $this->response->getBody()->getContents());
}
}
16 changes: 16 additions & 0 deletions src/Traits/ModelsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ArdaGnsrn\ElevenLabs\Traits;

use ArdaGnsrn\ElevenLabs\Utils\API;

trait ModelsTrait
{
/**
* Get all models
*/
public function getModels(): array
{
return API::request('GET', 'models');
}
}
29 changes: 29 additions & 0 deletions src/Traits/TextToSpeechTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ArdaGnsrn\ElevenLabs\Traits;

use ArdaGnsrn\ElevenLabs\Responses\TextToSpeechResponse;
use ArdaGnsrn\ElevenLabs\Utils\API;

trait TextToSpeechTrait
{
/**
* Text to speech
*/
public function 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]): 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);
}
}
36 changes: 36 additions & 0 deletions src/Utils/API.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ArdaGnsrn\ElevenLabs\Utils;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Config;
use Psr\Http\Message\ResponseInterface;

class API
{
private const BASE_URL = 'https://api.elevenlabs.io/';

public static function request($method, $endpoint, $options = [], $isJson = true): ResponseInterface|array
{
try {
$client = new Client([
'base_uri' => 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;
}
}
}
15 changes: 13 additions & 2 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

it('can test', function () {
expect(true)->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();
});
});
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 6c04192

Please sign in to comment.