diff --git a/.gitignore b/.gitignore index 16c5cfa..33b10d1 100644 --- a/.gitignore +++ b/.gitignore @@ -180,4 +180,3 @@ ollama *.kate-swp # sphinx rst files docs/source/_modules - diff --git a/senju/haiku.py b/senju/haiku.py index 0ff3b24..68ffecd 100644 --- a/senju/haiku.py +++ b/senju/haiku.py @@ -4,21 +4,27 @@ Haiku Generation Module A client interface for AI-powered haiku poem generation. -This module provides the core functionality for communicating with an Ollama-based -AI service to generate three-line haiku poems. It handles the entire generation -process, from sending properly formatted requests to processing and validating +This module provides the core functionality for communicating +with an Ollama-based +AI service to generate three-line haiku poems. It handles the +entire generation +process, from sending properly formatted requests to processing +and validating the returned poems. Classes ------- Haiku - A dataclass representation of a haiku poem, providing structure for storage, + A dataclass representation of a haiku poem, providing structure +for storage, manipulation and serialization of poem data. **Methods**: - * ``to_json()``: Converts a haiku instance to JSON format for API responses - * ``generate_haiku(seed_text)``: Creates a new haiku using the AI service + * ``to_json()``: Converts a haiku instance to JSON format for API + responses + * ``generate_haiku(seed_text)``: Creates a new haiku using + the AI service Constants --------- @@ -40,7 +46,8 @@ Dependencies Implementation Details ---------------------- -The module implements a robust communication pattern with the AI service, including: +The module implements a robust communication pattern with the +AI service, including: 1. Proper request formatting with seed text integration 2. Multiple retry attempts for handling temporary service issues @@ -88,9 +95,11 @@ class Haiku: @staticmethod def request_haiku(seed: str) -> 'Haiku': """ - Generates a haiku using an AI model based on the provided seed text. + Generates a haiku using an AI model based on the + provided seed text. - This function prompts the AI to generate a haiku based on the user input. + This function prompts the AI to generate a haiku based on the + user input. It validates that the response contains exactly 3 lines. The function will retry until a valid haiku is generated. @@ -99,7 +108,8 @@ class Haiku: :return: A new Haiku object containing the generated three lines. :rtype: Haiku - :raises: Possible JSONDecodeError which is caught and handled with retries. + :raises: Possible JSONDecodeError which is caught and handled + with retries. """ ai_gen_request = { "model": "haiku", diff --git a/senju/main.py b/senju/main.py index e30df77..306a532 100644 --- a/senju/main.py +++ b/senju/main.py @@ -4,8 +4,10 @@ Senju Haiku Web Application A Flask-based web interface for generating, viewing, and managing haiku poetry. -This application provides a comprehensive interface between users and an AI-powered -haiku generation service, with persistent storage capabilities. Users can interact +This application provides a comprehensive interface between users +and an AI-powered +haiku generation service, with persistent storage capabilities. +Users can interact with the system through both a web interface and a RESTful API. Features @@ -13,13 +15,15 @@ Features * **Landing page**: Welcome interface introducing users to the Senju service * **Browsing interface**: Gallery-style viewing of previously generated haikus * **Prompt interface**: Text input system for generating haikus from seed text -* **Image scanning**: Experimental interface for creating haikus from visual inputs +* **Image scanning**: Experimental interface for creating haikus + from visual inputs * **RESTful API**: Programmatic access for integration with other services Architecture ------------ The application implements a RESTful architecture using Flask's routing system -and template rendering. All user interactions are handled through clearly defined +and template rendering. All user interactions are handled through +clearly defined routes, with appropriate error handling for exceptional cases. Dependencies @@ -63,6 +67,7 @@ def index_view(): """ return render_template("index.html", title="Senju") + @app.route("/haiku/") def haiku_index_view(): """ @@ -77,6 +82,7 @@ def haiku_index_view(): haiku_id = 0 return redirect(url_for("haiku_view", haiku_id=haiku_id, is_default=1)) + @app.route("/haiku/") def haiku_view(haiku_id): """ @@ -107,6 +113,7 @@ def haiku_view(haiku_id): context=context, title="Haiku of the Day") + @app.route("/prompt") def prompt_view(): """ @@ -120,6 +127,7 @@ def prompt_view(): title="Haiku generation" ) + @app.route("/scan") def scan_view(): """ @@ -133,6 +141,7 @@ def scan_view(): title="Image scanning" ) + @app.route("/api/v1/haiku", methods=['POST']) def generate_haiku(): """ @@ -157,6 +166,7 @@ def generate_haiku(): else: return "Method not allowed", 405 + @app.route('/favicon.ico') def favicon(): """ diff --git a/senju/store_manager.py b/senju/store_manager.py index 7580255..e94295b 100644 --- a/senju/store_manager.py +++ b/senju/store_manager.py @@ -4,25 +4,30 @@ Senju Database Management Module A database interaction layer for the Senju haiku management system. -This module implements a lightweight document database abstraction using TinyDB -for persistent storage of haiku poems. It provides a clean interface for storing, +This module implements a lightweight document database +abstraction using TinyDB +for persistent storage of haiku poems. It provides a +clean interface for storing, retrieving, updating, and managing haiku entries in the system. Classes ------- StoreManager - The primary class responsible for all database operations. Handles connection + The primary class responsible for all database operations. + Handles connection management, CRUD operations, and query capabilities for haiku data. Functions --------- utility_function - Provides simple arithmetic operations to support database functionalities. + Provides simple arithmetic operations to support + database functionalities. Constants --------- DEFAULT_DB_PATH - The default filesystem location for the TinyDB database file (/var/lib/senju.json). + The default filesystem location for the TinyDB database file + (/var/lib/senju.json). Dependencies ------------ @@ -38,7 +43,8 @@ Implementation Details ---------------------- The module uses TinyDB as its storage engine, providing a JSON-based document storage solution that balances simplicity with functionality. The StoreManager -abstracts all database operations behind a clean API, handling connection lifecycle +abstracts all database operations behind a clean API, +handling connection lifecycle and providing methods for common operations on haiku data. """ @@ -67,9 +73,11 @@ class BadStoreManagerFileError(Exception): class StoreManager: """ - Manages the storage and retrieval of haiku data using TinyDB. + Manages the storage and retrieval of haiku + data using TinyDB. - This class provides an interface for saving and loading haikus from + This class provides an interface for saving and + loading haikus from a TinyDB database file. :ivar _db: Database instance for storing haiku data. @@ -85,7 +93,8 @@ class StoreManager: """ Initialize the StoreManager with a database path. - :param path_to_db: Path to the TinyDB database file. Defaults to DEFAULT_DB_PATH. + :param path_to_db: Path to the TinyDB database file. + Defaults to DEFAULT_DB_PATH. :type path_to_db: Path, optional :return: None """ @@ -118,7 +127,8 @@ class StoreManager: :rtype: Optional[dict] .. note:: - Logs a warning if document with specified ID is not found. + Logs a warning if document with specified + ID is not found. """ try: return self._db.get(doc_id=id) @@ -169,7 +179,8 @@ class StoreManager: """ Get the ID of the most recently added haiku. - :return: The ID of the latest haiku if any exists, None otherwise. + :return: The ID of the latest haiku if any exists, + None otherwise. :rtype: Optional[int] .. note:: diff --git a/senju/templates/prompt.html b/senju/templates/prompt.html index cf1aa73..e9128ff 100644 --- a/senju/templates/prompt.html +++ b/senju/templates/prompt.html @@ -8,7 +8,7 @@ { responseText.textContent = "Sus imposter āļž"; }, 1500); - } + } else { responseText.textContent = "🤖 AI is thinking..."; responseBox.classList.remove("opacity-0");