def process_image(self, image_path):
        is_valid_url = validators.url(image_path)
        if is_valid_url:
            request = service_pb2.PostModelOutputsRequest(
                # This is the model ID of a publicly available General model. You may use any other public or custom model ID.
                model_id=self.model_id,
                inputs=[
                resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(url=image_path)))
                ])
        elif os.path.isfile(image_path):
            with open(image_path, "rb") as f:
                file_bytes = f.read()
            request = service_pb2.PostModelOutputsRequest(
                # This is the model ID of a publicly available General model. You may use any other public or custom model ID.
                model_id=self.model_id,
                inputs=[
                resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(base64=file_bytes)))
                ])
        else:
            raise ValueError('image_path: {} does not exist'.format(image_path))

        response = stub.PostModelOutputs(request, metadata=self.metadata)

        if response.status.code != status_code_pb2.SUCCESS:
            raise Exception("Request failed, status code: " + str(response.status.code))

        else:
            return response
def request_call_integration(user_url, user_lan):
    request = service_pb2.PostModelOutputsRequest(
        model_id='aaa03c23b3724a16a56b629203edc62c',
        inputs=[
        resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(url=user_url)))
        ],
        model=resources_pb2.Model(
            output_info=resources_pb2.OutputInfo(
                output_config=resources_pb2.OutputConfig(
                    language=user_lan  
                )
            )
        ))
    response = stub.PostModelOutputs(request, metadata=metadata)




    if response.status.code != status_code_pb2.SUCCESS:
        raise Exception("Request failed, status code: " + str(response.status.code))

    request_data=[]
    for concept in response.outputs[0].data.concepts:
        request_data.append(concept.name)
    return request_data
Exemple #3
0
def getRecipe():
    ingredients = []
    # Handle image file
    if 'image' not in request.files:
        print("did not recive a file")
        pass  # TODO: handle error
    file = request.files['image']
    if file.filename == '':
        pass  # TODO: handle 'No selected file'
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

        # A call to clarifai
        imageURL = UPLOAD_FOLDER + '/' + filename
        with open(imageURL, "rb") as f:
            file_bytes = f.read()

        print(imageURL)
        APIrequest = service_pb2.PostModelOutputsRequest(
            model_id='bd367be194cf45149e75f01d59f77ba7',
            inputs=[
                resources_pb2.Input(data=resources_pb2.Data(
                    image=resources_pb2.Image(base64=file_bytes)))
            ])
        response = stub.PostModelOutputs(APIrequest, metadata=metadata)

        if response.status.code != status_code_pb2.SUCCESS:
            raise Exception("Request failed, status code: " +
                            str(response.status.code))

        for ingredient in response.outputs[0].data.concepts:
            print('%12s: %.2f' % (ingredient.name, ingredient.value))
            ingredients.append(ingredient.name)

        # TODO: add delete file after use
    else:
        pass  # TODO: handle error

    ingredients.append(request.form['ingredients'])

    # Get recipe from spoonacular
    if ingredients:
        payload = {
            'fillIngredients': False,
            'ingredients': ingredients,
            'limitLicense': False,
            'number': 5,
            'ranking': 1
        }
        endpoint = 'https://api.spoonacular.com/recipes/findByIngredients?apiKey=' + SPOONACULAR_KEY

        r = requests.get(endpoint, params=payload)
        results = r.json()
        title = results[0]['title']
        print(title)
        return jsonify(recipes=results, ingredients=ingredients)

    print('error lol')
    return jsonify(error="No ingredients were supplied!")
def test_predict_image_url_with_selected_concepts(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                image=resources_pb2.Image(url=DOG_IMAGE_URL, ), ), )
        ],
        model=resources_pb2.Model(output_info=resources_pb2.OutputInfo(
            output_config=resources_pb2.OutputConfig(select_concepts=[
                resources_pb2.Concept(name="dog"),
                resources_pb2.Concept(name="cat"),
            ]))),
    )
    response = post_model_outputs_and_maybe_allow_retries(stub,
                                                          request,
                                                          metadata=metadata())
    raise_on_failure(response)

    concepts = response.outputs[0].data.concepts
    assert len(concepts) == 2
    dog_concept = [c for c in concepts if c.name == "dog"][0]
    cat_concept = [c for c in concepts if c.name == "cat"][0]
    assert dog_concept.value > cat_concept.value
def classification(img):
    #with open(img, "rb") as f:
    file_bytes = img.read()

    post_model_outputs_response = stub.PostModelOutputs(
        service_pb2.PostModelOutputsRequest(
            model_id="bd367be194cf45149e75f01d59f77ba7",
            inputs=[
                resources_pb2.Input(data=resources_pb2.Data(
                    image=resources_pb2.Image(base64=file_bytes)))
            ]),
        metadata=metadata)

    if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
        raise Exception("Post model outputs failed, status: " +
                        post_model_outputs_response.status.description)

    output = post_model_outputs_response.outputs[0]

    consolidate = []
    print("Predicted concepts:")
    for concept in output.data.concepts:
        #print("%s %.2f" % (concept.name, concept.value))
        consolidate.append((concept.name, concept.value))

    return consolidate


#solucion = classification()
#print(solucion)
Exemple #6
0
def test_predict_video_url_with_custom_sample_ms(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                video=resources_pb2.Video(url=BEER_VIDEO_URL)))
        ],
        model=resources_pb2.Model(output_info=resources_pb2.OutputInfo(
            output_config=resources_pb2.OutputConfig(sample_ms=2000))),
    )
    response = post_model_outputs_and_maybe_allow_retries(stub,
                                                          request,
                                                          metadata=metadata())
    raise_on_failure(response)

    # The expected time per frame is the middle between the start and the end of the frame
    # (in milliseconds).
    expected_time = 1000

    assert len(response.outputs[0].data.frames) > 0
    for frame in response.outputs[0].data.frames:
        assert frame.frame_info.time == expected_time
        expected_time += 2000
    def get_tags(self, image_url):
        stub = service_pb2_grpc.V2Stub(self.channel)

        request = service_pb2.PostModelOutputsRequest(
            model_id='aaa03c23b3724a16a56b629203edc62c',
            inputs=[
                resources_pb2.Input(data=resources_pb2.Data(
                    image=resources_pb2.Image(url=image_url)))
            ])

        metadata = (('authorization', 'Key {0}'.format(self.key)), )

        response = stub.PostModelOutputs(request, metadata=metadata)

        if response.status.code != status_code_pb2.SUCCESS:
            raise Exception("Request failed, status code: " +
                            str(response.status.code))

        tags = []
        for concept in response.outputs[0].data.concepts:
            tags.append(str(concept.name))
            print(concept.name)

        str1 = ','.join(str(e) for e in tags)
        return str1
Exemple #8
0
 def get_concepts(self, url):
     request = service_pb2.PostModelOutputsRequest(
         model_id='aaa03c23b3724a16a56b629203edc62c',
         inputs=[
             resources_pb2.Input(data=resources_pb2.Data(
                 image=resources_pb2.Image(url=url)))
         ])
     response = self.stub.PostModelOutputs(request, metadata=self.metadata)
     if response.status.code != status_code_pb2.SUCCESS:
         raise Exception("Request failed, status code: " + str(response.status.code))
     return [concept.name for concept in response.outputs[0].data.concepts]
 def txt_mod_embed(self, caption):
     response = self.stub.PostModelOutputs(
         service_pb2.PostModelOutputsRequest(
             model_id="39f2950a32173f61b3eb40ede0d254e1",
             inputs=[
                 resources_pb2.Input(data=resources_pb2.Data(
                     text=resources_pb2.Text(raw=caption)))
             ]),
         metadata=(('authorization', self.app_id), ))
     if response.status.code != status_code_pb2.SUCCESS:
         raise Exception("Request failed, status code: " +
                         str(response.status.code))
     return response.outputs[0].data.embeddings[0].vector
 def txt_embed(self, caption):
     response = self.stub.PostModelOutputs(
         service_pb2.PostModelOutputsRequest(
             model_id="568d48e82924a00d0f98a6d34fa426cf",
             inputs=[
                 resources_pb2.Input(data=resources_pb2.Data(
                     text=resources_pb2.Text(raw=caption)))
             ]),
         metadata=(('authorization', self.app_id), ))
     if response.status.code != status_code_pb2.SUCCESS:
         raise Exception("Request failed, status code: " +
                         str(response.status.code))
     return response.outputs[0].data.embeddings[0].vector
 def img_mod_embed(self, url):
     request = service_pb2.PostModelOutputsRequest(
         model_id='d16f390eb32cad478c7ae150069bd2c6',
         inputs=[
             resources_pb2.Input(data=resources_pb2.Data(
                 image=resources_pb2.Image(url=url)))
         ])
     metadata = (('authorization', self.app_id), )
     response = self.stub.PostModelOutputs(request, metadata=metadata)
     if response.status.code != status_code_pb2.SUCCESS:
         raise Exception("Request failed, status code: " +
                         str(response.status.code))
     return [x.value for x in response.outputs[0].data.concepts]
Exemple #12
0
def test_predict_image_url(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                image=resources_pb2.Image(url=DOG_IMAGE_URL)))
        ],
    )
    response = stub.PostModelOutputs(request, metadata=metadata())
    raise_on_failure(response)

    assert len(response.outputs[0].data.concepts) > 0
    def img_embed(self, url):
        request = service_pb2.PostModelOutputsRequest(
            model_id='bbb5f41425b8468d9b7a554ff10f8581',
            inputs=[
                resources_pb2.Input(data=resources_pb2.Data(
                    image=resources_pb2.Image(url=url)))
            ])
        metadata = (('authorization', self.app_id), )
        response = self.stub.PostModelOutputs(request, metadata=metadata)
        if response.status.code != status_code_pb2.SUCCESS:
            raise Exception("Request failed, status code: " +
                            str(response.status.code))

        return response.outputs[0].data.embeddings[0].vector
def get_tags_from_url(image_url):
    tags = []
    request = service_pb2.PostModelOutputsRequest(
    model_id='aaa03c23b3724a16a56b629203edc62c',
    inputs=[
      resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(url=image_url)))
    ])
    response = stub.PostModelOutputs(request, metadata=metadata)

    if response.status.code != status_code_pb2.SUCCESS:
        raise Exception("Request failed, status code: " + str(response.status.code))

    for concept in response.outputs[0].data.concepts:
        tags.append(concept.name)
    return tags
Exemple #15
0
def test_failed_predict(channel):
    stub = service_pb2_grpc.V2Stub(channel)
    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                image=resources_pb2.Image(url=NON_EXISTING_IMAGE_URL)))
        ],
    )
    response = stub.PostModelOutputs(request, metadata=metadata())

    assert response.status.code == status_code_pb2.FAILURE
    assert response.status.description == "Failure"

    assert response.outputs[
        0].status.code == status_code_pb2.INPUT_DOWNLOAD_FAILED
 def img_txt_embed(self, url, caption):
     request = service_pb2.PostModelOutputsRequest(
         model_id='aaa03c23b3724a16a56b629203edc62c',
         inputs=[
             resources_pb2.Input(data=resources_pb2.Data(
                 image=resources_pb2.Image(url=url)))
         ])
     metadata = (('authorization', self.app_id), )
     response = self.stub.PostModelOutputs(request, metadata=metadata)
     if response.status.code != status_code_pb2.SUCCESS:
         raise Exception("Request failed, status code: " +
                         str(response.status.code))
     img_cons = ' '.join(
         [x.name for x in response.outputs[0].data.concepts])
     return self.txt_embed(caption + '. ' +
                           ' '.join(img_cons.split(' ')[:10]))
Exemple #17
0
def test_predict_image_url_with_max_concepts(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                image=resources_pb2.Image(url=DOG_IMAGE_URL, ), ), )
        ],
        model=resources_pb2.Model(output_info=resources_pb2.OutputInfo(
            output_config=resources_pb2.OutputConfig(max_concepts=3))),
    )
    response = stub.PostModelOutputs(request, metadata=metadata())
    raise_on_failure(response)

    assert len(response.outputs[0].data.concepts) == 3
def test_predict_video_url(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                video=resources_pb2.Video(url=CONAN_GIF_VIDEO_URL)))
        ],
    )
    response = stub.PostModelOutputs(request, metadata=metadata())
    raise_on_failure(response)

    assert len(response.outputs[0].data.frames) > 0
    for frame in response.outputs[0].data.frames:
        assert len(frame.data.concepts) > 0
Exemple #19
0
def test_image_predict_on_public_models(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    for title, model_id in MODEL_TITLE_AND_ID_PAIRS:
        request = service_pb2.PostModelOutputsRequest(
            model_id=model_id,
            inputs=[
                resources_pb2.Input(data=resources_pb2.Data(
                    image=resources_pb2.Image(url=DOG_IMAGE_URL)))
            ],
        )
        response = stub.PostModelOutputs(request, metadata=metadata())
        raise_on_failure(
            response,
            custom_message=
            f"Image predict failed for the {title} model (ID: {model_id}).",
        )
def get_tags_from_path(image_path):
    print("image path => ",image_path)
    with open(image_path,"rb") as f:
        file_bytes = f.read()
    tags = []
    request = service_pb2.PostModelOutputsRequest(
    model_id='aaa03c23b3724a16a56b629203edc62c',
    inputs=[
      resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(base64=file_bytes)))
    ])
    response = stub.PostModelOutputs(request, metadata=metadata)

    if response.status.code != status_code_pb2.SUCCESS:
        raise Exception("Request failed, status code: " + str(response.status.code))

    for concept in response.outputs[0].data.concepts:
        tags.append(concept.name)
    return tags
Exemple #21
0
def test_mixed_success_predict(channel):
    stub = service_pb2_grpc.V2Stub(channel)
    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                image=resources_pb2.Image(url=DOG_IMAGE_URL))),
            resources_pb2.Input(data=resources_pb2.Data(
                image=resources_pb2.Image(url=NON_EXISTING_IMAGE_URL))),
        ],
    )
    response = stub.PostModelOutputs(request, metadata=metadata())

    assert response.status.code == status_code_pb2.MIXED_STATUS

    assert response.outputs[0].status.code == status_code_pb2.SUCCESS
    assert response.outputs[
        1].status.code == status_code_pb2.INPUT_DOWNLOAD_FAILED
Exemple #22
0
def prepare_keywords(image):
    metadata = (('authorization', 'Key a5288575bd8b453285d62995dd09cb9a'),)
    request = service_pb2.PostModelOutputsRequest(
    # This is the model ID of a publicly available General model. You may use any other public or custom model ID.
    model_id='aaa03c23b3724a16a56b629203edc62c',
    inputs=[
      resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(url=image)))
    ])
    response = stub.PostModelOutputs(request, metadata=metadata)

    response = model_clarifai.predict_by_url(image)
    keywords = []
    for dict_item in response.outputs[0].data.concepts:
        keywords.append(dict_item.name)
    str1 = " ".join(keywords)
    text1 = nltk.word_tokenize(str1)
    tags = nltk.pos_tag(text1)
    return tags
Exemple #23
0
def test_predict_image_bytes(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    with open(RED_TRUCK_IMAGE_FILE_PATH, "rb") as f:
        file_bytes = f.read()

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                image=resources_pb2.Image(base64=file_bytes)))
        ],
    )
    response = stub.PostModelOutputs(request, metadata=metadata())

    raise_on_failure(response)

    assert len(response.outputs[0].data.concepts) > 0
def test_predict_video_bytes(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    with open(TOY_VIDEO_FILE_PATH, "rb") as f:
        file_bytes = f.read()

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                video=resources_pb2.Video(base64=file_bytes)))
        ],
    )
    response = stub.PostModelOutputs(request, metadata=metadata())
    raise_on_failure(response)

    assert len(response.outputs[0].data.frames) > 0
    for frame in response.outputs[0].data.frames:
        assert len(frame.data.concepts) > 0
Exemple #25
0
    def get_food(self):
        metadata = (('authorization',
                     'Key ca6dff40c60c49f69cdafd0a3ea2b5e5'), )

        request = service_pb2.PostModelOutputsRequest(
            # This is the model ID of a publicly available General model. You may use any other public or custom model ID.
            # public id: aaa03c23b3724a16a56b629203edc62c
            model_id='bd367be194cf45149e75f01d59f77ba7',
            inputs=[
                resources_pb2.Input(data=resources_pb2.Data(
                    image=resources_pb2.Image(url=self.link)))
            ])
        response = stub.PostModelOutputs(request, metadata=metadata)

        if response.status.code != status_code_pb2.SUCCESS:
            raise Exception("Request failed, status code: " +
                            str(response.status.code))

        self.food_list = response.outputs[0].data.concepts
        return response.outputs[0].data.concepts  # may need to return
Exemple #26
0
def test_predict_video_url_with_max_concepts(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                video=resources_pb2.Video(url=CONAN_GIF_VIDEO_URL)))
        ],
        model=resources_pb2.Model(output_info=resources_pb2.OutputInfo(
            output_config=resources_pb2.OutputConfig(max_concepts=3))),
    )
    response = post_model_outputs_and_maybe_allow_retries(stub,
                                                          request,
                                                          metadata=metadata())
    raise_on_failure(response)

    assert len(response.outputs[0].data.frames) > 0
    for frame in response.outputs[0].data.frames:
        assert len(frame.data.concepts) == 3
Exemple #27
0
    def is_nsfw(self, img):
        request = service_pb2.PostModelOutputsRequest(
            model_id=self.model_id,
            inputs=[
                resources_pb2.Input(data=resources_pb2.Data(
                    image=resources_pb2.Image(base64=img)))
            ])
        response = self.stub.PostModelOutputs(request, metadata=self.auth)

        if response.status.code != status_code_pb2.SUCCESS:
            raise Exception("Request failed, status code: " +
                            str(response.status.code))

        total_nsfw_rating = 0.
        for concept in response.outputs[0].data.concepts:
            if concept.name in ('explicit', 'suggestive'):
                total_nsfw_rating += concept.value
        if total_nsfw_rating > 0.60:
            return True
        return False
Exemple #28
0
def has_object_on_image(file_name, object_name):
    channel = ClarifaiChannel.get_grpc_channel()
    app = service_pb2_grpc.V2Stub(channel)
    metadata = (('authorization', f'Key {settings.CLARIFAI_API_KEY}'),)

    with open(file_name, 'rb') as f:
        file_data = f.read()
        image = resources_pb2.Image(base64=file_data)

    request = service_pb2.PostModelOutputsRequest(
        model_id='aaa03c23b3724a16a56b629203edc62c',
        inputs=[
            resources_pb2.Input(
                data=resources_pb2.Data(image=image)
            )
        ])

    response = app.PostModelOutputs(request, metadata=metadata)
    # print(response)
    return check_response_for_object(response, object_name)
def test_predict_image_url_with_min_value(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                image=resources_pb2.Image(url=DOG_IMAGE_URL, ), ), )
        ],
        model=resources_pb2.Model(output_info=resources_pb2.OutputInfo(
            output_config=resources_pb2.OutputConfig(min_value=0.98))),
    )
    response = post_model_outputs_and_maybe_allow_retries(stub,
                                                          request,
                                                          metadata=metadata())
    raise_on_failure(response)

    assert len(response.outputs[0].data.concepts) > 0
    for c in response.outputs[0].data.concepts:
        assert c.value >= 0.98
def test_predict_video_url_with_min_value(channel):
    stub = service_pb2_grpc.V2Stub(channel)

    request = service_pb2.PostModelOutputsRequest(
        model_id=GENERAL_MODEL_ID,
        inputs=[
            resources_pb2.Input(data=resources_pb2.Data(
                video=resources_pb2.Video(url=CONAN_GIF_VIDEO_URL)))
        ],
        model=resources_pb2.Model(output_info=resources_pb2.OutputInfo(
            output_config=resources_pb2.OutputConfig(min_value=0.95))),
    )
    response = stub.PostModelOutputs(request, metadata=metadata())
    raise_on_failure(response)

    assert len(response.outputs[0].data.frames) > 0
    for frame in response.outputs[0].data.frames:
        assert len(frame.data.concepts) > 0
        for concept in frame.data.concepts:
            assert concept.value >= 0.95