mirror of
https://github.com/senju1337/senju.git
synced 2025-12-24 07:39:29 +00:00
refactor: Change line length to apease flake8
Refs: OPS-68
This commit is contained in:
parent
2e3987ab8b
commit
f17d29c39c
5 changed files with 59 additions and 29 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -180,4 +180,3 @@ ollama
|
||||||
*.kate-swp
|
*.kate-swp
|
||||||
# sphinx rst files
|
# sphinx rst files
|
||||||
docs/source/_modules
|
docs/source/_modules
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,21 +4,27 @@ Haiku Generation Module
|
||||||
|
|
||||||
A client interface for AI-powered haiku poem generation.
|
A client interface for AI-powered haiku poem generation.
|
||||||
|
|
||||||
This module provides the core functionality for communicating with an Ollama-based
|
This module provides the core functionality for communicating
|
||||||
AI service to generate three-line haiku poems. It handles the entire generation
|
with an Ollama-based
|
||||||
process, from sending properly formatted requests to processing and validating
|
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.
|
the returned poems.
|
||||||
|
|
||||||
Classes
|
Classes
|
||||||
-------
|
-------
|
||||||
Haiku
|
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.
|
manipulation and serialization of poem data.
|
||||||
|
|
||||||
**Methods**:
|
**Methods**:
|
||||||
|
|
||||||
* ``to_json()``: Converts a haiku instance to JSON format for API responses
|
* ``to_json()``: Converts a haiku instance to JSON format for API
|
||||||
* ``generate_haiku(seed_text)``: Creates a new haiku using the AI service
|
responses
|
||||||
|
* ``generate_haiku(seed_text)``: Creates a new haiku using
|
||||||
|
the AI service
|
||||||
|
|
||||||
Constants
|
Constants
|
||||||
---------
|
---------
|
||||||
|
|
@ -40,7 +46,8 @@ Dependencies
|
||||||
|
|
||||||
Implementation Details
|
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
|
1. Proper request formatting with seed text integration
|
||||||
2. Multiple retry attempts for handling temporary service issues
|
2. Multiple retry attempts for handling temporary service issues
|
||||||
|
|
@ -88,9 +95,11 @@ class Haiku:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def request_haiku(seed: str) -> 'Haiku':
|
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.
|
It validates that the response contains exactly 3 lines.
|
||||||
The function will retry until a valid haiku is generated.
|
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.
|
:return: A new Haiku object containing the generated three lines.
|
||||||
:rtype: Haiku
|
: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 = {
|
ai_gen_request = {
|
||||||
"model": "haiku",
|
"model": "haiku",
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,10 @@ Senju Haiku Web Application
|
||||||
|
|
||||||
A Flask-based web interface for generating, viewing, and managing haiku poetry.
|
A Flask-based web interface for generating, viewing, and managing haiku poetry.
|
||||||
|
|
||||||
This application provides a comprehensive interface between users and an AI-powered
|
This application provides a comprehensive interface between users
|
||||||
haiku generation service, with persistent storage capabilities. Users can interact
|
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.
|
with the system through both a web interface and a RESTful API.
|
||||||
|
|
||||||
Features
|
Features
|
||||||
|
|
@ -13,13 +15,15 @@ Features
|
||||||
* **Landing page**: Welcome interface introducing users to the Senju service
|
* **Landing page**: Welcome interface introducing users to the Senju service
|
||||||
* **Browsing interface**: Gallery-style viewing of previously generated haikus
|
* **Browsing interface**: Gallery-style viewing of previously generated haikus
|
||||||
* **Prompt interface**: Text input system for generating haikus from seed text
|
* **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
|
* **RESTful API**: Programmatic access for integration with other services
|
||||||
|
|
||||||
Architecture
|
Architecture
|
||||||
------------
|
------------
|
||||||
The application implements a RESTful architecture using Flask's routing system
|
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.
|
routes, with appropriate error handling for exceptional cases.
|
||||||
|
|
||||||
Dependencies
|
Dependencies
|
||||||
|
|
@ -63,6 +67,7 @@ def index_view():
|
||||||
"""
|
"""
|
||||||
return render_template("index.html", title="Senju")
|
return render_template("index.html", title="Senju")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/haiku/")
|
@app.route("/haiku/")
|
||||||
def haiku_index_view():
|
def haiku_index_view():
|
||||||
"""
|
"""
|
||||||
|
|
@ -77,6 +82,7 @@ def haiku_index_view():
|
||||||
haiku_id = 0
|
haiku_id = 0
|
||||||
return redirect(url_for("haiku_view", haiku_id=haiku_id, is_default=1))
|
return redirect(url_for("haiku_view", haiku_id=haiku_id, is_default=1))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/haiku/<int:haiku_id>")
|
@app.route("/haiku/<int:haiku_id>")
|
||||||
def haiku_view(haiku_id):
|
def haiku_view(haiku_id):
|
||||||
"""
|
"""
|
||||||
|
|
@ -107,6 +113,7 @@ def haiku_view(haiku_id):
|
||||||
context=context,
|
context=context,
|
||||||
title="Haiku of the Day")
|
title="Haiku of the Day")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/prompt")
|
@app.route("/prompt")
|
||||||
def prompt_view():
|
def prompt_view():
|
||||||
"""
|
"""
|
||||||
|
|
@ -120,6 +127,7 @@ def prompt_view():
|
||||||
title="Haiku generation"
|
title="Haiku generation"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/scan")
|
@app.route("/scan")
|
||||||
def scan_view():
|
def scan_view():
|
||||||
"""
|
"""
|
||||||
|
|
@ -133,6 +141,7 @@ def scan_view():
|
||||||
title="Image scanning"
|
title="Image scanning"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/v1/haiku", methods=['POST'])
|
@app.route("/api/v1/haiku", methods=['POST'])
|
||||||
def generate_haiku():
|
def generate_haiku():
|
||||||
"""
|
"""
|
||||||
|
|
@ -157,6 +166,7 @@ def generate_haiku():
|
||||||
else:
|
else:
|
||||||
return "Method not allowed", 405
|
return "Method not allowed", 405
|
||||||
|
|
||||||
|
|
||||||
@app.route('/favicon.ico')
|
@app.route('/favicon.ico')
|
||||||
def favicon():
|
def favicon():
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -4,25 +4,30 @@ Senju Database Management Module
|
||||||
|
|
||||||
A database interaction layer for the Senju haiku management system.
|
A database interaction layer for the Senju haiku management system.
|
||||||
|
|
||||||
This module implements a lightweight document database abstraction using TinyDB
|
This module implements a lightweight document database
|
||||||
for persistent storage of haiku poems. It provides a clean interface for storing,
|
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.
|
retrieving, updating, and managing haiku entries in the system.
|
||||||
|
|
||||||
Classes
|
Classes
|
||||||
-------
|
-------
|
||||||
StoreManager
|
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.
|
management, CRUD operations, and query capabilities for haiku data.
|
||||||
|
|
||||||
Functions
|
Functions
|
||||||
---------
|
---------
|
||||||
utility_function
|
utility_function
|
||||||
Provides simple arithmetic operations to support database functionalities.
|
Provides simple arithmetic operations to support
|
||||||
|
database functionalities.
|
||||||
|
|
||||||
Constants
|
Constants
|
||||||
---------
|
---------
|
||||||
DEFAULT_DB_PATH
|
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
|
Dependencies
|
||||||
------------
|
------------
|
||||||
|
|
@ -38,7 +43,8 @@ Implementation Details
|
||||||
----------------------
|
----------------------
|
||||||
The module uses TinyDB as its storage engine, providing a JSON-based document
|
The module uses TinyDB as its storage engine, providing a JSON-based document
|
||||||
storage solution that balances simplicity with functionality. The StoreManager
|
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.
|
and providing methods for common operations on haiku data.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
@ -67,9 +73,11 @@ class BadStoreManagerFileError(Exception):
|
||||||
|
|
||||||
class StoreManager:
|
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.
|
a TinyDB database file.
|
||||||
|
|
||||||
:ivar _db: Database instance for storing haiku data.
|
:ivar _db: Database instance for storing haiku data.
|
||||||
|
|
@ -85,7 +93,8 @@ class StoreManager:
|
||||||
"""
|
"""
|
||||||
Initialize the StoreManager with a database path.
|
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
|
:type path_to_db: Path, optional
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
|
@ -118,7 +127,8 @@ class StoreManager:
|
||||||
:rtype: Optional[dict]
|
:rtype: Optional[dict]
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
Logs a warning if document with specified ID is not found.
|
Logs a warning if document with specified
|
||||||
|
ID is not found.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return self._db.get(doc_id=id)
|
return self._db.get(doc_id=id)
|
||||||
|
|
@ -169,7 +179,8 @@ class StoreManager:
|
||||||
"""
|
"""
|
||||||
Get the ID of the most recently added haiku.
|
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]
|
:rtype: Optional[int]
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
id="user-input"
|
id="user-input"
|
||||||
minlength="0"
|
minlength="0"
|
||||||
maxlength="100"
|
maxlength="100"
|
||||||
placeholder="Type your prompt here..."
|
placeholder="Type your prompt here..."
|
||||||
class="w-full px-4 py-3 text-lg border-2 border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-violet-600"
|
class="w-full px-4 py-3 text-lg border-2 border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-violet-600"
|
||||||
|
|
@ -39,7 +39,7 @@ document.getElementById("submit-btn").addEventListener("click", function() {
|
||||||
|
|
||||||
if (userInput.trim() === "") {
|
if (userInput.trim() === "") {
|
||||||
responseText.textContent = "Please enter a prompt!";
|
responseText.textContent = "Please enter a prompt!";
|
||||||
}
|
}
|
||||||
else if (userInput.trim() === "amogus") {
|
else if (userInput.trim() === "amogus") {
|
||||||
responseText.textContent = "🤖 AI is thinking...";
|
responseText.textContent = "🤖 AI is thinking...";
|
||||||
responseBox.classList.remove("opacity-0");
|
responseBox.classList.remove("opacity-0");
|
||||||
|
|
@ -48,7 +48,7 @@ document.getElementById("submit-btn").addEventListener("click", function() {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
responseText.textContent = "Sus imposter ඞ";
|
responseText.textContent = "Sus imposter ඞ";
|
||||||
}, 1500);
|
}, 1500);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
responseText.textContent = "🤖 AI is thinking...";
|
responseText.textContent = "🤖 AI is thinking...";
|
||||||
responseBox.classList.remove("opacity-0");
|
responseBox.classList.remove("opacity-0");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue