feat: Add Method not Allowed handling for generate API and imporove error handling

Refs: OPS-12
This commit is contained in:
Alivecow 2025-03-03 15:36:03 +01:00
parent 0243091b76
commit edf145c8de
8 changed files with 53 additions and 28 deletions

View file

@ -1,5 +1,9 @@
from __future__ import annotations
import json import json
import requests import requests
from senju.haiku import Haiku from senju.haiku import Haiku
AI_BASE_URL: str = "http://ollama:11434/api" AI_BASE_URL: str = "http://ollama:11434/api"
@ -42,6 +46,7 @@ EXACT FORMAT compliance. Any deviation will cause the application to break.
USER INPUT FOR HAIKU CREATION: USER INPUT FOR HAIKU CREATION:
""" """
def request_haiku(seed: str) -> Haiku: def request_haiku(seed: str) -> Haiku:
"""This function prompts the ai to generate """This function prompts the ai to generate
the hauku based on the user input""" the hauku based on the user input"""
@ -54,12 +59,17 @@ def request_haiku(seed: str) -> Haiku:
while True: while True:
try: try:
r = requests.post(url=AI_BASE_URL+AI_GEN_ENDPOINT, json=ai_gen_request) r = requests.post(url=AI_BASE_URL+AI_GEN_ENDPOINT,
json=ai_gen_request)
ai_response = json.loads(r.json()["response"]) ai_response = json.loads(r.json()["response"])
haiku = Haiku([ai_response["line1"], ai_response["line2"], ai_response["line3"]]) haiku = Haiku(
break; [
except: ai_response["line1"],
pass ai_response["line2"],
ai_response["line3"]
])
break
except json.JSONDecodeError:
continue
return haiku return haiku

View file

@ -1,5 +1,8 @@
from dataclasses import dataclass from __future__ import annotations
import json import json
from dataclasses import dataclass
@dataclass @dataclass
class Haiku: class Haiku:
@ -7,4 +10,3 @@ class Haiku:
def get_json(self): def get_json(self):
return json.dumps(self.lines) return json.dumps(self.lines)

View file

@ -1,5 +1,8 @@
from __future__ import annotations
from pathlib import Path from pathlib import Path
from flask import Flask, redirect, render_template, url_for, request
from flask import Flask, redirect, render_template, request, url_for
from senju.ai_gen import request_haiku from senju.ai_gen import request_haiku
from senju.haiku import Haiku from senju.haiku import Haiku
@ -47,13 +50,15 @@ def prompt_view():
title="Haiku generation" title="Haiku generation"
) )
@app.route("/generate_haiku", methods=['POST']) @app.route("/generate_haiku", methods=['POST'])
def generate_haiku(): def generate_haiku():
prompt = "a" prompt = "a"
print("Generation function")
if request.method == 'POST': if request.method == 'POST':
json_data = request.get_json() json_data = request.get_json()
prompt = json_data["prompt"] prompt = json_data["prompt"]
haiku = request_haiku(prompt) haiku = request_haiku(prompt)
id = store.save_haiku(haiku) id = store.save_haiku(haiku)
return str(id) return str(id)
else:
return "Method not allowed", 405

View file

@ -1,8 +1,10 @@
from typing import Optional from __future__ import annotations
from tinydb import TinyDB
from pathlib import Path
from logging import Logger
from logging import Logger
from pathlib import Path
from typing import Optional
from tinydb import TinyDB
from tinydb.queries import QueryImpl from tinydb.queries import QueryImpl
from senju.haiku import Haiku from senju.haiku import Haiku

View file

@ -72,4 +72,3 @@ document.getElementById("submit-btn").addEventListener("click", function() {
}); });
</script> </script>
{% endblock %} {% endblock %}

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import tempfile import tempfile
from pathlib import Path from pathlib import Path

View file

@ -1,5 +1,7 @@
# do not remove this import. This is needed for # do not remove this import. This is needed for
# pytest fixtures to work # pytest fixtures to work
from __future__ import annotations
import pytest # noqa: F401 import pytest # noqa: F401
from senju.haiku import Haiku from senju.haiku import Haiku

View file

@ -1,7 +1,10 @@
# I do not trust python and it's tests, so I'm testing them. May not be worth # I do not trust python and it's tests, so I'm testing them. May not be worth
# much, but at least it shows me a few things. # much, but at least it shows me a few things.
from __future__ import annotations
import os import os
# do not remove this import. This is needed for # do not remove this import. This is needed for
# pytest fixtures to work # pytest fixtures to work
import pytest # noqa: F401 import pytest # noqa: F401