mirror of
https://github.com/senju1337/senju.git
synced 2025-12-23 23:39:27 +00:00
64 lines
1.5 KiB
Python
64 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from flask import Flask, redirect, render_template, request, url_for
|
|
|
|
from senju.ai_gen import request_haiku
|
|
from senju.haiku import Haiku
|
|
from senju.store_manager import StoreManager
|
|
|
|
app = Flask(__name__)
|
|
|
|
store = StoreManager(Path("/tmp/store.db"))
|
|
|
|
|
|
@app.route("/")
|
|
def index_view():
|
|
return render_template("index.html", title="Senju")
|
|
|
|
|
|
@app.route("/haiku/")
|
|
def haiku_index_view():
|
|
haiku_id: int | None = store.get_id_of_latest_haiku()
|
|
if haiku_id is None:
|
|
# TODO: add "empty haiku list" error page
|
|
raise KeyError("no haiku exist yet")
|
|
return redirect(url_for("haiku_view", haiku_id=haiku_id))
|
|
|
|
|
|
@app.route("/haiku/<int:haiku_id>")
|
|
def haiku_view(haiku_id):
|
|
haiku: Haiku | None = store.load_haiku(haiku_id)
|
|
if haiku is None:
|
|
# TODO: add "haiku not found" page
|
|
raise KeyError("haiku not found")
|
|
context: dict = {
|
|
"haiku": haiku
|
|
}
|
|
|
|
return render_template(
|
|
"haiku.html",
|
|
context=context,
|
|
title="Haiku of the Day")
|
|
|
|
|
|
@app.route("/prompt")
|
|
def prompt_view():
|
|
return render_template(
|
|
"prompt.html",
|
|
title="Haiku generation"
|
|
)
|
|
|
|
|
|
@app.route("/generate_haiku", methods=['POST'])
|
|
def generate_haiku():
|
|
prompt = "a"
|
|
if request.method == 'POST':
|
|
json_data = request.get_json()
|
|
prompt = json_data["prompt"]
|
|
haiku = request_haiku(prompt)
|
|
id = store.save_haiku(haiku)
|
|
return str(id)
|
|
else:
|
|
return "Method not allowed", 405
|