How to run php code only for a specific widget on a page and not all widgets on that page?

Seeing as it’s in a loop, you should be able to check against the type of post, and only add it to that type.

function add_rating_html( $title) {

    if ( is_front_page() && 'post' === get_post_type() ) {
        $out    = "Rating: 5";
        $title .= $out;
    }
    return $title;
}

add_filter( 'the_title', 'add_rating_html', 1 );

There’s probably a better way to do it by hooking on to the specific widget, but I’m no expert with how Elementor works