Spaces:
Running
Running
Update turbowarp-server/qr-converter.py
Browse files
turbowarp-server/qr-converter.py
CHANGED
|
@@ -52,13 +52,35 @@ with open("turbowarp-server/n-chars.txt", "r", encoding="utf-8") as f:
|
|
| 52 |
n_chars = [line.strip() for line in f]
|
| 53 |
print(f"[INFO] Loaded {len(n_chars)} characters.")
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def decode_prompt(encoded_str):
|
| 56 |
-
"""クラウド変数の数字列を元にプロンプト文字列に復号"""
|
| 57 |
chars = []
|
| 58 |
-
|
|
|
|
| 59 |
idx = int(encoded_str[i:i+2])
|
| 60 |
-
if idx
|
|
|
|
|
|
|
| 61 |
chars.append(n_chars[idx])
|
|
|
|
|
|
|
|
|
|
| 62 |
decoded = "".join(chars)
|
| 63 |
print(f"[DECODE] Encoded prompt -> '{decoded[:50]}...'")
|
| 64 |
return decoded
|
|
@@ -97,7 +119,7 @@ def resize_and_encode(img):
|
|
| 97 |
for x in range(90):
|
| 98 |
encoded += rgb_to_scratch_number(img.getpixel((x, y)))
|
| 99 |
print(f"[INFO] Image encoded to string of length {len(encoded)}.")
|
| 100 |
-
return encoded
|
| 101 |
|
| 102 |
def split_packets(data, max_length=9998):
|
| 103 |
"""10000文字制限に合わせて分割(先頭 '10' 含む)"""
|
|
|
|
| 52 |
n_chars = [line.strip() for line in f]
|
| 53 |
print(f"[INFO] Loaded {len(n_chars)} characters.")
|
| 54 |
|
| 55 |
+
def encode_prompt(prompt):
|
| 56 |
+
"""プロンプト文字列を数字列にエンコード(半角スペースは96として特別処理)"""
|
| 57 |
+
encoded = ""
|
| 58 |
+
for char in prompt:
|
| 59 |
+
if char == " ": # 半角スペースを特別に96として処理
|
| 60 |
+
encoded += "96"
|
| 61 |
+
else:
|
| 62 |
+
try:
|
| 63 |
+
idx = n_chars.index(char)
|
| 64 |
+
encoded += f"{idx:02d}"
|
| 65 |
+
except ValueError:
|
| 66 |
+
print(f"[WARNING] Character '{char}' not found in n_chars, skipping")
|
| 67 |
+
continue
|
| 68 |
+
print(f"[ENCODE] Prompt encoded to length {len(encoded)}")
|
| 69 |
+
return encoded
|
| 70 |
+
|
| 71 |
def decode_prompt(encoded_str):
|
| 72 |
+
"""クラウド変数の数字列を元にプロンプト文字列に復号(96は半角スペースとして処理)"""
|
| 73 |
chars = []
|
| 74 |
+
i = 0
|
| 75 |
+
while i < len(encoded_str):
|
| 76 |
idx = int(encoded_str[i:i+2])
|
| 77 |
+
if idx == 96: # 96は半角スペースとして扱う
|
| 78 |
+
chars.append(" ")
|
| 79 |
+
elif idx < len(n_chars):
|
| 80 |
chars.append(n_chars[idx])
|
| 81 |
+
else:
|
| 82 |
+
print(f"[WARNING] Invalid index {idx} at position {i}")
|
| 83 |
+
i += 2
|
| 84 |
decoded = "".join(chars)
|
| 85 |
print(f"[DECODE] Encoded prompt -> '{decoded[:50]}...'")
|
| 86 |
return decoded
|
|
|
|
| 119 |
for x in range(90):
|
| 120 |
encoded += rgb_to_scratch_number(img.getpixel((x, y)))
|
| 121 |
print(f"[INFO] Image encoded to string of length {len(encoded)}.")
|
| 122 |
+
return encoded
|
| 123 |
|
| 124 |
def split_packets(data, max_length=9998):
|
| 125 |
"""10000文字制限に合わせて分割(先頭 '10' 含む)"""
|