swapinvidya/laravel-huggingface-client
Composer 安装命令:
composer require swapinvidya/laravel-huggingface-client
包简介
A Laravel package for interacting with Hugging Face API
README 文档
README
swapinvidya/laravel-huggingface-client is a Laravel package that simplifies integration with the Hugging Face API. This package provides an easy way to interact with models for text generation, image creation, and other AI-powered features.
Features
- Text Generation: Generate text using models like
gpt2andgpt-neo. - Image Generation: Create images based on text prompts using models such as
stable-diffusion. - Custom Model Completions: Use any available Hugging Face model for specialized tasks.
- Seamless Integration: Built for Laravel, with easy dependency injection.
Installation
Step 1: Install via Composer
Run the following command in your Laravel project directory:
composer require swapinvidya/laravel-huggingface-client
Step 2: Publish Configuration
Publish the configuration file to customize your API key and other options:
php artisan vendor:publish --provider="Swapinvidya\HuggingFaceClient\HuggingFaceServiceProvider"
Step 3: Set Up API Key
Add your Hugging Face API key to the .env file:
HUGGINGFACE_API_KEY=your_huggingface_api_key
Configuration
The configuration file will be published to config/huggingface.php. You can customize your API key and other settings there.
return [ 'api_key' => env('HUGGINGFACE_API_KEY'), 'base_uri' => 'https://api-inference.huggingface.co/', ];
Usage
Inject the HuggingFaceClient in your controllers or services to use its capabilities.
Example: Generate Text
use Swapinvidya\HuggingFaceClient\HuggingFaceClient; public function generateText(HuggingFaceClient $huggingFaceClient) { $response = $huggingFaceClient->generateText('gpt2', 'Write a short story about a hero.'); return response()->json($response); }
Example: Generate Image
public function generateImage(HuggingFaceClient $huggingFaceClient) { $response = $huggingFaceClient->generateImage('stable-diffusion-v1', 'A futuristic city with flying cars.'); return response()->json($response); }
Example: Custom Model Completion
public function generateCompletion(HuggingFaceClient $huggingFaceClient) { $params = [ 'inputs' => 'Explain the theory of relativity in simple terms.', 'parameters' => [ 'temperature' => 0.7, 'max_new_tokens' => 200, ], ]; $response = $huggingFaceClient->generateCompletion('gpt-neo', $params); return response()->json($response); }
Available Methods
generateText($model, $input)
Generates text output using the specified model.
$model: The model to use (e.g.,gpt2,gpt-neo).$input: The text input/prompt for the model.
generateImage($model, $input)
Creates an image based on a text prompt.
$model: The image generation model (e.g.,stable-diffusion).$input: The text prompt describing the image.
generateCompletion($model, $params)
Generates a completion response with custom parameters.
$model: The model to use (e.g.,gpt-neo,opt).$params: Array of parameters includinginputsand optional model settings liketemperatureandmax_new_tokens.
Error Handling
The client will return error information in the response if a request fails:
$response = $huggingFaceClient->generateText('gpt2', 'Hello world'); if (isset($response['error'])) { // Handle the error return response()->json(['error' => $response['error']], 500); } return response()->json($response);
Testing
To test your package locally, you can link it to a Laravel project using:
composer config repositories.local '{"type": "path", "url": "../path/to/laravel-huggingface-client"}'
composer require swapinvidya/laravel-huggingface-client:@dev
Contributing
Feel free to open issues or submit pull requests for improvements and new features.
License
This package is open-source software licensed under the MIT license.
Developed by swapinvidya
swapinvidya/laravel-huggingface-client 适用场景与选型建议
swapinvidya/laravel-huggingface-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 182 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 swapinvidya/laravel-huggingface-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 swapinvidya/laravel-huggingface-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 182
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-09