File size: 18,175 Bytes
8fe8dce 4a1bb2e 8fe8dce c62d05d 8fe8dce 4a1bb2e 8fe8dce 4a1bb2e 8fe8dce 9d6304b 8fe8dce 9d6304b 4a1bb2e 9d6304b 4a1bb2e 9d6304b 4a1bb2e 9d6304b 4a1bb2e 9d6304b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | 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 |