Let H be a graph allowing loops as well as vertex and edge weights. We prove that, for every triangle-free graph G without isolated vertices, the weighted number of graph homomorphisms \(\hom (G, H)\) satisfies the inequality $$\begin{aligned} \hom (G, H ) \le \prod _{uv \in E(G)} \hom (K_{d_u,d_v}, H )^{1/(d_ud_v)}, \end{aligned}$$where \(d_u\) denotes the degree of vertex u in G. In particular, one has $$\begin{aligned} \hom (G, H )^{1/|E(G)|} \le \hom (K_{d,d}, H )^{1/d^2} \end{aligned}$$for every d-regular triangle-free G. The triangle-free hypothesis on G is best possible. More generally, we prove a graphical Brascamp–Lieb type inequality, where every edge of G is assigned some two-variable function. These inequalities imply tight upper bounds on the partition function of various statistical models such as the Ising and Potts models, which includes independent sets and graph colorings. For graph colorings, corresponding to \(H = K_q\), we show that the triangle-free hypothesis on G may be dropped; this is also valid if some of the vertices of \(K_q\) are looped. A corollary is that among d-regular graphs, \(G = K_{d,d}\) maximizes the quantity \(c_q(G)^{1/|V(G)|}\) for every q and d, where \(c_q(G)\) counts proper q-colorings of G.Finally, we show that if the edge-weight matrix of H is positive semidefinite, then $$\begin{aligned} \hom (G, H) \le \prod _{v \in V(G)} \hom (K_{d_v+1}, H )^{1/(d_v+1)}. \end{aligned}$$This implies that among d-regular graphs, \(G = K_{d+1}\) maximizes \(\hom (G, H)^{1/|V(G)|}\). For 2-spin Ising models, our results give a complete characterization of extremal graphs: complete bipartite graphs maximize the partition function of 2-spin antiferromagnetic models and cliques maximize the partition function of ferromagnetic models. These results settle a number of conjectures by Galvin–Tetali, Galvin, and Cohen–Csikvári–Perkins–Tetali, and provide an alternate proof to a conjecture by Kahn.