RafaelFonseca
Add Django Django_webapp project to HF space
e50f22e
Raw History Blame Contribute Delete
617 Bytes
# webapp/services/charts.py
import io, base64
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
def preds_hist_data_url(preds) -> str:
buf = io.BytesIO()
plt.figure(figsize=(6,4))
try:
plt.hist(preds, bins=3)
except Exception:
vals, counts = np.unique(preds, return_counts=True)
plt.bar([str(v) for v in vals], counts)
plt.title('Predictions')
plt.tight_layout()
plt.savefig(buf, format='png')
plt.close()
buf.seek(0)
b64 = base64.b64encode(buf.read()).decode('utf-8')
return f'data:image/png;base64,{b64}'