Download utils/api.py from NAko3/nako_text: direct link, hf CLI and curl.
- Browser
- Download file 18.2 kB
-
https://huggingface.co/spaces/NAko3/nako_text/resolve/main/utils/api.py
- Command line
-
hf download hf://spaces/NAko3/nako_text/utils/api.py
-
curl -L -o api.py https://huggingface.co/spaces/NAko3/nako_text/resolve/main/utils/api.py
18.2 kB
| import json | |
| import re | |
| from typing import Dict, Any, Optional | |
| from groq import Groq | |
| def generate_fancy_text( | |
| text: str, | |
| api_key: str, | |
| model: str = "meta-llama/llama-4-maverick-17b-128e-instruct", | |
| ) -> Optional[Dict[str, Any]]: | |
| """ | |
| Groq APIを使用して入力テキストを様々な装飾スタイルに変換する | |
| Args: | |
| text: 変換するテキスト | |
| api_key: Groq APIキー | |
| model: 使用するGroq APIのモデル名 | |
| Returns: | |
| 装飾スタイルの辞書。エラーが発生した場合はNone | |
| """ | |
| # システムプロンプトを改善 - 複雑なコンビニング文字も含める | |
| system_prompt = """あなたはテキスト装飾の専門家です。 | |
| 与えられたテキストを35種類以上の異なるスタイルに変換してください。 | |
| 以下のような多様な装飾スタイルを必ず含めてください: | |
| 1. Unicode変換スタイル: | |
| - 数学体(𝐟𝐨𝐥𝐥𝐨𝐰) | |
| - フラクトゥール(𝔣𝔬𝔩𝔩𝔬𝔴) | |
| - 太字フラクトゥール(𝖋𝖔𝖑𝖑𝖔𝖜) | |
| - スクリプト体(𝓯𝓸𝓵𝓵𝓸𝔀) | |
| - 太字(𝗳𝗼𝗹𝗹𝗼𝘄) | |
| - イタリック(𝘧𝘰𝘭𝘭𝘰𝘸) | |
| - 太字イタリック(𝙛𝙤𝙡𝙡𝙤𝙬) | |
| - サンセリフ(𝚏𝚘𝚕𝚕𝚘𝚠) | |
| - 小型大文字(ꜰᴏʟʟᴏᴡ) | |
| 2. 装飾スタイル: | |
| - 囲み文字(【f】【o】【l】【l】【o】【w】) | |
| - 波線付き(f̴o̴l̴l̴o̴w̴) | |
| - 取り消し線(f̶o̶l̶l̶o̶w̶) | |
| - 下線付き(f̲o̲l̲l̲o̲w̲) | |
| - 点付き(ḟöḷḷöẇ) | |
| 3. クリエイティブな変換: | |
| - 反転テキスト(ʍolloɟ) | |
| - ブロック文字(🅵🅾🅻🅻🅾🆆) | |
| - サークル文字(ⓕⓞⓛⓛⓞⓦ) | |
| - 装飾フレーム(✿ follow ✿) | |
| - 絵文字装飾(💖 follow 💖) | |
| - かわいい装飾(🎀 follow 🎀) | |
| 4. 文字間に記号を挿入するスタイルも含めてください(f♥o♥l♥l♥o♥w) | |
| 5. 高度なコンビニング文字スタイル: | |
| - 複雑な重ね合わせ(例:⨍ᨵׁׅᥣׁׅ֪ᥣׁׅ֪ᨵׁׅᨰׁׅ) | |
| - グリッチ風装飾(例:あ̸̰͕͈̹̃̇た̷̗̼͛̒̑͂̃̐̄し̷̡̻͖̣̲̰̼̲̎̈́̈́́̈͝͝を̶̲̯͓͖͍̜̻͒͗f̵̻̤̯͎̠͉̼̬̘͉͗́̍͋̅̋͛̀o̸̡̤̺̭̿̄͒̎̔̈́͆̐̇̂ͅl̸̡̪̭͗̓̄̀̑̉̓͑ḻ̷͙̞̤̰̗̍̅̿ơ̴̯̙̠̜̠̐̚͝w̴̢̻̰̱͉̣̍̈́͐̈́) | |
| - ブロック文字によるマーキング(例:あ⃞ た⃞ し⃞ を⃞ f⃞ o⃞ l⃞ l⃞ o⃞ w⃞) | |
| - サークル付き(例:あ⃝ た⃝ し⃝ を⃝) | |
| - 国旗文字装飾(例:🇫🇴🇱🇱🇴🇼) | |
| - 数字や文字エンクロージング(例:あ⃣ た⃣ し⃣ を⃣ f⃣ o⃣ l⃣ l⃣ o⃣ w⃣) | |
| - 二重打ち消し線、波線など複数のコンビニング文字の組み合わせ | |
| - 縦書きスタイル(例:あ︎た︎し︎を︎f︎o︎l︎l︎o︎w︎) | |
| 必ず日本語部分と英語/記号部分の両方を適切に装飾してください。 | |
| 日本語はそのままに、英語/記号部分(例:follow)だけを変換するスタイルも含めてください。 | |
| 出力は必ず以下のJSON形式で返してください: | |
| { | |
| "styles": [ | |
| {"name": "スタイル名1", "text": "装飾されたテキスト1"}, | |
| {"name": "スタイル名2", "text": "装飾されたテキスト2"}, | |
| ... | |
| ] | |
| } | |
| """ | |
| try: | |
| # Groq APIクライアントを初期化 | |
| client = Groq(api_key=api_key) | |
| # API呼び出し | |
| response = client.chat.completions.create( | |
| model=model, | |
| messages=[ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": f"次のテキストを装飾してください: {text}"} | |
| ], | |
| temperature=0.7, | |
| response_format={"type": "json_object"} | |
| ) | |
| # レスポンスからコンテンツを取得 | |
| content = response.choices[0].message.content | |
| # JSONをパース | |
| result = json.loads(content) | |
| return result | |
| except Exception as e: | |
| print(f"API通信エラー: {e}") | |
| # フォールバック: 事前定義したスタイルを返す | |
| return get_fallback_styles(text) | |
| def get_fallback_styles(text): | |
| """ | |
| API呼び出しが失敗した場合のフォールバックスタイルを提供 | |
| Args: | |
| text: 装飾するテキスト | |
| Returns: | |
| スタイルの辞書 | |
| """ | |
| # 英数字部分の抽出 | |
| alphanum_parts = re.findall(r'[a-zA-Z0-9]+', text) | |
| # 全角変換用の関数 | |
| def to_fullwidth(text): | |
| result = "" | |
| for char in text: | |
| if ord('!') <= ord(char) <= ord('~'): | |
| # ASCII文字を全角に変換 | |
| result += chr(ord(char) + 0xFEE0) | |
| else: | |
| result += char | |
| return result | |
| # テキストの数字→装飾文字変換関数 | |
| def to_unicode_style(text, start_code, is_alpha_only=True): | |
| result = "" | |
| for char in text: | |
| if 'a' <= char.lower() <= 'z': | |
| # アルファベットをUnicode装飾フォントに変換 | |
| offset = ord(char.lower()) - ord('a') | |
| result += chr(start_code + offset) | |
| elif '0' <= char <= '9' and not is_alpha_only: | |
| # 数字を変換(一部のスタイルのみ) | |
| offset = ord(char) - ord('0') | |
| result += chr(start_code + 26 + offset) | |
| else: | |
| # その他の文字はそのまま | |
| result += char | |
| return result | |
| # 数学フォント(𝐚-𝐳) | |
| def to_math_bold(text): | |
| return to_unicode_style(text, 0x1D41A) | |
| # フラクトゥール(𝔞-𝔷) | |
| def to_fraktur(text): | |
| return to_unicode_style(text, 0x1D51E) | |
| # 太字フラクトゥール(𝖆-𝖟) | |
| def to_bold_fraktur(text): | |
| return to_unicode_style(text, 0x1D586) | |
| # スクリプト(𝓪-𝔃) | |
| def to_script(text): | |
| return to_unicode_style(text, 0x1D4EA) | |
| # 太字(𝗮-𝘇) | |
| def to_bold(text): | |
| return to_unicode_style(text, 0x1D5EE) | |
| # イタリック(𝘢-𝘻) | |
| def to_italic(text): | |
| return to_unicode_style(text, 0x1D622) | |
| # 太字イタリック(𝙖-𝙯) | |
| def to_bold_italic(text): | |
| return to_unicode_style(text, 0x1D656) | |
| # サンセリフ(𝚊-𝚣) | |
| def to_sans(text): | |
| return to_unicode_style(text, 0x1D68A) | |
| # 小型大文字 | |
| def to_small_caps(text): | |
| result = "" | |
| for char in text: | |
| if 'a' <= char.lower() <= 'z': | |
| if char.lower() in 'abcdefghijklmnopqrstuvwxyz': | |
| small_caps_map = { | |
| 'a': 'ᴀ', 'b': 'ʙ', 'c': 'ᴄ', 'd': 'ᴅ', 'e': 'ᴇ', 'f': 'ꜰ', | |
| 'g': 'ɢ', 'h': 'ʜ', 'i': 'ɪ', 'j': 'ᴊ', 'k': 'ᴋ', 'l': 'ʟ', | |
| 'm': 'ᴍ', 'n': 'ɴ', 'o': 'ᴏ', 'p': 'ᴘ', 'q': 'ǫ', 'r': 'ʀ', | |
| 's': 's', 't': 'ᴛ', 'u': 'ᴜ', 'v': 'ᴠ', 'w': 'ᴡ', 'x': 'x', | |
| 'y': 'ʏ', 'z': 'ᴢ' | |
| } | |
| result += small_caps_map[char.lower()] | |
| else: | |
| result += char | |
| else: | |
| result += char | |
| return result | |
| # 元のテキストから英数字部分のみを装飾するヘルパー関数 | |
| def decorate_alphanum(text, decorator_func): | |
| if not alphanum_parts: | |
| return decorator_func(text) | |
| result = text | |
| for part in alphanum_parts: | |
| decorated = decorator_func(part) | |
| # 元のテキスト内の英数字部分を置換 | |
| result = result.replace(part, decorated, 1) | |
| return result | |
| # 様々な囲み文字 | |
| def surround_with(text, prefix, suffix=None): | |
| if suffix is None: | |
| suffix = prefix | |
| return f"{prefix}{text}{suffix}" | |
| def add_symbols_between(text, symbol): | |
| return symbol.join(text) | |
| # 様々な装飾文字生成 | |
| def decorate_with_emojis(text, prefix, suffix=None): | |
| if suffix is None: | |
| suffix = prefix | |
| return f"{prefix} {text} {suffix}" | |
| # テキストを反転する | |
| def reverse_text(text): | |
| # 文字反転マッピング | |
| flip_map = { | |
| 'a': 'ɐ', 'b': 'q', 'c': 'ɔ', 'd': 'p', 'e': 'ǝ', 'f': 'ɟ', 'g': 'ƃ', | |
| 'h': 'ɥ', 'i': 'ᴉ', 'j': 'ɾ', 'k': 'ʞ', 'l': 'l', 'm': 'ɯ', 'n': 'u', | |
| 'o': 'o', 'p': 'd', 'q': 'b', 'r': 'ɹ', 's': 's', 't': 'ʇ', 'u': 'n', | |
| 'v': 'ʌ', 'w': 'ʍ', 'x': 'x', 'y': 'ʎ', 'z': 'z' | |
| } | |
| result = "" | |
| for char in reversed(text): | |
| if char.lower() in flip_map: | |
| result += flip_map[char.lower()] | |
| else: | |
| result += char | |
| return result | |
| # グリッチ風テキスト生成 | |
| def create_glitch_text(text): | |
| # ランダムなコンビニング文字を追加 | |
| import random | |
| combining_marks = [ | |
| '\u0300', '\u0301', '\u0302', '\u0303', '\u0304', '\u0305', | |
| '\u0306', '\u0307', '\u0308', '\u0309', '\u030A', '\u030B', | |
| '\u030C', '\u030D', '\u030E', '\u030F', '\u0310', '\u0311', | |
| '\u0312', '\u0313', '\u0314', '\u0315', '\u0316', '\u0317', | |
| '\u0318', '\u0319', '\u031A', '\u031B', '\u031C', '\u031D', | |
| '\u031E', '\u031F', '\u0320', '\u0321', '\u0322', '\u0323', | |
| '\u0324', '\u0325', '\u0326', '\u0327', '\u0328', '\u0329', | |
| '\u032A', '\u032B', '\u032C', '\u032D', '\u032E', '\u032F' | |
| ] | |
| result = "" | |
| for char in text: | |
| result += char | |
| # 各文字に1-3個のコンビニング文字をランダムに追加 | |
| marks_count = random.randint(1, 3) | |
| for _ in range(marks_count): | |
| result += random.choice(combining_marks) | |
| return result | |
| # 強めのグリッチ効果 | |
| def create_heavy_glitch_text(text): | |
| import random | |
| # より多くのコンビニング文字と線を追加 | |
| combining_marks = [ | |
| '\u0300', '\u0301', '\u0302', '\u0303', '\u0304', '\u0305', | |
| '\u0306', '\u0307', '\u0308', '\u0309', '\u030A', '\u030B', | |
| '\u030C', '\u030D', '\u030E', '\u030F', '\u0310', '\u0311', | |
| '\u0312', '\u0313', '\u0314', '\u0315', '\u0316', '\u0317', | |
| '\u0334', '\u0335', '\u0336', '\u0337', '\u0338', '\u0339', | |
| '\u033A', '\u033B', '\u033C', '\u033D', '\u033E', '\u033F' | |
| ] | |
| result = "" | |
| for char in text: | |
| result += char | |
| # 各文字に3-5個のコンビニング文字をランダムに追加 | |
| marks_count = random.randint(3, 5) | |
| for _ in range(marks_count): | |
| result += random.choice(combining_marks) | |
| return result | |
| # ブロック付きテキスト (文字ごとに四角で囲む) | |
| def create_square_enclosed_text(text): | |
| result = "" | |
| for char in text: | |
| if char.strip(): # 空白文字でない場合 | |
| result += char + "\u20DE " # U+20DE COMBINING ENCLOSING SQUARE | |
| else: | |
| result += char + " " | |
| return result | |
| # サークル付きテキスト (文字ごとに丸で囲む) | |
| def create_circle_enclosed_text(text): | |
| result = "" | |
| for char in text: | |
| if char.strip(): # 空白文字でない場合 | |
| result += char + "\u20DD " # U+20DD COMBINING ENCLOSING CIRCLE | |
| else: | |
| result += char + " " | |
| return result | |
| # キーキャップ付きテキスト(数字やアルファベットに四角) | |
| def create_keycap_text(text): | |
| result = "" | |
| for char in text: | |
| # 数字または英字の場合 | |
| if char.isalnum(): | |
| result += char + "\u20E3 " # U+20E3 COMBINING ENCLOSING KEYCAP | |
| else: | |
| result += char + " " | |
| return result | |
| # 地域インジケーター文字装飾(アルファベットを国旗風に) | |
| def create_regional_indicator_text(text): | |
| result = "" | |
| for char in text.lower(): | |
| if 'a' <= char <= 'z': | |
| # 地域インジケーター文字 (🇦 🇧 🇨など) | |
| regional_indicator = chr(0x1F1E6 + ord(char) - ord('a')) | |
| result += regional_indicator + "\u200B" # ゼロ幅スペースで区切る | |
| else: | |
| result += char | |
| return result | |
| # 複数のコンビニング文字スタイル | |
| def text_with_multiple_combining(text): | |
| # 下線と上線の両方を適用 | |
| result = "" | |
| for char in text: | |
| if char.strip(): | |
| # 下線 + 上線 + 打ち消し線 | |
| result += char + "\u0332\u0305\u0336" | |
| else: | |
| result += char | |
| return result | |
| # 縦書きスタイルのテキスト | |
| def create_vertical_text(text): | |
| result = "" | |
| for char in text: | |
| if char.strip(): | |
| result += char + "\uFE0E " # U+FE0E VARIATION SELECTOR-15 (テキスト表示) | |
| else: | |
| result += char + " " | |
| return result | |
| # 事前定義したスタイル | |
| fallback_styles = { | |
| "styles": [ | |
| {"name": "数学体", "text": decorate_alphanum(text, to_math_bold)}, | |
| {"name": "フラクトゥール", "text": decorate_alphanum(text, to_fraktur)}, | |
| {"name": "太字フラクトゥール", "text": decorate_alphanum(text, to_bold_fraktur)}, | |
| {"name": "スクリプト体", "text": decorate_alphanum(text, to_script)}, | |
| {"name": "太字", "text": decorate_alphanum(text, to_bold)}, | |
| {"name": "イタリック", "text": decorate_alphanum(text, to_italic)}, | |
| {"name": "太字イタリック", "text": decorate_alphanum(text, to_bold_italic)}, | |
| {"name": "サンセリフ", "text": decorate_alphanum(text, to_sans)}, | |
| {"name": "小型大文字", "text": decorate_alphanum(text, to_small_caps)}, | |
| {"name": "全角変換", "text": to_fullwidth(text)}, | |
| {"name": "シンプル星", "text": f"★ {text} ★"}, | |
| {"name": "波線", "text": f"〜{text}〜"}, | |
| {"name": "ドット", "text": f"・{text}・"}, | |
| {"name": "括弧", "text": f"(( {text} ))"}, | |
| {"name": "星空", "text": f"✧{text}✧"}, | |
| {"name": "ハート", "text": f"♡{text}♡"}, | |
| {"name": "アスタリスク", "text": f"*{text}*"}, | |
| {"name": "矢印", "text": f"→{text}←"}, | |
| {"name": "四角", "text": f"■{text}■"}, | |
| {"name": "丸", "text": f"●{text}●"}, | |
| {"name": "キラキラ", "text": f"✨{text}✨"}, | |
| {"name": "シンプル装飾", "text": f"▶ {text} ◀"}, | |
| {"name": "花装飾", "text": f"❀ {text} ❀"}, | |
| {"name": "音符", "text": f"♪ {text} ♪"}, | |
| {"name": "サンダーマーク", "text": f"⚡ {text} ⚡"}, | |
| {"name": "ハートフレーム", "text": f"❤️ {text} ❤️"}, | |
| {"name": "丸括弧", "text": f"({text})"}, | |
| {"name": "角括弧", "text": f"[{text}]"}, | |
| {"name": "波括弧", "text": f"{{{text}}}"}, | |
| {"name": "山括弧", "text": f"《{text}》"}, | |
| {"name": "二重山括弧", "text": f"《《{text}》》"}, | |
| {"name": "鉤括弧", "text": f"「{text}」"}, | |
| {"name": "二重鉤括弧", "text": f"『{text}』"}, | |
| {"name": "下線", "text": f"_{text}_"}, | |
| {"name": "スパークル", "text": f"*:・゚{text}*:・゚"}, | |
| {"name": "チェック", "text": f"✓ {text} ✓"}, | |
| {"name": "囲み文字", "text": ''.join([f"【{c}】" for c in text])}, | |
| {"name": "ドット区切り", "text": '•'.join(text)}, | |
| {"name": "ハート区切り", "text": '♥'.join(text)}, | |
| {"name": "星区切り", "text": '★'.join(text)}, | |
| {"name": "花区切り", "text": '✿'.join(text)}, | |
| {"name": "虹装飾", "text": f"🌈 {text} 🌈"}, | |
| {"name": "キラキラハート", "text": f"💖 {text} 💖"}, | |
| {"name": "クラウン", "text": f"👑 {text} 👑"}, | |
| {"name": "バブル", "text": f"🫧 {text} 🫧"}, | |
| {"name": "かわいい花", "text": f"🌸 {text} 🌸"}, | |
| {"name": "リボン装飾", "text": f"🎀 {text} 🎀"}, | |
| {"name": "クラシック", "text": f"『 {text} 』"}, | |
| {"name": "ピース", "text": f"✌️ {text} ✌️"}, | |
| {"name": "レインボー", "text": f"🌈✨ {text} ✨🌈"}, | |
| # 追加のコンビニング文字スタイル | |
| {"name": "グリッチ風", "text": create_glitch_text(text)}, | |
| {"name": "激しいグリッチ", "text": create_heavy_glitch_text(text)}, | |
| {"name": "四角囲み", "text": create_square_enclosed_text(text)}, | |
| {"name": "丸囲み", "text": create_circle_enclosed_text(text)}, | |
| {"name": "キーキャップ", "text": create_keycap_text(text)}, | |
| {"name": "リージョナルインジケーター", "text": create_regional_indicator_text(text)}, | |
| {"name": "多重装飾", "text": text_with_multiple_combining(text)}, | |
| {"name": "テキスト反転", "text": reverse_text(text)}, | |
| {"name": "縦書きスタイル", "text": create_vertical_text(text)}, | |
| ] | |
| } | |
| return fallback_styles |