Download utils/styles.py from NAko3/nako_text: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/spaces/NAko3/nako_text/resolve/main/utils/styles.py
- Command line
-
hf download hf://spaces/NAko3/nako_text/utils/styles.py
-
curl -L -o styles.py https://huggingface.co/spaces/NAko3/nako_text/resolve/main/utils/styles.py
1.77 kB
| from typing import List, Dict, Any | |
| def parse_styles_response(response: Dict[str, Any]) -> List[Dict[str, str]]: | |
| """ | |
| API応答からスタイルリストを抽出する | |
| Args: | |
| response: API応答のJSONオブジェクト | |
| Returns: | |
| スタイルのリスト(辞書形式) | |
| """ | |
| if not response: | |
| return [] | |
| try: | |
| styles = response.get("styles", []) | |
| # 各スタイルが必要なフィールドを持っているか確認 | |
| validated_styles = [] | |
| for style in styles: | |
| if isinstance(style, dict) and "name" in style and "text" in style: | |
| validated_styles.append({ | |
| "name": style["name"], | |
| "text": style["text"] | |
| }) | |
| return validated_styles | |
| except Exception as e: | |
| print(f"スタイル解析エラー: {e}") | |
| return [] | |
| def chunk_styles(styles: List[Dict[str, str]], chunks: int = 3) -> List[List[Dict[str, str]]]: | |
| """ | |
| スタイルリストを指定された数のチャンクに分割する | |
| Streamlitのカラム表示に使用 | |
| Args: | |
| styles: スタイルのリスト | |
| chunks: 分割するチャンクの数 | |
| Returns: | |
| チャンクのリスト | |
| """ | |
| # 入力チェック | |
| if not styles: | |
| return [] | |
| if chunks <= 0: | |
| chunks = 1 | |
| # スタイルの総数 | |
| total_styles = len(styles) | |
| # スタイルを行ごとに分割(各行に3つずつ) | |
| result = [] | |
| for i in range(0, total_styles, chunks): | |
| chunk = styles[i:i+chunks] | |
| if chunk: # 空でないチャンクのみ追加 | |
| result.append(chunk) | |
| return result |