Align viewport rotation to normal vector like Shift+7 in edit mode
I solved it. If the tangent picks the wrong direction and doesn't align to the viewport, the rotation matrix inverts and uses the negative value of the angle for correction. Keeping it here for prosperity and to help those seeking this kind of alignment behavior.
import bpy
import bmesh
from mathutils import *; from math import *

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
       rv3d = area.spaces[0].region_3d

obj = bpy.context.active_object
mat = obj.matrix_world
me = obj.data

bm = bmesh.from_edit_mesh(me)
f = bm.select_history.active

up = Vector((0,0,1))
up1 = Vector((0,-1,0))

center = f.calc_center_median()
normal = f.normal
tangent = f.calc_tangent_edge()

mw = mat.to_3x3()
quat = normal.rotation_difference(mw.inverted() @ up)
rot_mat = mw @ quat.to_matrix()

loc, rot, scale = rv3d.view_matrix.decompose()

mat_scale = Matrix()
for i in range(3):
    mat_scale[i][i] = scale[i]

rv3d.view_matrix = Matrix.Translation(loc) \
    @ rot_mat.to_4x4() \
    @ mat_scale
    
vrot = rv3d.view_rotation
rot_axis = vrot @ up
rot_dir = vrot @ up1

rotdiff = rot_dir.rotation_difference(tangent)
M = mw @ Matrix.Rotation(rotdiff.angle, 3, rot_axis)
rv3d.view_rotation.rotate(M)

if (rv3d.view_rotation @ up1).rotation_difference(tangent).angle != 0:
    rv3d.view_rotation.rotate(M.inverted())
    M = mw @ Matrix.Rotation(-rotdiff.angle, 3, rot_axis)
    rv3d.view_rotation.rotate(M)    

rv3d.view_location = obj.matrix_world @ center
bmesh.update_edit_mesh(me, True)