Camera Live Docker - Code Review and Limerick

#1
by awacke1 - opened

This is working well.
image.png

Code Review as a Limerick:

// In the land of Node, so vast and so express, πŸš€
import express from "express";
// Dotenv's secrets, we do confess. πŸ—οΈ
import 'dotenv/config.js'
// HTTP's server, a foundation no less. πŸ—οΈ
import { createServer } from "http";
// Socket.IO, for real-time distress. πŸ“‘
import { Server } from "socket.io";
// Gradio client, AI's new dress. 🧠
import { client } from "@gradio/client";
// Require's magic, Node's old guess. πŸ§™β€β™‚οΈ
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url);
// EventSource global, for SSE's press. 🌐
global.EventSource = require('eventsource');

// Our app, an Express instance, no jest. πŸŽͺ
const app = express();
// HTTP server on top, wearing its best. 🎩
const httpServer = createServer(app);
// Serving static files, a public fest. πŸ“‚
app.use(express.static('public'));
// Socket.IO server, handling each request. πŸ’Œ
const io = new Server(httpServer, { /* options */ });

// When a connection is made, it's not just any selection. 🌟
io.on("connection", (socket) => {
// Announce each socket connection, a momentous section. πŸ“’
console.log("new socket connection");

// On 'ask_api', with intent and direction, πŸ›«
socket.on("ask_api", (client_data) => {
    // Log the data, a detailed inspection, πŸ”
    console.log(client_data)
    // Reaching out to the API, a connection affection. πŸ’ž
    console.log("trying to reach api");   
    asyncAPICall(client_data, socket)
});

});

// Testing servers, a cautious reflection, πŸ€”
async function test_servers(){
try{
// Gradio client test, an AI connection, πŸ€–
const grapi_test = await client("https://gradio-hello-world.hf.space");
// Predict and test, seeking perfection, ✨
const apitest_result = await grapi_test.predict("/predict", [
"John",
]);
// Success! A jubilant collection. πŸŽ‰
console.log(apitest_result);
}
catch(e){
// On error, we log, no deception. 🚫
console.log(e)
}

}

// Commented out, for now, no action. πŸ’€
//test_servers()

// Async API call, a complex transaction, 🏦
async function asyncAPICall(data, socket) {

// Gradio client, for model interaction, πŸ“²
const grapi = await client("fffiloni/mndrm-call");
try{
// Predicting with data, an AI faction, πŸ§ͺ
const api_result = await grapi.predict("/infer", [
data[0], // blob in 'image' Image component
data[1], // string in 'Question' Textbox component
]);
// Log and emit, a satisfactory reaction. 😁
console.log(api_result)
socket.emit("api_response", (api_result.data))
}
catch(e){
// On error, emit, a warning infraction. ⚠️
console.log(e)
socket.emit("api_error", ("ERROR ON API SIDE, SORRY..."))
}

}

// The server listens, a port declaration, πŸšͺ
httpServer.listen(7860);
// Running on localhost, a proud proclamation. πŸ“£
console.log("App running on localhost:7860")

Sign up or log in to comment