Exemplo n.º 1
0
def test_bulk_workflow_predict_with_url(
        mock_http_client):  # type: (mock.Mock) -> None
    mock_execute_request = mock_request(mock_http_client,
                                        json_response="""
{
  "status": {
    "code": 10000,
    "description": "Ok"
  },
  "workflow": {
    "id": "@workflowID",
    "app_id": "@appID",
    "created_at": "2017-06-15T15:17:30.462323Z"
  },
  "results": [
    {
      "status": {
        "code": 10000,
        "description": "Ok"
      },
      "input": {
        "id": "@inputID1",
        "data": {
          "image": {
            "url": "@url1"
          }
        }
      },
      "outputs": [
        {
          "id": "@outputID11",
          "status": {
            "code": 10000,
            "description": "Ok"
          },
          "created_at": "2019-01-29T17:36:23.736685542Z",
          "model": {
            "id": "@modelID1",
            "name": "food-items-v1.0",
            "created_at": "2016-09-17T22:18:59.955626Z",
            "app_id": "main",
            "output_info": {
              "message": "Show output_info with: GET /models/{model_id}/output_info",
              "type": "concept",
              "type_ext": "concept"
            },
            "model_version": {
              "id": "@modelVersionID1",
              "created_at": "2016-09-17T22:18:59.955626Z",
              "status": {
                "code": 21100,
                "description": "Model trained successfully"
              },
              "train_stats": {}
            },
            "display_name": "Food"
          },
          "data": {}
        },
        {
          "id": "@outputID12",
          "status": {
            "code": 10000,
            "description": "Ok"
          },
          "created_at": "2019-01-29T17:36:23.736712374Z",
          "model": {
            "id": "@modelID2",
            "name": "general",
            "created_at": "2016-03-09T17:11:39.608845Z",
            "app_id": "main",
            "output_info": {
              "message": "Show output_info with: GET /models/{model_id}/output_info",
              "type": "concept",
              "type_ext": "concept"
            },
            "model_version": {
              "id": "@modelVersion2",
              "created_at": "2016-07-13T01:19:12.147644Z",
              "status": {
                "code": 21100,
                "description": "Model trained successfully"
              },
              "train_stats": {}
            },
            "display_name": "General"
          },
          "data": {
            "concepts": [
              {
                "id": "@conceptID11",
                "name": "people",
                "value": 0.9963381,
                "app_id": "main"
              },
              {
                "id": "@conceptID12",
                "name": "one",
                "value": 0.9879056,
                "app_id": "main"
              },
              {
                "id": "@conceptID13",
                "name": "portrait",
                "value": 0.9849082,
                "app_id": "main"
              }
            ]
          }
        }
      ]
    },
    {
      "status": {
        "code": 10000,
        "description": "Ok"
      },
      "input": {
        "id": "@inputID2",
        "data": {
          "image": {
            "url": "@url2"
          }
        }
      },
      "outputs": [
        {
          "id": "@outputID21",
          "status": {
            "code": 10000,
            "description": "Ok"
          },
          "created_at": "2019-01-29T17:36:23.736685542Z",
          "model": {
            "id": "@modelID1",
            "name": "food-items-v1.0",
            "created_at": "2016-09-17T22:18:59.955626Z",
            "app_id": "main",
            "output_info": {
              "message": "Show output_info with: GET /models/{model_id}/output_info",
              "type": "concept",
              "type_ext": "concept"
            },
            "model_version": {
              "id": "@modelVersion1",
              "created_at": "2016-09-17T22:18:59.955626Z",
              "status": {
                "code": 21100,
                "description": "Model trained successfully"
              },
              "train_stats": {}
            },
            "display_name": "Food"
          },
          "data": {
            "concepts": [
              {
                "id": "@concept21",
                "name": "spatula",
                "value": 0.9805687,
                "app_id": "main"
              }
            ]
          }
        },
        {
          "id": "@outputID22",
          "status": {
            "code": 10000,
            "description": "Ok"
          },
          "created_at": "2019-01-29T17:36:23.736712374Z",
          "model": {
            "id": "@modelID2",
            "name": "general",
            "created_at": "2016-03-09T17:11:39.608845Z",
            "app_id": "main",
            "output_info": {
              "message": "Show output_info with: GET /models/{model_id}/output_info",
              "type": "concept",
              "type_ext": "concept"
            },
            "model_version": {
              "id": "@modelVersion2",
              "created_at": "2016-07-13T01:19:12.147644Z",
              "status": {
                "code": 21100,
                "description": "Model trained successfully"
              },
              "train_stats": {}
            },
            "display_name": "General"
          },
          "data": {
            "concepts": [
              {
                "id": "@conceptID31",
                "name": "eyewear",
                "value": 0.99984586,
                "app_id": "main"
              },
              {
                "id": "@conceptID32",
                "name": "lens",
                "value": 0.999823,
                "app_id": "main"
              },
              {
                "id": "@conceptID33",
                "name": "eyeglasses",
                "value": 0.99980056,
                "app_id": "main"
              }
            ]
          }
        }
      ]
    }
  ]
}
""")

    app = ClarifaiApp()

    workflow = Workflow(app.api, workflow_id='@workflowID')
    response = workflow.predict(
        [Image(url='@url1'), Image(url='@url2')],
        ModelOutputConfig(min_value=0.5, max_concepts=3))

    assert response['workflow']['id'] == '@workflowID'
    assert response['results'][0]['outputs'][1]['data']['concepts'][0][
        'id'] == '@conceptID11'

    assert_request(
        mock_execute_request, 'POST', '/v2/workflows/@workflowID/results', """
{
  "inputs": [
    {
      "data": {
        "image": {
          "url": "@url1"
        }
      }
    },
    {
      "data": {
        "image": {
          "url": "@url2"
        }
      }
    }
  ],
  "output_config": {
    "max_concepts": 3,
    "min_value": 0.5
  }
}
  """)
Exemplo n.º 2
0
import json
from clarifai.rest import ClarifaiApp
from clarifai.rest import Image as ClImage
from clarifai.rest import Workflow

app = ClarifaiApp(api_key='f721938da77f4cd8b508f8fe61265812')

workflow = Workflow(app.api, workflow_id="workflow-1")

# model = app.models.get('demographics')

# BY URL
image = ClImage(url='https://samples.clarifai.com/demographics.jpg')
response = workflow.predict([image])

# BY LOCAL FILE
# image = model.predict_by_filename('/home/user/image.jpeg')
# response = model.predict_by_filename('/home/user/image.jpeg')

print(json.dumps(response, sort_keys=True, indent=1))