Commit e54c2813 authored by GROSS-AMBLARD David's avatar GROSS-AMBLARD David
Browse files

Adds Wirk service call for image annotation

Corresponds to ServiceCall in db
parent d14a7397
......@@ -205,10 +205,10 @@ function step($conn, $artifactid, $from, $to, $guard, $actions)
$action="
select @TITLE:='default title';
select @DESCRIPTION:='default description';
select @TEMPLATE:='default template';
select @BODY:='default body';
select @TEMPLATE:='';
select @BODY:=@TEMPLATE;
select @CHECKER:=NULL;
select @CHECKERMSG:='default checker message';
select @CHECKERMSG:=NULL;
";
$action.="select @TASKID:=".$matches[1].";";
switch ($matches[2]){
......@@ -244,12 +244,28 @@ function step($conn, $artifactid, $from, $to, $guard, $actions)
$action=str_replace("CURRENT_ARTIFACT",$artifactid, $action);
if($action=="PIGNISTIC")
pignistic($conn,$artifactid);
else
$conn->query($action);
if (preg_match("/^call wirk image annotator as task ([0-9]+) for '(.*)'/",$action,$matches)){
$taskid=$matches[1];
$imageUrlQuery=$matches[2];
$imageUrls=$conn->query($imageUrlQuery);
$imageUrl=$imageUrls->fetch()[0];
callWirkImageAnnotator($imageUrl,$artifactid,$taskid,$_SESSION['id'],$conn);
$action="";
}
if (preg_match("/^check wirk answer/",$action,$matches)){
checkWirkImageAnnotator($conn);
$action="";
}
if($action=="PIGNISTIC"){
pignistic($conn,$artifactid);
$action="";
}
if ($action)
$conn->query($action);
}
}
......@@ -310,5 +326,194 @@ function pignistic($conn,$artifactid){
$conn->query("insert into Answer(idtask,user,Artifact,value,mass) select MAX(idtask)+1,1,$artifactid,$choice,1 from Answer where Artifact=$artifactid");
}
/**
* callWirkImageAnnotator
*
* given an image URL, call the Wirk API for image annotation
* @param $imageURL: the URL of the image to annotate
* @param $artifactid: the id of the artifact issuing the call
* @param $taskid: the id of the task corresponding to this call
* @param $userid: the id of the user of the artifact
* @param conn: a valid PDO connection
*/
// TODO move this away is a specific package
function callWirkImageAnnotator($imageUrl,$artifactid,$taskid,$userid,$conn){
// Setup
$userApi = "headwork_wirk.io";
$passwordApi = "Y2h/Nq8p5VZF29kHXfEhuzl6c4a5FqkElw==";
$idApp = 853;
$idQuality = 1386;
$title = "Headwork/Wirk image categorization ($artifactid,$taskid,$userid)";
// Create AppProject
$bodyAppProject = new \stdClass();
$bodyAppProject->IdApp = $idApp;
$bodyAppProject->Title = $title;
$bodyAppProject->IdQuality = $idQuality;
$bodyAppProject->UrlNotification = "";
debug("Create AppProject");
$answerAppProject = CallAPI("POST", "https://api.wirk.io/v1_0/AppProject", $userApi, $passwordApi, json_encode($bodyAppProject));
debug("Call: ".json_encode($bodyAppProject));
debug("Reponse: ".$answerAppProject);
$jsonAppProject = json_decode($answerAppProject);
$IdAppProject = $jsonAppProject->{'IdAppProject'};
// Create request
$bodyTaskLine = new \stdClass();
$bodyTaskLine->IdAppProject = $IdAppProject;
$bodyTaskLine->Inputs[] = $imageUrl;
debug("Create TaskLine");
$answerTaskLine = CallAPI("POST", "https://api.wirk.io/v1_0/TaskLine", $userApi, $passwordApi, json_encode($bodyTaskLine));
debug("Call: ".json_encode($bodyTaskLine));
debug("Reponse: ".$answerTaskLine);
$jsonTaskLine = json_decode($answerTaskLine);
$IdTaskLine = $jsonTaskLine->{'IdTaskLine'};
$conn->query("insert into ServiceCall(artifactid,taskid,tasklineid,userid,description) values ($artifactid,$taskid,$IdTaskLine,$userid,'Wirk Image Annotator')");
/*
//sleep("350");
sleep("120");
// Get answer
echo "<b>Get TaskLine:</b><br>";
$answerTaskLineAnswer = CallAPI("GET", "https://api.wirk.io/v1_0/TaskLine/".$IdTaskLine, $userApi, $passwordApi);
echo "Reponse: ".$answerTaskLineAnswer."<br><br>";
*/
}
/**
* callAPI
* Call an external API
* @author Wirk
*/
function CallAPI($method, $url, $username, $password, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
/**
* chekWirkImageAnnotator
*
* Check if the current calls to Wirk image annotator have finished. Fills the corresponding
* Answer and Answered tables.
* @param $conn: a valid PDO connection
*/
function checkWirkImageAnnotator($conn){
// TODO The taskline from creation step is required
// Setup
$userApi = "headwork_wirk.io";
$passwordApi = "Y2h/Nq8p5VZF29kHXfEhuzl6c4a5FqkElw==";
//$idApp = 853;
//$idQuality = 1386;
//$title = "Mon projet de categorisation";
//$image = "https://static.lpnt.fr/images/2019/09/25/19403277lpw-19403339-article-chat-etude-felin-jpg_6528763_660x281.jpg";
//$image = "https://www.rd.com/wp-content/uploads/2020/03/GettyImages-1060486568.jpg?resize=1536,1536";
// Create AppProject
//$bodyAppProject = new \stdClass();
//$bodyAppProject->IdApp = $idApp;
/*
$bodyAppProject->Title = $title;
$bodyAppProject->IdQuality = $idQuality;
$bodyAppProject->UrlNotification = "";
*/
/*
echo "<b>Create AppProject:</b><br>";
$answerAppProject = CallAPI("POST", "https://api.wirk.io/v1_0/AppProject", $userApi, $passwordApi, json_encode($bodyAppProject));
echo "Call: ".json_encode($bodyAppProject)."<br>";
echo "Reponse: ".$answerAppProject."<br><br>";
$jsonAppProject = json_decode($answerAppProject);
$IdAppProject = $jsonAppProject->{'IdAppProject'};
// Create request
$bodyTaskLine = new \stdClass();
$bodyTaskLine->IdAppProject = $IdAppProject;
$bodyTaskLine->Inputs[] = $image;
echo "<b>Create TaskLine:</b><br>";
$answerTaskLine = CallAPI("POST", "https://api.wirk.io/v1_0/TaskLine", $userApi, $passwordApi, json_encode($bodyTaskLine));
echo "Call: ".json_encode($bodyTaskLine)."<br>";
echo "Reponse: ".$answerTaskLine."<br><br>";
*/
$awaitingServices=$conn->query("select * from ServiceCall");
foreach($awaitingServices as $awaitingService){
debug("Get TaskLine: ".$awaitingService['tasklineid']);
$IdTaskLine=$awaitingService['tasklineid'];
$artifactid=$awaitingService['artifactid'];
$taskid=$awaitingService['taskid'];
$userid=$awaitingService['userid'];
// TODO add taskid into ServiceCall and put it in callwirk
$answerTaskLineAnswer = CallAPI("GET", "https://api.wirk.io/v1_0/TaskLine/".$IdTaskLine, $userApi, $passwordApi);
debug("answer to call: ".$answerTaskLineAnswer);
$jsonAnswer=json_decode($answerTaskLineAnswer);
$tasks=$jsonAnswer->{'Tasks'};
if(empty($tasks))
debug("Not yet backed");
else {
$result=$tasks[0]->{'Outputs'}->{'1605'};
$conn->query("insert into Answer(idtask,user,artifact,value) values ($taskid,$userid,$artifactid,'".$result."')");
$conn->query("insert into Answered(id,artifact) values ($taskid,$artifactid)");
$conn->query("delete from ServiceCall where artifactid=$artifactid and userid=$userid and taskid=$taskid");
}
// Thu, 29 Apr 2021 09:36:51 +0000 checkWirkImageAnnotator:answer to call: {"IdTaskLine":625941,"IdAppProject":6334,"Inputs":["https://www.rd.com/wp-content/uploads/2020/03/GettyImages-1060486568.jpg?resize=1536,1536"],"Tasks":[]}
// Reponse: {"IdTaskLine":625967,"IdAppProject":6337,"Inputs":["https://www.rd.com/wp-content/uploads/2020/03/GettyImages-1060486568.jpg?resize=1536,1536"],"Tasks":[{"IdTask":812315,"Outputs":{"1605":"animal_panda","1606":"0.99609375"},"UpdateDate":"2021-04-29T14:39:29.927"}]}
}
}
//$jsonTaskLine = json_decode($answerTaskLine);
//$IdTaskLine = $jsonTaskLine->{'IdTaskLine'};
// Get answer
?>
......@@ -34,6 +34,7 @@ DROP TABLE IF EXISTS TaskClass;
DROP TABLE IF EXISTS Task;
DROP TABLE IF EXISTS StartMe;
DROP TABLE IF EXISTS Answered;
DROP TABLE IF EXISTS ServiceCall;
-- TODO missing other chat/forum tables, bad name format
......@@ -565,3 +566,11 @@ CREATE TABLE Answered (
artifact int NOT NULL
);
CREATE TABLE ServiceCall (
artifactid int NOT NULL,
taskid int NOT NULL,
userid int NOT NULL,
tasklineid int NOT NULL,
description varchar(100) NOT NULL
);
insert into SkillTree values
(2000, 1, 'Wirk'),
(2001, 2000, 'Basics');
......@@ -2,6 +2,12 @@ DELETE from Artifact where classid in (select id from ArtifactClass where projec
DELETE from ArtifactClass where project = 'Wirk';
DELETE from StartMe;
-- Chasser le Totoro Artifact
insert into ArtifactClass(id,description,definition,tablename,project) values (7000,'Chasser le Totoro','Wirk/wirk.sca', 'Artifact', 'Wirk');
-- sending to broadcast
insert into Artifact(id,ownerid,classid,state,node,attributes) values (2000,0,7000,'running',1,1);
-- Wirk Service call example
insert into ArtifactClass(id,description,definition,tablename,project) values (7001,'Wirk service call demo','Wirk/wirk-service.sca', 'Artifact', 'Wirk');
-- sending to broadcast
insert into Artifact(id,ownerid,classid,state,node,attributes) values (2001,0,7001,'running',1,1);
{
"1": {
"2": {
"guard": "select true from dual",
"actions": [
"select @TEMPLATE:='Give the URL of a picture, I will guess its content!'",
"select @TITLE:='Image Oracle'",
"select @CHECKER:='REGEXP \"^(https?://|www\\\\.)[\\.A-Za-z0-9/\\-]+\\\\.[a-zA-Z]{2,4}\"'",
"select @CHECKERMSG:='Please enter a valid URL (http://, https://, ...)'",
"select @TYPE:=NULL",
"select @ARTIFACTID:=CURRENT_ARTIFACT",
"insert into Task(id,title,description,body,checker,checkermsg,type,artifact) values (1,'demo',@TITLE,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE,@ARTIFACTID)",
"insert into Profile values (1,CURRENT_ARTIFACT,1)"
]
}
},
"2": {
"3": {
"guard": "select true from Answered where id=1 and artifact=CURRENT_ARTIFACT",
"actions": [
"call wirk image annotator as task 3 for 'select value from Answer where idtask=1 and artifact=CURRENT_ARTIFACT'",
"delete from Task where artifact=CURRENT_ARTIFACT",
"prepare task 2 as message",
"use 'Waiting for the oracle' as title",
"use 'Waiting for the oracle' as description",
"select @TEMPLATE:='<iframe width=\"100%\" height=\"480px\" src=\"https://www.watchisup.com/timer-free/embed/1970-01-01-00-03-609?backgroundcolor=&color=\" frameborder=\"0\" allowfullscreen></iframe>'",
"forget profile for current artifact",
"offer task to anyone",
"install task"
]
}
},
"3": {
"4": {
"guard": "task 3 is answered",
"actions": [
"delete from Task where artifact=CURRENT_ARTIFACT",
"prepare task 4 as message",
"use 'I got it!' as title",
"use 'I got it!' as description",
"select @RESULT:=value from Answer where idtask=3 and user=SESSION_USER and artifact=CURRENT_ARTIFACT",
"select @TEMPLATE:=concat('I think this is ...',@RESULT,'!') from dual",
"forget profile for current artifact",
"offer task to anyone",
"install task"
]
},
"3":{
"guard": "task 2 is answered",
"actions":[
"check wirk answer",
"delete from Answer where idtask=2 and artifact=CURRENT_ARTIFACT",
"delete from Answered where id=2 and artifact=CURRENT_ARTIFACT"
]
}
},
"4": {
"5":{
"guard":"task 4 is answered",
"actions":[
"delete from Task where artifact=CURRENT_ARTIFACT"
]
}
},
"5":{}
}
......@@ -8,32 +8,32 @@
"select @CHECKER:='REGEXP \"^(https?://|www\\\\.)[\\.A-Za-z0-9/\\-]+\\\\.[a-zA-Z]{2,4}\"'",
"select @CHECKERMSG:='Entrez une URL valide (http://, https://, ...)'",
"select @TYPE:=NULL",
"select @ARTIFACTID:=2000",
"insert into Task(title,description,body,checker,checkermsg,type,artifact) values ('demo',@TITLE,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE,@ARTIFACTID)",
"insert into Profile (select id,artifact,1 from Task)"
"select @ARTIFACTID:=CURRENT_ARTIFACT",
"insert into Task(id,title,description,body,checker,checkermsg,type,artifact) values (1,'demo',@TITLE,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE,@ARTIFACTID)",
"insert into Profile values (1,CURRENT_ARTIFACT,1)"
]
}
},
"2": {
"3": {
"guard": "select count(*)>=1 from Answer",
"guard": "task 1 is answered",
"actions": [
"select @TEMPLATE:='Merci ! Entrez votre identifiant Wirk'",
"select @TITLE:='Obtenez votre code de validation'",
"select @CHECKER:=null",
"select @CHECKERMSG:=null",
"select @TYPE:=NULL",
"select @ARTIFACTID:=2000",
"select @ARTIFACTID:=CURRENT_ARTIFACT",
"delete from Task where artifact=@ARTIFACTID",
"insert into Task(title,description,body,checker,checkermsg,type,artifact) values ('demo',@TITLE,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE,@ARTIFACTID)",
"insert into Profile (select id,artifact,1 from Task)"
"insert into Task(id,title,description,body,checker,checkermsg,type,artifact) values (2,'demo',@TITLE,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE,@ARTIFACTID)",
"insert into Profile values (2,CURRENT_ARTIFACT,1)"
]
}
},
"3": {
"4": {
"guard": "select count(*)>=2 from Answer",
"guard": "task 2 is answered",
"actions": [
"select @SECRET:=42666",
"select @WIRKID:=value from Answer where user=SESSION_USER and idtask=2",
......@@ -44,18 +44,18 @@
"select @CHECKER:=null",
"select @CHECKERMSG:=null",
"select @TYPE:=0",
"select @ARTIFACTID:=2000",
"select @ARTIFACTID:=CURRENT_ARTIFACT",
"delete from Task where artifact=@ARTIFACTID",
"insert into Task(title,description,body,checker,checkermsg,type,artifact) values ('demo',@TITLE,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE,@ARTIFACTID)",
"insert into Profile (select id,artifact,1 from Task)"
"insert into Task(id,title,description,body,checker,checkermsg,type,artifact) values (3,'demo',@TITLE,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE,@ARTIFACTID)",
"insert into Profile values (3,CURRENT_ARTIFACT,1)"
]
}
},
"4": {
"5": {
"guard": "select count(*)>=3 from Answer",
"guard": "task 3 is answered",
"actions": [
"select @ARTIFACTID:=2000",
"select @ARTIFACTID:=CURRENT_ARTIFACT",
"delete from Task where artifact=@ARTIFACTID"
]
}
......
{
"1": {
"2": {
"guard": "select true from dual",
"actions": [
"select @TEMPLATE:=body from Template where id='headwork-gui-1'",
"select @TITLE:='Simple question, simple checking'",
"select @CHECKER:='REGEXP \"^A$\"'",
"select @CHECKERMSG:='Please enter the letter A'",
"select @TYPE=NULL",
"insert into task(title,description,body,checker,checkermsg,type,artifact) values ('demo',@TITLE,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE,1001)",
"insert into profile (select id,1 from task)"
]
}
},
"2": {
"3": {
"guard": "select count(*)>=1 from answer",
"actions": [
"delete from task where artifact=1001",
"select @TEMPLATE:=body from Template where id='headwork-gui-2'",
"select @TITLE:='HTML'",
"select @CHECKER:=NULL",
"select @CHECKERMSG:=NULL",
"select @TYPE=NULL",
"insert into task(title,description,body,checker,checkermsg,type,artifact) values ('demo',@TITLE,@TEMPLATE,@CHECKER,@CHECKERMSG,@TYPE,1001)",
"insert into profile (select id,1 from task)"
]
}
},
"3": {
"4": {
"guard": "select false from dual",
"actions": [
"select @TEMPLATE:=body from Template where id='headwork-step2'",
"select @TITLE:='title'",
"insert into task(title,description,body,type,artifact) values ('demo',@TITLE,@TEMPLATE,0,1001)",
"insert into profile (select id,1 from task)"
]
}
},
"4":{}
}
\ No newline at end of file
<?php
// TODO The taskline from creation step is required
// TODO Make a simple wirk with URL selector, identification
// Setup
$userApi = "headwork_wirk.io";
$passwordApi = "Y2h/Nq8p5VZF29kHXfEhuzl6c4a5FqkElw==";
$idApp = 853;
$idQuality = 1386;
$title = "Mon projet de categorisation";
//$image = "https://static.lpnt.fr/images/2019/09/25/19403277lpw-19403339-article-chat-etude-felin-jpg_6528763_660x281.jpg";
$image = "https://www.rd.com/wp-content/uploads/2020/03/GettyImages-1060486568.jpg?resize=1536,1536";
// Create AppProject
$bodyAppProject = new \stdClass();
$bodyAppProject->IdApp = $idApp;
$bodyAppProject->Title = $title;
$bodyAppProject->IdQuality = $idQuality;
$bodyAppProject->UrlNotification = "";
/*
echo "<b>Create AppProject:</b><br>";
$answerAppProject = CallAPI("POST", "https://api.wirk.io/v1_0/AppProject", $userApi, $passwordApi, json_encode($bodyAppProject));
echo "Call: ".json_encode($bodyAppProject)."<br>";
echo "Reponse: ".$answerAppProject."<br><br>";
$jsonAppProject = json_decode($answerAppProject);
$IdAppProject = $jsonAppProject->{'IdAppProject'};
// Create request
$bodyTaskLine = new \stdClass();
$bodyTaskLine->IdAppProject = $IdAppProject;
$bodyTaskLine->Inputs[] = $image;
echo "<b>Create TaskLine:</b><br>";
$answerTaskLine = CallAPI("POST", "https://api.wirk.io/v1_0/TaskLine", $userApi, $passwordApi, json_encode($bodyTaskLine));
echo "Call: ".json_encode($bodyTaskLine)."<br>";
echo "Reponse: ".$answerTaskLine."<br><br>";
*/
$jsonTaskLine = json_decode($answerTaskLine);
$IdTaskLine = $jsonTaskLine->{'IdTaskLine'};
// Get answer
echo "<b>Get TaskLine:</b><br>";
$answerTaskLineAnswer = CallAPI("GET", "https://api.wirk.io/v1_0/TaskLine/".$IdTaskLine, $userApi, $passwordApi);
echo "Reponse: ".$answerTaskLineAnswer."<br><br>";
function CallAPI($method, $url, $username, $password, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
?>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment