Skip to content

Commit b00dd60

Browse files
committed
refactor: add type hints and improve docstrings for core functions
1 parent 80791f9 commit b00dd60

2 files changed

Lines changed: 48 additions & 14 deletions

File tree

stego_llm/core/decoder.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from typing import Optional
23
from stego_llm.steganography import (
34
chunks_to_message,
45
find_acceptable_token,
@@ -17,13 +18,29 @@
1718

1819

1920
def main_decode(
20-
encoded_prompt,
21-
initial_prompt,
22-
chunk_size,
23-
num_logprobs,
24-
llm_path=None,
25-
):
26-
"""Main decoding function for steganographic message extraction."""
21+
encoded_prompt: str,
22+
initial_prompt: str,
23+
chunk_size: int,
24+
num_logprobs: int,
25+
llm_path: Optional[str] = None,
26+
) -> Optional[bytes]:
27+
"""Decodes a message hidden in a text.
28+
29+
This function extracts a hidden message from a text that was encoded
30+
using steganography. It uses a language model to determine the likely
31+
sequence of tokens that represent the hidden message.
32+
33+
Args:
34+
encoded_prompt (str): The text containing the hidden message.
35+
initial_prompt (str): The initial text used to start the encoding process.
36+
chunk_size (int): The number of bits per chunk used for encoding.
37+
num_logprobs (int): The number of token probabilities to consider.
38+
llm_path (Optional[str]): The path to the language model file.
39+
If None, the default model is used.
40+
41+
Returns:
42+
Optional[bytes]: The decoded message as bytes, or None if decoding fails.
43+
"""
2744
llm = create_llm_client(model_path=llm_path)
2845
message_carrying_text = encoded_prompt[len(initial_prompt) :]
2946
memo = {}

stego_llm/core/encoder.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from typing import Optional
23
from stego_llm.steganography import (
34
message_to_chunks,
45
find_acceptable_token,
@@ -17,13 +18,29 @@
1718

1819

1920
def main_encode(
20-
initial_prompt,
21-
msg,
22-
chunk_size,
23-
num_logprobs,
24-
llm_path=None,
25-
):
26-
"""Main encoding function for steganographic text generation."""
21+
initial_prompt: str,
22+
msg: bytes,
23+
chunk_size: int,
24+
num_logprobs: int,
25+
llm_path: Optional[str] = None,
26+
) -> str:
27+
"""Encodes a message into a text using steganography.
28+
29+
This function hides a message within a carrier text generated by a language
30+
model. It embeds the message by selecting specific tokens based on the
31+
message's binary representation.
32+
33+
Args:
34+
initial_prompt (str): The starting text to prompt the language model.
35+
msg (bytes): The message to be hidden, as a byte string.
36+
chunk_size (int): The number of bits from the message to encode in each step.
37+
num_logprobs (int): The number of next-token probabilities to request from the LLM.
38+
llm_path (Optional[str]): The path to the language model file.
39+
If None, the default model is used.
40+
41+
Returns:
42+
str: The generated text with the message embedded within it.
43+
"""
2744
llm = create_llm_client(model_path=llm_path)
2845
enc_ints = message_to_chunks(msg, chunk_size=chunk_size)
2946
current_prompt = initial_prompt

0 commit comments

Comments
 (0)