def test_post_delete_batch_images(channel): stub = service_pb2_grpc.V2Stub(channel) post_response = stub.PostInputs( service_pb2.PostInputsRequest( inputs=[ resources_pb2.Input( data=resources_pb2.Data( image=resources_pb2.Image(url=TRUCK_IMAGE_URL, allow_duplicate_url=True) ) ), resources_pb2.Input( data=resources_pb2.Data( image=resources_pb2.Image(url=TRUCK_IMAGE_URL, allow_duplicate_url=True) ) ), ] ), metadata=metadata(), ) raise_on_failure(post_response) input_id1 = post_response.inputs[0].id input_id2 = post_response.inputs[1].id wait_for_inputs_upload(stub, metadata(), [input_id1, input_id2]) delete_response = stub.DeleteInputs( service_pb2.DeleteInputsRequest(ids=[input_id1, input_id2]), metadata=metadata() ) raise_on_failure(delete_response)
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 test_search_by_image_url_and_geo_box(channel): stub = service_pb2_grpc.V2Stub(channel) with SetupImage(stub) as input_: response = stub.PostSearches( service_pb2.PostSearchesRequest( query=resources_pb2.Query(ands=[ resources_pb2.And(output=resources_pb2.Output( input=resources_pb2.Input(data=resources_pb2.Data( image=resources_pb2.Image(url=DOG_IMAGE_URL))))), resources_pb2.And(input=resources_pb2.Input( data=resources_pb2.Data(geo=resources_pb2.Geo(geo_box=[ resources_pb2.GeoBoxedPoint( geo_point=resources_pb2.GeoPoint(longitude=43, latitude=54)), resources_pb2.GeoBoxedPoint( geo_point=resources_pb2.GeoPoint(longitude=45, latitude=56)), ])))), ]), pagination=service_pb2.Pagination(page=1, per_page=1000), ), metadata=metadata(), ) raise_on_failure(response) assert len(response.hits) > 0 assert input_.id in [hit.input.id for hit in response.hits]
def test_search_by_geo_box_and_annotated_name_and_predicted_name(channel): stub = service_pb2_grpc.V2Stub(channel) with SetupImage(stub) as input_: my_concept_name = input_.data.concepts[0].name response = stub.PostSearches( service_pb2.PostSearchesRequest( query=resources_pb2.Query(ands=[ resources_pb2.And(input=resources_pb2.Input( data=resources_pb2.Data(geo=resources_pb2.Geo(geo_box=[ resources_pb2.GeoBoxedPoint( geo_point=resources_pb2.GeoPoint(longitude=43, latitude=54)), resources_pb2.GeoBoxedPoint( geo_point=resources_pb2.GeoPoint(longitude=45, latitude=56)), ])))), resources_pb2.And(input=resources_pb2.Input( data=resources_pb2.Data(concepts=[ resources_pb2.Concept(name=my_concept_name, value=1) ]))), resources_pb2.And(output=resources_pb2.Output( data=resources_pb2.Data(concepts=[ resources_pb2.Concept(name="dog", value=1) ]))), ]), pagination=service_pb2.Pagination(page=1, per_page=1000), ), metadata=metadata(), ) raise_on_failure(response) assert len(response.hits) > 0 assert input_.id in [hit.input.id for hit in response.hits]
def test_post_list_patch_get_delete_image(channel): stub = service_pb2_grpc.V2Stub(channel) post_response = stub.PostInputs( service_pb2.PostInputsRequest( inputs=[ resources_pb2.Input( data=resources_pb2.Data( image=resources_pb2.Image(url=TRUCK_IMAGE_URL, allow_duplicate_url=True), concepts=[resources_pb2.Concept(id="some-concept")], ) ) ] ), metadata=metadata(), ) raise_on_failure(post_response) input_id = post_response.inputs[0].id try: wait_for_inputs_upload(stub, metadata(), [input_id]) list_response = stub.ListInputs( service_pb2.ListInputsRequest(per_page=1), metadata=metadata() ) raise_on_failure(list_response) assert len(list_response.inputs) == 1 # Most likely we don"t have that many inputs, so this should return 0. list_response2 = stub.ListInputs( service_pb2.ListInputsRequest(per_page=500, page=1000), metadata=metadata() ) raise_on_failure(list_response2) assert len(list_response2.inputs) == 0 patch_response = stub.PatchInputs( service_pb2.PatchInputsRequest( action="overwrite", inputs=[ resources_pb2.Input( id=input_id, data=resources_pb2.Data( concepts=[resources_pb2.Concept(id="some-new-concept")] ), ) ], ), metadata=metadata(), ) raise_on_failure(patch_response) get_response = stub.GetInput( service_pb2.GetInputRequest(input_id=input_id), metadata=metadata() ) raise_on_failure(get_response) assert get_response.input.data.concepts[0].name == "some-new-concept" finally: delete_request = service_pb2.DeleteInputRequest(input_id=input_id) delete_response = stub.DeleteInput(delete_request, metadata=metadata()) raise_on_failure(delete_response)
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
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 test_image_with_bytes(channel): stub = service_pb2_grpc.V2Stub(channel) with open(RED_TRUCK_IMAGE_FILE_PATH, "rb") as f: file_bytes = f.read() post_response = stub.PostInputs( service_pb2.PostInputsRequest( inputs=[ resources_pb2.Input( data=resources_pb2.Data(image=resources_pb2.Image(base64=file_bytes)) ) ] ), metadata=metadata(), ) raise_on_failure(post_response) input_id = post_response.inputs[0].id wait_for_inputs_upload(stub, metadata(), [input_id]) delete_response = stub.DeleteInputs( service_pb2.DeleteInputsRequest(ids=[input_id]), metadata=metadata() ) raise_on_failure(delete_response)
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 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)
def test_save_and_execute_search_by_id(channel): stub = service_pb2_grpc.V2Stub(channel) search_id = "my-search-id-" + uuid.uuid4().hex[:15] with SetupImage(stub) as input_: my_concept_id = input_.data.concepts[0].id # This saves the search under an ID, but does not execute it / return any results. save_search_response = stub.PostSearches( service_pb2.PostSearchesRequest(searches=[ resources_pb2.Search( id=search_id, save=True, query=resources_pb2.Query(ands=[ resources_pb2.And(input=resources_pb2.Input( data=resources_pb2.Data(concepts=[ resources_pb2.Concept(id=my_concept_id, value=1) ]))) ]), ) ]), metadata=metadata(), ) raise_on_failure(save_search_response) # Executing the search returns results. post_search_by_id_response = stub.PostSearchesByID( service_pb2.PostSearchesByIDRequest(id=search_id), metadata=metadata(), ) raise_on_failure(post_search_by_id_response) assert len(post_search_by_id_response.hits) == 1 assert post_search_by_id_response.hits[0].input.id == input_.id
def __enter__(self) -> resources_pb2.Input: my_concept_id = "my-concept-id-" + uuid.uuid4().hex my_concept_name = "my concept name " + uuid.uuid4().hex image_metadata = struct_pb2.Struct() image_metadata.update({ "some-key": "some-value", "another-key": { "inner-key": "inner-value" } }) post_response = self._stub.PostInputs( service_pb2.PostInputsRequest(inputs=[ resources_pb2.Input(data=resources_pb2.Data( image=resources_pb2.Image(url=DOG_IMAGE_URL, allow_duplicate_url=True), concepts=[ resources_pb2.Concept( id=my_concept_id, name=my_concept_name, value=1) ], metadata=image_metadata, geo=resources_pb2.Geo(geo_point=resources_pb2.GeoPoint( longitude=44, latitude=55)), ), ) ]), metadata=metadata(), ) raise_on_failure(post_response) self._input = post_response.inputs[0] wait_for_inputs_upload(self._stub, metadata(), [self._input.id]) return self._input
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
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
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 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]
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_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 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 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
def __enter__(self) -> resources_pb2.Input: post_response = self._stub.PostInputs( service_pb2.PostInputsRequest(inputs=[ resources_pb2.Input(data=resources_pb2.Data( image=resources_pb2.Image(url=TRAVEL_IMAGE_URL, allow_duplicate_url=True), ), ) ]), metadata=metadata(), ) raise_on_failure(post_response) self._input = post_response.inputs[0] wait_for_inputs_upload(self._stub, metadata(), [self._input.id]) return self._input
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
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 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 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]))
def test_search_by_annotated_concept_name(channel): stub = service_pb2_grpc.V2Stub(channel) with SetupImage(stub) as input_: my_concept_name = input_.data.concepts[0].name response = stub.PostSearches( service_pb2.PostSearchesRequest(query=resources_pb2.Query(ands=[ resources_pb2.And(input=resources_pb2.Input( data=resources_pb2.Data(concepts=[ resources_pb2.Concept(name=my_concept_name, value=1) ]))) ])), metadata=metadata(), ) raise_on_failure(response) assert len(response.hits) == 1 assert response.hits[0].input.id == input_.id
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 test_search_by_image_url(channel): stub = service_pb2_grpc.V2Stub(channel) with SetupImage(stub) as input_: response = stub.PostSearches( service_pb2.PostSearchesRequest( query=resources_pb2.Query(ands=[ resources_pb2.And(output=resources_pb2.Output( input=resources_pb2.Input(data=resources_pb2.Data( image=resources_pb2.Image(url=DOG_IMAGE_URL))))) ]), pagination=service_pb2.Pagination(page=1, per_page=1000), ), metadata=metadata(), ) raise_on_failure(response) assert len(response.hits) > 0 assert input_.id in [hit.input.id for hit in response.hits]