When pressing the "uidown"-key you're setting the velocity to (-speed, 0). If you then press "uiup" it will calculate the angle of this velocity-vector and rotate accordingly by 180°. In addition to that it will also set the velocity-vector, but not to (speed, 0), as you also rotate that vector by 180°. So the new velocity is identical to the old velocity of (-speed, 0). Meaning the player will rotate by 180° but still continue it's movement.
This should work:
if Input.is_action_pressed("ui_down"):
rotation = velocity.angle()
velocity = Vector2(-speed, 0)
moving = true
elif Input.is_action_pressed("ui_up"):
rotation = velocity.angle()
velocity = Vector2(speed, 0)
moving = true