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,8 +1,12 @@
from __future__ import annotations
import json
import requests
from senju.haiku import Haiku
AI_BASE_URL: str = "http://ollama:11434/api"
AI_BASE_URL: str = "http://ollama:11434/api"
AI_GEN_ENDPOINT: str = "/generate"
AI_GEN_SYS_PROMPT = """
@ -42,10 +46,11 @@ 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"""
ai_gen_request = {
"model": "llama3.2:1b",
"prompt": f"{AI_GEN_SYS_PROMPT}{seed}",
@ -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
return haiku
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

@ -5,21 +5,21 @@
<div class="bg-white text-gray-900 p-8 rounded-xl shadow-lg max-w-lg w-full text-center transform transition duration-300 hover:scale-105">
<h1 class="text-3xl font-bold text-violet-700 mb-4">Very 1337 prompt input</h1>
<div class="flex flex-col gap-4">
<input
type="text"
id="user-input"
placeholder="Type your prompt here..."
<input
type="text"
id="user-input"
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"
/>
<button
id="submit-btn"
<button
id="submit-btn"
class="bg-violet-600 text-white font-bold py-3 px-6 rounded-lg text-lg shadow-md transition duration-300 hover:bg-violet-700 hover:scale-105"
>
🚀 Submit
</button>
</div>
</div>
<div id="response-box" class="mt-8 bg-white text-gray-900 p-6 rounded-lg shadow-lg max-w-lg w-full text-center opacity-0 transition-opacity duration-500 ease-in-out">
<h2 class="text-2xl font-semibold text-violet-700">Response:</h2>
<p id="ai-response" class="text-lg text-gray-700 mt-2 italic">Waiting for input...</p>
@ -72,4 +72,3 @@ document.getElementById("submit-btn").addEventListener("click", function() {
});
</script>
{% endblock %}