mirror of
https://github.com/senju1337/senju.git
synced 2025-12-23 23:39:27 +00:00
feat: Add Method not Allowed handling for generate API and imporove error handling
Refs: OPS-12
This commit is contained in:
parent
0243091b76
commit
edf145c8de
8 changed files with 53 additions and 28 deletions
|
|
@ -1,8 +1,12 @@
|
||||||
|
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"
|
||||||
AI_GEN_ENDPOINT: str = "/generate"
|
AI_GEN_ENDPOINT: str = "/generate"
|
||||||
|
|
||||||
AI_GEN_SYS_PROMPT = """
|
AI_GEN_SYS_PROMPT = """
|
||||||
|
|
@ -42,10 +46,11 @@ 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"""
|
||||||
|
|
||||||
ai_gen_request = {
|
ai_gen_request = {
|
||||||
"model": "llama3.2:1b",
|
"model": "llama3.2:1b",
|
||||||
"prompt": f"{AI_GEN_SYS_PROMPT}{seed}",
|
"prompt": f"{AI_GEN_SYS_PROMPT}{seed}",
|
||||||
|
|
@ -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"]
|
||||||
return haiku
|
])
|
||||||
|
break
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
return haiku
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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">
|
<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>
|
<h1 class="text-3xl font-bold text-violet-700 mb-4">Very 1337 prompt input</h1>
|
||||||
<div class="flex flex-col gap-4">
|
<div class="flex flex-col gap-4">
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
id="user-input"
|
id="user-input"
|
||||||
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"
|
||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
id="submit-btn"
|
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"
|
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
|
🚀 Submit
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</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">
|
<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>
|
<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>
|
<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>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import tempfile
|
import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue