Created
February 29, 2024 02:04
-
-
Save bmorphism/06244d25f458d655d1f1534911eb1bfe to your computer and use it in GitHub Desktop.
topos.bb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(require '[babashka.process :refer [shell]]) | |
(require '[cheshire.core :as json]) | |
(require '[clojure.java.io :as io]) | |
(require '[clojure.string :as str]) | |
(require '[babashka.http-client :as client]) | |
(import '(java.util Base64 UUID) | |
'(java.nio.file Files Paths) | |
'(java.text SimpleDateFormat) | |
'(java.util Date)) | |
(defn image-to-base64 [path] | |
(try | |
(let [file-path (Paths/get path (make-array String 0))] | |
(let [file-content (Files/readAllBytes file-path)] | |
(.encodeToString (Base64/getEncoder) file-content))) | |
(catch Exception e | |
(println "Error converting image to Base64:" (.getMessage e)) | |
nil))) ;; Return nil to indicate failure | |
(defn process-json-object [json-object] | |
(let [parsed-object (json/parse-string json-object true) | |
response-text (:response parsed-object)] | |
(doseq [char response-text] | |
(print char) | |
(flush)))) | |
(defn cogen [frame] | |
(let [response (client/post "https://alien-learning-vertically.ngrok.app/api/generate" | |
{:headers {"Content-Type" "application/json"} | |
:body (json/generate-string {:model "llava:34b" | |
:temperature 1.23 | |
:prompt "describe this image" | |
:images [(image-to-base64 frame)]}) | |
:as :stream | |
:throw false}) | |
stream (:body response)] | |
(with-open [reader (io/reader stream)] | |
(doseq [line (line-seq reader)] | |
;; Assuming each line is a complete JSON object | |
(process-json-object line))))) | |
(defn cogen-batch [frame] | |
(let [response (client/post "https://alien-learning-vertically.ngrok.app/api/generate" | |
{:headers {"Content-Type" "application/json"} | |
:body (json/generate-string {:model "llava:34b" | |
:temperature 1.23 | |
:prompt "describe this image" | |
:images [(image-to-base64 frame)]}) | |
:as :stream | |
:throw false})] | |
(println "Response:" response))) | |
(println "Welcome to topOS! 🦆") | |
(cogen "/Users/barton/marina/fuckit/src/xenomerica.png") ;; img2text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment