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 requests
from senju.haiku import Haiku
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:
"""
def request_haiku(seed: str) -> Haiku:
"""This function prompts the ai to generate
the hauku based on the user input"""
@ -54,12 +59,17 @@ def request_haiku(seed: str) -> Haiku:
while True:
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"])
haiku = Haiku([ai_response["line1"], ai_response["line2"], ai_response["line3"]])
break;
except:
pass
haiku = Haiku(
[
ai_response["line1"],
ai_response["line2"],
ai_response["line3"]
])
break
except json.JSONDecodeError:
continue
return haiku

View file

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

View file

@ -1,5 +1,8 @@
from __future__ import annotations
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.haiku import Haiku
@ -47,13 +50,15 @@ def prompt_view():
title="Haiku generation"
)
@app.route("/generate_haiku", methods=['POST'])
def generate_haiku():
prompt = "a"
print("Generation function")
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)
haiku = request_haiku(prompt)
id = store.save_haiku(haiku)
return str(id)
else:
return "Method not allowed", 405

View file

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

View file

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

View file

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

View file

@ -1,5 +1,7 @@
# do not remove this import. This is needed for
# pytest fixtures to work
from __future__ import annotations
import pytest # noqa: F401
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
# much, but at least it shows me a few things.
from __future__ import annotations
import os
# do not remove this import. This is needed for
# pytest fixtures to work
import pytest # noqa: F401