183 lines
4.3 KiB
PHP
183 lines
4.3 KiB
PHP
<?php
|
||
|
||
class ImportGeminiJob extends Job {
|
||
|
||
public static function jobs() { return 1; }
|
||
|
||
public $upload_id;
|
||
|
||
public function __construct($upload_id) {
|
||
parent::__construct("upload:" . $upload_id);
|
||
$this->upload_id = $upload_id;
|
||
}
|
||
|
||
public function run() {
|
||
|
||
Config::refresh();
|
||
|
||
$pdf = false;
|
||
|
||
$subs = [
|
||
"Installation and Operating Information",
|
||
"Installation and Configuration",
|
||
"Installing and Getting Started",
|
||
"Installation/Operator's Manual",
|
||
"Installation/Operator's Guide",
|
||
"Programmer's Reference Guide",
|
||
"Field Maintenance Print Set",
|
||
"Illustrated Parts Breakdown",
|
||
"Installation/Owner's Guide",
|
||
"Installation Information",
|
||
"Installation/User Guide",
|
||
"User Documentation Kit",
|
||
"Programmer Information",
|
||
"Technical Description",
|
||
"Operator Information",
|
||
"Configuration Guide",
|
||
"Upgrade Information",
|
||
"Installation Manual",
|
||
"Service Information",
|
||
"Installation Guide",
|
||
"Programming Manual",
|
||
"Maintenance Manual",
|
||
"Maintenance Guide",
|
||
"Technical Summary",
|
||
"Operator's Guide",
|
||
"System Reference",
|
||
"User Information",
|
||
"Technical Manual",
|
||
"Language Manual",
|
||
"Service Manual",
|
||
"Service Guide",
|
||
"Read Me First",
|
||
"Owner's Guide",
|
||
"Release Notes",
|
||
"Options Guide",
|
||
"Users' Manual",
|
||
"User's Manual",
|
||
"HiTest Notes",
|
||
"User's Guide",
|
||
"Design Guide",
|
||
"User Manual",
|
||
"User Guide",
|
||
|
||
];
|
||
|
||
$doc = new Upload($this->upload_id);
|
||
$doc->ai = "P";
|
||
$doc->save();
|
||
$pdf = $doc->getPDF();
|
||
|
||
if (!$pdf) {
|
||
$this->fail();
|
||
$doc->ai = "F";
|
||
$this->status("PDF File Missing");
|
||
$doc->save();
|
||
return;
|
||
}
|
||
|
||
if ($pdf->size() > 50000000) {
|
||
$this->status("File too large");
|
||
$doc->ai = "F";
|
||
$doc->save();
|
||
$this->fail();
|
||
return;
|
||
}
|
||
|
||
$this->status("Processing " . $doc->filename . " with Gemini");
|
||
try {
|
||
$gemini = new Gemini();
|
||
$gemini->upload_callback([$this, "uploadcb"]);
|
||
$gemini->process_callback([$this, "processcb"]);
|
||
$lines = $gemini->geminiOverview($pdf);
|
||
} catch (Exception $e) {
|
||
$mess = $e->getMessage();
|
||
print($mess . "\n");
|
||
|
||
switch ($mess) {
|
||
case "This model is currently experiencing high demand. Spikes in demand are usually temporary. Please try again later.":
|
||
case "The request timed out. Please try again.":
|
||
case "HTTP Error 0 requesting AI assistance":
|
||
$this->retry();
|
||
print("Retrying\n");
|
||
break;
|
||
default:
|
||
$this->fail();
|
||
$this->status(substr($mess, 0, 250));
|
||
$doc->overview = $mess;
|
||
$doc->ai = "F";
|
||
$doc->save();
|
||
}
|
||
return;
|
||
}
|
||
|
||
$this->status("Postprocessing returned data");
|
||
$ld = explode("\n", $lines);
|
||
if (preg_match('/^\{(.*)\}$/', trim($ld[0]), $m)) {
|
||
$title = trim($m[1]);
|
||
|
||
$title = str_replace("’", "'", $title);
|
||
|
||
$newsub = "";
|
||
$newsubsub = "";
|
||
|
||
|
||
$b = explode(":", $title);
|
||
if (count($b) > 1) {
|
||
$title = trim(array_shift($b));
|
||
$newsub = trim(array_shift($b));
|
||
$newsubsub = implode(": ", $b);
|
||
}
|
||
|
||
if ($newsub == "") {
|
||
foreach ($subs as $sub) {
|
||
if (str_ends_with($title, $sub)) {
|
||
$newsub = $sub;
|
||
$title = substr($title, 0, 0 - (strlen($sub) + 1));
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
$doc->title = $title;
|
||
$doc->subtitle = $newsub;
|
||
$doc->subsubtitle = $newsubsub;
|
||
|
||
array_shift($ld);
|
||
if (trim($ld[0]) == "") {
|
||
array_shift($ld);
|
||
}
|
||
}
|
||
if (preg_match('/^\[(.*)\]$/', trim($ld[0]), $m)) {
|
||
|
||
$iid = IDMatch::find_docid($this->cleanup($m[1]));
|
||
|
||
if ($iid) {
|
||
$doc->ai_docid = $iid[0];
|
||
}
|
||
array_shift($ld);
|
||
}
|
||
$doc->overview = implode("\n", $ld);
|
||
$doc->ai = "Y";
|
||
|
||
$doc->save();
|
||
$this->status("Finished");
|
||
$this->finish();
|
||
return;
|
||
}
|
||
|
||
|
||
public function uploadcb($percent) {
|
||
$this->status("Uploading: " . $percent . "% complete");
|
||
}
|
||
|
||
public function processcb($message) {
|
||
$this->status($message);
|
||
}
|
||
|
||
function cleanup($txt) {
|
||
$txt = str_replace("Ø", "0", $txt);
|
||
$txt = str_replace(".", " ", $txt);
|
||
return $txt;
|
||
}
|
||
}
|