Spaces:
Sleeping
Sleeping
Update turbowarp-server/qr-converter.py
Browse files
turbowarp-server/qr-converter.py
CHANGED
|
@@ -7,27 +7,37 @@ import scratchcommunication
|
|
| 7 |
|
| 8 |
# --- Scratchクラウド接続 ---
|
| 9 |
PROJECT_ID = "1293030706"
|
|
|
|
| 10 |
tw = scratchcommunication.TwCloudConnection(
|
| 11 |
project_id=PROJECT_ID,
|
| 12 |
username="server",
|
| 13 |
contact_info="contact"
|
| 14 |
)
|
|
|
|
| 15 |
|
| 16 |
def get_var(name):
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
return None
|
| 21 |
|
| 22 |
def set_var(name, value):
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# --- n-chars.txt の読み込み ---
|
|
|
|
| 29 |
with open("turbowarp-server/n-chars.txt", "r", encoding="utf-8") as f:
|
| 30 |
n_chars = [line.strip() for line in f]
|
|
|
|
| 31 |
|
| 32 |
def decode_prompt(encoded_str):
|
| 33 |
"""クラウド変数の数字列を元にプロンプト文字列に復号"""
|
|
@@ -36,7 +46,9 @@ def decode_prompt(encoded_str):
|
|
| 36 |
idx = int(encoded_str[i:i+2])
|
| 37 |
if idx < len(n_chars):
|
| 38 |
chars.append(n_chars[idx])
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def rgb_to_scratch_number(rgb):
|
| 42 |
"""RGBを0-999999の10進数に変換"""
|
|
@@ -46,6 +58,7 @@ def rgb_to_scratch_number(rgb):
|
|
| 46 |
def generate_image(prompt):
|
| 47 |
"""APIから352x352の画像生成"""
|
| 48 |
seed = random.randint(0, 999999)
|
|
|
|
| 49 |
params = {
|
| 50 |
"prompt": prompt,
|
| 51 |
"negative_prompt": "nsfw, low quality",
|
|
@@ -58,15 +71,19 @@ def generate_image(prompt):
|
|
| 58 |
}
|
| 59 |
resp = requests.get("https://izuemon-pixart-alpha-pixart-sigma-xl-2-1024-ms.hf.space/gen", params=params)
|
| 60 |
resp.raise_for_status()
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
def resize_and_encode(img):
|
| 64 |
"""90x90にリサイズして数字列に変換"""
|
| 65 |
img = img.resize((90, 90))
|
|
|
|
| 66 |
encoded = ""
|
| 67 |
for y in range(90):
|
| 68 |
for x in range(90):
|
| 69 |
encoded += rgb_to_scratch_number(img.getpixel((x, y)))
|
|
|
|
| 70 |
return "10" + encoded # 先頭に "10"
|
| 71 |
|
| 72 |
def split_packets(data, max_length=9998):
|
|
@@ -77,14 +94,18 @@ def split_packets(data, max_length=9998):
|
|
| 77 |
chunk = data[idx:idx + max_length]
|
| 78 |
packets.append(chunk)
|
| 79 |
idx += max_length
|
|
|
|
| 80 |
return packets
|
| 81 |
|
| 82 |
# --- メインループ ---
|
|
|
|
| 83 |
while True:
|
| 84 |
try:
|
| 85 |
n1 = get_var("n1")
|
| 86 |
if n1 and len(n1) >= 3 and n1[0] == "0": # ID受信フラグ
|
|
|
|
| 87 |
if get_var("n0") == "0": # 他ユーザー未使用
|
|
|
|
| 88 |
set_var("n0", "1")
|
| 89 |
user_id = n1[1:3] # 実際のIDはここを調整
|
| 90 |
encoded_prompt = n1[3:]
|
|
@@ -96,15 +117,18 @@ while True:
|
|
| 96 |
|
| 97 |
# パケット分割
|
| 98 |
packets = split_packets(scratch_data)
|
| 99 |
-
for pkt in packets:
|
|
|
|
| 100 |
set_var("n1", pkt) # 送信
|
| 101 |
time.sleep(0.2)
|
| 102 |
# 次パケット送信待ち
|
| 103 |
while get_var("n1") != "11":
|
|
|
|
| 104 |
time.sleep(0.1)
|
| 105 |
|
| 106 |
# 完了後リセット
|
|
|
|
| 107 |
set_var("n0", "0")
|
| 108 |
except Exception as e:
|
| 109 |
-
print("
|
| 110 |
time.sleep(0.2)
|
|
|
|
| 7 |
|
| 8 |
# --- Scratchクラウド接続 ---
|
| 9 |
PROJECT_ID = "1293030706"
|
| 10 |
+
print(f"[INFO] Connecting to Scratch project {PROJECT_ID}...")
|
| 11 |
tw = scratchcommunication.TwCloudConnection(
|
| 12 |
project_id=PROJECT_ID,
|
| 13 |
username="server",
|
| 14 |
contact_info="contact"
|
| 15 |
)
|
| 16 |
+
print("[INFO] Connected successfully.")
|
| 17 |
|
| 18 |
def get_var(name):
|
| 19 |
try:
|
| 20 |
+
value = tw.get_variable(name=name, name_literal=False)
|
| 21 |
+
print(f"[GET] Variable '{name}' = {value}")
|
| 22 |
+
return value
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"[ERROR] Failed to get variable '{name}': {e}")
|
| 25 |
return None
|
| 26 |
|
| 27 |
def set_var(name, value):
|
| 28 |
try:
|
| 29 |
+
tw.set_variable(name=name, value=value, name_literal=False)
|
| 30 |
+
print(f"[SET] Variable '{name}' = {value}")
|
| 31 |
+
return True
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"[ERROR] Failed to set variable '{name}': {e}")
|
| 34 |
+
return False
|
| 35 |
|
| 36 |
# --- n-chars.txt の読み込み ---
|
| 37 |
+
print("[INFO] Loading n-chars.txt...")
|
| 38 |
with open("turbowarp-server/n-chars.txt", "r", encoding="utf-8") as f:
|
| 39 |
n_chars = [line.strip() for line in f]
|
| 40 |
+
print(f"[INFO] Loaded {len(n_chars)} characters.")
|
| 41 |
|
| 42 |
def decode_prompt(encoded_str):
|
| 43 |
"""クラウド変数の数字列を元にプロンプト文字列に復号"""
|
|
|
|
| 46 |
idx = int(encoded_str[i:i+2])
|
| 47 |
if idx < len(n_chars):
|
| 48 |
chars.append(n_chars[idx])
|
| 49 |
+
decoded = "".join(chars)
|
| 50 |
+
print(f"[DECODE] Encoded prompt '{encoded_str[:20]}...' -> '{decoded[:30]}...'")
|
| 51 |
+
return decoded
|
| 52 |
|
| 53 |
def rgb_to_scratch_number(rgb):
|
| 54 |
"""RGBを0-999999の10進数に変換"""
|
|
|
|
| 58 |
def generate_image(prompt):
|
| 59 |
"""APIから352x352の画像生成"""
|
| 60 |
seed = random.randint(0, 999999)
|
| 61 |
+
print(f"[INFO] Generating image with prompt: '{prompt[:50]}...', seed={seed}")
|
| 62 |
params = {
|
| 63 |
"prompt": prompt,
|
| 64 |
"negative_prompt": "nsfw, low quality",
|
|
|
|
| 71 |
}
|
| 72 |
resp = requests.get("https://izuemon-pixart-alpha-pixart-sigma-xl-2-1024-ms.hf.space/gen", params=params)
|
| 73 |
resp.raise_for_status()
|
| 74 |
+
img = Image.open(BytesIO(resp.content))
|
| 75 |
+
print(f"[INFO] Image generated: size={img.size}, mode={img.mode}")
|
| 76 |
+
return img
|
| 77 |
|
| 78 |
def resize_and_encode(img):
|
| 79 |
"""90x90にリサイズして数字列に変換"""
|
| 80 |
img = img.resize((90, 90))
|
| 81 |
+
print("[INFO] Resized image to 90x90.")
|
| 82 |
encoded = ""
|
| 83 |
for y in range(90):
|
| 84 |
for x in range(90):
|
| 85 |
encoded += rgb_to_scratch_number(img.getpixel((x, y)))
|
| 86 |
+
print(f"[INFO] Image encoded to string of length {len(encoded)}.")
|
| 87 |
return "10" + encoded # 先頭に "10"
|
| 88 |
|
| 89 |
def split_packets(data, max_length=9998):
|
|
|
|
| 94 |
chunk = data[idx:idx + max_length]
|
| 95 |
packets.append(chunk)
|
| 96 |
idx += max_length
|
| 97 |
+
print(f"[INFO] Data split into {len(packets)} packet(s).")
|
| 98 |
return packets
|
| 99 |
|
| 100 |
# --- メインループ ---
|
| 101 |
+
print("[INFO] Starting main loop...")
|
| 102 |
while True:
|
| 103 |
try:
|
| 104 |
n1 = get_var("n1")
|
| 105 |
if n1 and len(n1) >= 3 and n1[0] == "0": # ID受信フラグ
|
| 106 |
+
print("[INFO] Received new request from n1.")
|
| 107 |
if get_var("n0") == "0": # 他ユーザー未使用
|
| 108 |
+
print("[INFO] n0 is free, starting processing...")
|
| 109 |
set_var("n0", "1")
|
| 110 |
user_id = n1[1:3] # 実際のIDはここを調整
|
| 111 |
encoded_prompt = n1[3:]
|
|
|
|
| 117 |
|
| 118 |
# パケット分割
|
| 119 |
packets = split_packets(scratch_data)
|
| 120 |
+
for i, pkt in enumerate(packets):
|
| 121 |
+
print(f"[INFO] Sending packet {i+1}/{len(packets)}")
|
| 122 |
set_var("n1", pkt) # 送信
|
| 123 |
time.sleep(0.2)
|
| 124 |
# 次パケット送信待ち
|
| 125 |
while get_var("n1") != "11":
|
| 126 |
+
print("[INFO] Waiting for ACK '11'...")
|
| 127 |
time.sleep(0.1)
|
| 128 |
|
| 129 |
# 完了後リセット
|
| 130 |
+
print("[INFO] Transmission complete. Resetting n0.")
|
| 131 |
set_var("n0", "0")
|
| 132 |
except Exception as e:
|
| 133 |
+
print(f"[ERROR] Exception in main loop: {e}")
|
| 134 |
time.sleep(0.2)
|