Spaces:
Sleeping
Camera Live Docker - Code Review and Limerick
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")