12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace manager\controllers;
- use app\models\OllamaAI;
- use app\models\NvidiaAI;
- use Yii;
- class AiController extends BaseController
- {
- public function actionIndex()
- {
- return 'Используй API для получения данных';
- }
- public function actionAjaxGetTitle()
- {
- if (Yii::$app->request->isPost){
- $cache = Yii::$app->cache;
- $type = Yii::$app->request->post('AI');
- $text = Yii::$app->request->post('text');
- $progress = Yii::$app->request->post('progress');
- $key = 'AI_time_mid'.$type.$progress;
- if( $text && $type == 'ollama' ){
- $ses = false;
- if( \Yii::$app->session->isActive ){
- \Yii::$app->session->close();
- $ses = true;
- }
- $ollama = new OllamaAI();
- $r = $ollama->generate(strip_tags($text));
- $data = $ollama->Getres();
- $t = $ollama->GetTime();
- $tm = $cache->get($key);
- if( $tm ){
- $cache->set($key, ($t+$tm)/2);
- }else{
- $cache->set($key, $t);
- }
- if( $ses ) \Yii::$app->session->open();
- return json_encode( ['status'=>'ok', 'data'=>$data, 'progress'=>$progress] );
- }elseif( $text && $type == 'nv' ){
- $ses = false;
- if( \Yii::$app->session->isActive ){
- \Yii::$app->session->close();
- $ses = true;
- }
- $nv = new NvidiaAI();
- $r = $nv->generate(strip_tags($text));
- if( $r ){
- $data = $nv->Getres();
- }else{
- if( $ses ) \Yii::$app->session->open();
- return json_encode( ['status'=>'err', 'msg'=>'Нет доступа до AI '.$type] );
- }
- $t = $nv->GetTime();
- $tm = $cache->get($key);
- if( $tm ){
- $cache->set($key, ($t+$tm)/2);
- }else{
- $cache->set($key, $t);
- }
- if( $ses ) \Yii::$app->session->open();
- return json_encode( ['status'=>'ok', 'data'=>$data, 'progress'=>$progress] );
- }else{
- return json_encode( ['status'=>'err', 'msg'=>'Нет доступа до AI '.$type] );
- }
- }
- return json_encode( ['status'=>'err', 'msg'=>'Ошибка получения данных'] );
- }
- public function actionAjaxTime()
- {
- $tm = 0;
- if (Yii::$app->request->isPost){
- $cache = Yii::$app->cache;
- $type = Yii::$app->request->post('AI');
- $progress = Yii::$app->request->post('progress');
- $key = 'AI_time_mid'.$type.$progress;
- $tm = $cache->get($key);
- }
- return json_encode( ['status'=>'ok', 'time'=>$tm] );
- }
- }
|