88 lines
5.6 KiB
PHP
88 lines
5.6 KiB
PHP
<?php
|
|
set_time_limit(30);
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
if($_SERVER['REQUEST_METHOD']==='OPTIONS'){http_response_code(200);exit;}
|
|
if($_SERVER['REQUEST_METHOD']==='GET'){die(json_encode(['status'=>'ok']));}
|
|
|
|
$in=json_decode(file_get_contents('php://input'),true)?:[];
|
|
$msg=trim($in['message']??'');$title=trim($in['title']??'Document WEVIA');
|
|
if(!$msg)die(json_encode(['error'=>'message required']));
|
|
|
|
require_once '/opt/wevads/vault/credentials.php';
|
|
$body=[['role'=>'system','content'=>'Tu es WEVIA. Reponds en francais avec tableaux markdown et structure.'],['role'=>'user','content'=>mb_substr($msg,0,2000)]];
|
|
|
|
$ch=curl_init('https://api.groq.com/openai/v1/chat/completions');
|
|
curl_setopt_array($ch,[CURLOPT_POST=>true,CURLOPT_RETURNTRANSFER=>true,CURLOPT_TIMEOUT=>15,CURLOPT_CONNECTTIMEOUT=>3,
|
|
CURLOPT_HTTPHEADER=>['Content-Type: application/json','Authorization: Bearer '.GROQ_KEY],
|
|
CURLOPT_POSTFIELDS=>json_encode(['model'=>'qwen-3-235b-a22b-instruct-2507-versatile','messages'=>$body,'max_tokens'=>1200,'temperature'=>0.7])]);
|
|
$r=curl_exec($ch);$code=curl_getinfo($ch,CURLINFO_HTTP_CODE);curl_close($ch);
|
|
$text='';$provider='?';
|
|
if($code==200&&$r){$d=json_decode($r,true);$text=trim($d['choices'][0]['message']['content']??'');$provider='Groq qwen-3-235b-a22b-instruct-2507';}
|
|
// Fallback Cerebras
|
|
if(strlen($text)<20){
|
|
$ck=(date("i")%2==0)?CEREBRAS_KEY:CEREBRAS_KEY2;
|
|
$ch2=curl_init("https://api.cerebras.ai/v1/chat/completions");
|
|
curl_setopt_array($ch2,[CURLOPT_POST=>true,CURLOPT_RETURNTRANSFER=>true,CURLOPT_TIMEOUT=>10,CURLOPT_CONNECTTIMEOUT=>3,
|
|
CURLOPT_HTTPHEADER=>["Content-Type: application/json","Authorization: Bearer ".$ck],
|
|
CURLOPT_POSTFIELDS=>json_encode(["model"=>"qwen-3-235b-a22b-instruct-2507","messages"=>$body,"max_tokens"=>1200,"temperature"=>0.7])]);
|
|
$r2=curl_exec($ch2);$code2=curl_getinfo($ch2,CURLINFO_HTTP_CODE);curl_close($ch2);
|
|
if($code2==200&&$r2){$d2=json_decode($r2,true);$text=trim($d2["choices"][0]["message"]["content"]??"");$provider="Cerebras Qwen-235B";}
|
|
}
|
|
// Fallback SambaNova
|
|
if(strlen($text)<20){
|
|
$ch3=curl_init("https://api.sambanova.ai/v1/chat/completions");
|
|
curl_setopt_array($ch3,[CURLOPT_POST=>true,CURLOPT_RETURNTRANSFER=>true,CURLOPT_TIMEOUT=>8,CURLOPT_CONNECTTIMEOUT=>2,
|
|
CURLOPT_HTTPHEADER=>["Content-Type: application/json","Authorization: Bearer 9541b2a0-6ddc-4e7d-a957-c348d6119c3f"],
|
|
CURLOPT_POSTFIELDS=>json_encode(["model"=>"Meta-Llama-3.3-70B-Instruct","messages"=>$body,"max_tokens"=>1200,"temperature"=>0.7])]);
|
|
$r3=curl_exec($ch3);$code3=curl_getinfo($ch3,CURLINFO_HTTP_CODE);curl_close($ch3);
|
|
if($code3==200&&$r3){$d3=json_decode($r3,true);$text=trim($d3["choices"][0]["message"]["content"]??"");$provider="SambaNova Llama-3.3-70B";}
|
|
}
|
|
if(strlen($text)<20)die(json_encode(['error'=>'API returned empty','provider'=>$provider]));
|
|
|
|
$id=uniqid('wevia_');$txt_file="/tmp/{$id}.txt";$pdf_file="/var/www/html/test-report/{$id}.pdf";
|
|
file_put_contents($txt_file,$text);
|
|
|
|
$py=<<<'PY'
|
|
import sys
|
|
import warnings
|
|
warnings.filterwarnings("ignore")
|
|
from fpdf import FPDF
|
|
class P(FPDF):
|
|
def header(self):
|
|
self.set_font('DV','B',16);self.set_text_color(88,28,135);self.cell(0,10,'WEVIA',ln=True,align='C')
|
|
self.set_font('DV','',8);self.set_text_color(120,120,120);self.cell(0,5,'Intelligence Artificielle Cognitive',ln=True,align='C')
|
|
self.ln(3);self.set_draw_color(88,28,135);self.line(10,self.get_y(),200,self.get_y());self.ln(4)
|
|
def footer(self):
|
|
self.set_y(-12);self.set_font('DV','',7);self.set_text_color(150,150,150);self.cell(0,8,f'WEVIA p.{self.page_no()}/{{nb}}',align='C')
|
|
pdf=P();pdf.alias_nb_pages();pdf.add_font('DV','','/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf');pdf.add_font('DV','B','/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf');pdf.add_page()
|
|
pdf.set_font('DV','B',13);pdf.set_text_color(30,30,30);pdf.cell(0,8,sys.argv[2],ln=True);pdf.ln(2)
|
|
for line in open(sys.argv[1],encoding='utf-8'):
|
|
s=line.strip()
|
|
if not s:pdf.ln(2);continue
|
|
if s.startswith('# '):pdf.ln(3);pdf.set_font('DV','B',13);pdf.set_text_color(88,28,135);pdf.cell(0,7,s[2:],ln=True);continue
|
|
if s.startswith('## '):pdf.ln(2);pdf.set_font('DV','B',11);pdf.set_text_color(60,60,60);pdf.cell(0,6,s[3:],ln=True);continue
|
|
if s.startswith('### '):pdf.set_font('DV','B',10);pdf.set_text_color(80,80,80);pdf.cell(0,5,s[4:],ln=True);continue
|
|
if s.startswith('|') and '|' in s[1:]:
|
|
cells=[c.strip() for c in s.split('|')[1:-1]]
|
|
if all(set(c)<=set('-: ') for c in cells):continue
|
|
w=190/max(len(cells),1)
|
|
if any(c.startswith('**') for c in cells):
|
|
pdf.set_font('DV','B',8);pdf.set_fill_color(88,28,135);pdf.set_text_color(255,255,255)
|
|
for c in cells:pdf.cell(w,5,c.replace('**',''),border=1,fill=True)
|
|
else:
|
|
pdf.set_font('DV','',8);pdf.set_text_color(30,30,30)
|
|
for c in cells:pdf.cell(w,5,c.replace('**',''),border=1)
|
|
pdf.ln();continue
|
|
if s.startswith(('- ','* ')):
|
|
pdf.set_font('DV','',9);pdf.set_text_color(30,30,30);pdf.cell(5,4,' ');pdf.multi_cell(180,4,s[2:].replace('**',''));continue
|
|
pdf.set_font('DV','',9);pdf.set_text_color(30,30,30);pdf.multi_cell(0,4,s.replace('**',''));pdf.ln(1)
|
|
pdf.output(sys.argv[3]);print('OK')
|
|
PY;
|
|
$py_file="/tmp/{$id}.py";file_put_contents($py_file,$py);
|
|
$out=shell_exec("python3 {$py_file} {$txt_file} ".escapeshellarg($title)." {$pdf_file} 2>&1");
|
|
if(file_exists($pdf_file)){
|
|
die(json_encode(['pdf_url'=>"/test-report/{$id}.pdf",'response'=>$text,'provider'=>$provider,'title'=>$title,'size'=>filesize($pdf_file)]));
|
|
}
|
|
die(json_encode(['error'=>'PDF gen failed: '.$out]));
|