PHP code example of php-loves-ai / multimodal-ai-runner
1. Go to this page and download the library: Download php-loves-ai/multimodal-ai-runner library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
php-loves-ai / multimodal-ai-runner example snippets
use PhpLovesAi\Runner\TextToImage;
$image = (new TextToImage())->generate(
model: 'stabilityai/sd-turbo',
prompt: 'a cozy cat by the fireplace',
outputPath: storage_path('app/cat.png'),
steps: 1,
guidanceScale: 0.0,
);
use PhpLovesAi\Process\ModelPuller;
$paths = (new ModelPuller())->pull(
['openai-community/gpt2', 'distilbert/distilbert-base-uncased'],
onProgress: fn (string $chunk) => fwrite(STDERR, $chunk),
// allFiles: true, // download every file, as `pull --all` does
onSkipped: fn (string $model, int $files, int $bytes) => fwrite(STDERR, "{$model}: skipped {$files} files\n"),
);
// ['openai-community/gpt2' => '/var/www/my-app/.local/models/openai-community/gpt2', ...]
use PhpLovesAi\Runner\TextToImage;
// Finds the runner and the pulled model in the project's .local directory by itself.
$image = (new TextToImage())->generate(
model: 'stabilityai/sd-turbo',
prompt: 'a cozy cat by the fireplace',
outputPath: __DIR__ . '/cat.png',
steps: 1,
guidanceScale: 0.0,
);
// '/.../cat.png'
use PhpLovesAi\Runner\ImageToImage;
// Finds the runner and the pulled model in the project's .local directory by itself.
$imageToImage = new ImageToImage();
$bigger = $imageToImage->transform(
model: 'caidas/swin2SR-classical-sr-x2-64',
imagePath: storage_path('app/photo.jpg'),
outputPath: storage_path('app/photo-2x.png'),
);
$painting = $imageToImage->transform(
model: 'stabilityai/sd-turbo',
imagePath: storage_path('app/photo.jpg'),
outputPath: storage_path('app/painting.png'),
prompt: 'a watercolor painting',
strength: 0.6,
steps: 2,
guidanceScale: 0.0,
);
use PhpLovesAi\Runner\TextToText;
// Finds the runner and the pulled model in the project's .local directory by itself.
$answer = (new TextToText())->generate(
model: 'Qwen/Qwen2.5-0.5B-Instruct',
prompt: 'Summarize in one sentence: PHP is a popular general-purpose scripting language...',
systemPrompt: 'You are a concise assistant.',
maxNewTokens: 100,
temperature: 0.0,
);
use PhpLovesAi\Runner\ImageToText;
// Finds the runner and the pulled model in the project's .local directory by itself.
$imageToText = new ImageToText();
$description = $imageToText->generate('HuggingFaceTB/SmolVLM-256M-Instruct', storage_path('app/photo.jpg'));
$answer = $imageToText->generate(
model: 'HuggingFaceTB/SmolVLM-256M-Instruct',
imagePath: storage_path('app/photo.jpg'),
prompt: 'Is there any text in this image? Write it out.',
maxNewTokens: 100,
temperature: 0.0,
);
use PhpLovesAi\Runner\SpeechToText;
// Finds the runner and the pulled model in the project's .local directory by itself.
$speechToText = new SpeechToText();
$transcript = $speechToText->transcribe('openai/whisper-tiny', storage_path('app/interview.m4a'));
$english = $speechToText->transcribe(
model: 'openai/whisper-small',
audioPath: storage_path('app/interview-uk.mp3'),
language: 'uk',
translate: true,
);
// For subtitles: seconds from the start of the audio; the last segment may have no end.
$segments = $speechToText->transcribeWithTimestamps('openai/whisper-tiny', storage_path('app/interview.m4a'));
// [['start' => 0.0, 'end' => 5.56, 'text' => 'He hoped there would be stew for dinner, …'], ...]
use PhpLovesAi\Runner\TextToSpeech;
// Finds the runner and the pulled model in the project's .local directory by itself.
$file = (new TextToSpeech())->speak(
model: 'facebook/mms-tts-eng',
text: 'PHP loves AI, and now it can speak.',
outputPath: storage_path('app/hello.mp3'),
speed: 0.9,
);
// '/var/www/my-app/storage/app/hello.mp3'
use PhpLovesAi\Runner\TextToVideo;
// Finds the runner and the pulled model in the project's .local directory by itself.
$clip = (new TextToVideo())->generate(
model: 'Wan-AI/Wan2.1-T2V-1.3B-Diffusers',
prompt: 'a cat walking through tall grass',
outputPath: storage_path('app/cat.mp4'),
frames: 33,
fps: 16,
);
use PhpLovesAi\Runner\ImageToVideo;
// Finds the runner and the pulled model in the project's .local directory by itself.
$clip = (new ImageToVideo())->generate(
model: 'Wan-AI/Wan2.1-I2V-14B-480P-Diffusers',
imagePath: storage_path('app/photo.jpg'),
outputPath: storage_path('app/clip.mp4'),
prompt: 'the camera slowly zooms out',
frames: 33,
fps: 16,
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.