From 2c1e1e1488f99fb7ad3e9e6ce88ee1ca24425d2b Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Sun, 23 Mar 2025 18:12:19 +0100 Subject: [PATCH] feat: generate haiku from image Regs: OPS-85 OPS-84 --- senju/static/js/scan.js | 36 ++++++----- senju/templates/prompt.html | 117 +++++++++++++++++++----------------- senju/templates/scan.html | 4 +- 3 files changed, 87 insertions(+), 70 deletions(-) diff --git a/senju/static/js/scan.js b/senju/static/js/scan.js index 8a0275a..6faff12 100644 --- a/senju/static/js/scan.js +++ b/senju/static/js/scan.js @@ -11,6 +11,7 @@ const yesButton = document.getElementById("yes-button"); const noButton = document.getElementById("no-button"); const generatingHaikuBox = document.getElementById("generating-haiku-box"); const generatedHaikuBox = document.getElementById("generated-haiku-box"); +let haiku_prompt = ""; let imageUploaded = false; function handleFileSelect(event) { @@ -80,10 +81,10 @@ function handleSubmit() { }) .then((data) => { // Extract top result and display it - if (data.results && data.results.length > 0) { - const topResult = data.results[0]; + if (data.description) { + haiku_prompt = data.description; document.getElementById("ai-response").textContent = - `${topResult.label} (${Math.round(topResult.confidence * 100)}% confidence)`; + `Recognized: ${haiku_prompt}`; } else { document.getElementById("ai-response").textContent = "Could not identify the image"; @@ -102,23 +103,30 @@ function handleSubmit() { }, 600); } } - function handleYesClick() { // Hide response box responseBox.classList.add("opacity-0"); - // Show generating haiku box first - setTimeout(() => { - responseBox.classList.add("hidden"); - generatingHaikuBox.classList.remove("hidden"); + responseBox.textContent = "🤖 AI is thinking..."; + responseBox.classList.remove("opacity-0"); - // After a delay, hide generating box and show result - setTimeout(() => { - generatingHaikuBox.classList.add("hidden"); - generatedHaikuBox.classList.remove("hidden"); - }, 3000); // Show loading animation for 3 seconds before revealing the haiku - }, 500); // Wait for response box fade out + fetch("/api/v1/haiku", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ prompt: haiku_prompt }), + }) + .then((response) => response.text()) + .then((data) => { + let id = parseInt(data, 10); + window.location.href = "/haiku/" + id; + }) + .catch((error) => { + responseBox.textContent = "Error: " + error.message; + }); } + function handleNoClick() { // Reset everything removeImage(); diff --git a/senju/templates/prompt.html b/senju/templates/prompt.html index cf1aa73..46befeb 100644 --- a/senju/templates/prompt.html +++ b/senju/templates/prompt.html @@ -1,35 +1,44 @@ -{% extends "base.html" %} - -{% block content %} -
-
-

Very 1337 prompt input

-
- - -
+{% extends "base.html" %} {% block content %} +
+
+

+ Very 1337 prompt input +

+
+ +
+
-
-

Response:

-

Waiting for input...

-
+
+

Response:

+

+ Waiting for input... +

+
{% endblock %} diff --git a/senju/templates/scan.html b/senju/templates/scan.html index 6853afe..433b692 100644 --- a/senju/templates/scan.html +++ b/senju/templates/scan.html @@ -97,14 +97,14 @@