api_light_hf / apis /rader.py
Renecto's picture
deploy api_light_hf (2026-03-12 12:47:03)
cf7f643
import matplotlib.pyplot as plt
import numpy as np
import json
from io import BytesIO
from PIL import Image
from matplotlib import font_manager
import os
script_path = os.path.abspath(__file__)
print("Script absolute path:", script_path)
def rader(data_json, max_val):
"""
input1 (text): [{ "key": "Clarity", "val": 4 }, { "key": "Value Proposition", "val": 4 }, { "key": "Relevance", "val": 4 }, { "key": "Distraction", "val": 4 }, { "key": "Urgency", "val": 2 }]
input2 (number): 5
output1 (image): レーダーチャーチE
"""
try:
data = json.loads(data_json)
labels = [item["key"] for item in data]
values = [item["val"] for item in data]
except (json.JSONDecodeError, KeyError):
return "Invalid JSON input. Ensure it is a list of dictionaries with 'key' and 'val'."
# 持E���E日本語フォントを登録し、グローバル設定に反映
font_path = "/home/user/app/NotoSansJP-VariableFont_wght.ttf"
font_manager.fontManager.addfont(font_path)
jp_font = font_manager.FontProperties(fname=font_path)
plt.rcParams['font.family'] = jp_font.get_name()
# 忁E��なチE�Eタを計箁E
num_vars = len(labels)
values += values[:1]
angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False).tolist() + [0]
# レーダーチャートを描画
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))
ax.fill(angles, values, color="blue", alpha=0.45)
ax.plot(angles, values, color="blue", linewidth=2)
ax.set_xticks(angles[:-1])
# set_xticklabelsで日本語フォントを明示皁E��持E��E
ax.set_xticklabels(labels, fontproperties=jp_font, fontsize=12)
ax.set_ylim(0, int(max_val))
# チャートを画像に変換
buf = BytesIO()
plt.savefig(buf, format="png", bbox_inches="tight", dpi=100)
plt.close(fig)
buf.seek(0)
img = Image.open(buf)
return img