PHP Classes

File: backend/index.php

Recommend this page to a friend!
  Classes of Rodrigo Faustino   TradutorX   backend/index.php   Download  
File: backend/index.php
Role: Example script
Content type: text/plain
Description: Example script
Class: TradutorX
Let users send voice requests to ChatGPT or Google
Author: By
Last change:
Date: 1 month ago
Size: 906 bytes
 

Contents

Class file image Download
<?php

require '../vendor/autoload.php';

use
App\gpt\ChatGPT;
use
App\google\GoogleTranslator;
use
App\helper\CurlHandler;
use
App\TranslationRouter;
use
App\CacheHandler;

require_once
__DIR__ . '/config/config.php';

$curlHandler = new CurlHandler();
$cache = new CacheHandler();
$chatGPT = new ChatGPT(OPENAI_API_KEY, $curlHandler, $cache );
$googleTranslator = new GoogleTranslator(API, $curlHandler, $cache );
$router = new TranslationRouter($chatGPT, $googleTranslator);

if (
$_SERVER['REQUEST_METHOD'] == 'POST') {
   
$postData = file_get_contents('php://input');
   
$data = json_decode($postData, true);

   
$text = $data['text'] ?? '';
   
$langDestino = $data['langDestino'] ?? 'en';
   
$useGPT = $data['useGPT'] ?? false;
   
$response = $router->translate($text, $langDestino, $useGPT);

    echo
json_encode($response);
} else {
   
header('HTTP/1.1 405 Method Not Allowed');
    exit;
}