HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/laravel/public/gpt-tuning.php
<?php

function downloadFileByUrl($url, $outFileName)
{
    shell_exec('cd /var/www/ai-notam/laravel/storage/app/public/uploads/ && wget ' . $url);
}

function deployFile($file, $apiKey) {
    $url = 'https://api.openai.com/v1/files';
    $headers = [
        'Content-Type: multipart/form-data',
        'Authorization: Bearer ' . $apiKey,
    ];

    $fileName = basename($file);
    downloadFileByUrl($file, $fileName);

    $data = [
        'purpose' => 'fine-tune',
        'file' => curl_file_create('/var/www/ai-notam/laravel/storage/app/public/uploads/'.$fileName, 'application/jsonl', $fileName),
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        $result = curl_error($ch);
    }
    curl_close($ch);

    return json_decode($result);
}

try {
    $params = json_decode(file_get_contents('php://input'), true);
    header('Content-Type: application/json');
    $input = '';

    $status = 'success';
    $data = [];

    $apiKey = $params['key'];
    $path = $params['path'];

    $headers = [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $apiKey,
    ];

    if (!empty($params['file'])) {
        $uploadedFile = deployFile($params['file'], $apiKey);

        if (property_exists($uploadedFile, 'id')) {
            $data = [
                'training_file' => $uploadedFile->id,
                'model' => $params['model']
            ];
        }
    }


    $url = 'https://api.openai.com/v1/' . $path;

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($params['method'] !== 'GET') {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        $result = curl_error($ch);
    }

    curl_close($ch);

    echo $result;
} catch (\Exception $e) {
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}