Spaces:
Sleeping
Sleeping
File size: 1,817 Bytes
f9f0fec 2767dda f9f0fec 2767dda 9a70daf f9f0fec 2767dda f9f0fec 2767dda 9a70daf 2767dda f9f0fec 2767dda 9a70daf 2767dda f9f0fec 2767dda f3c1998 f9f0fec 2767dda |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import express from "express";
import 'dotenv/config.js';
import { createServer } from "http";
import { Server } from "socket.io";
import { client as gradioClient } from "@gradio/client";
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
global.EventSource = require('eventsource');
const app = express();
const httpServer = createServer(app);
app.use(express.static('public'));
const io = new Server(httpServer);
// ๐ Server Initialization
httpServer.listen(7860, () => {
console.log("App running on localhost:7860");
});
// ๐ Socket Connection Handler
io.on("connection", (socket) => {
console.log("New socket connection");
// ๐ก API Request Listener
socket.on("ask_api", (clientData) => {
console.log("Received client data:", clientData);
console.log("Contacting API...");
handleAPICall(clientData, socket);
});
});
// ๐ API Call Function
async function handleAPICall(data, socket) {
const grapi = await gradioClient("fffiloni/mndrm-call");
try {
const apiResult = await grapi.predict("/infer", [data[0], data[1]]);
console.log("API Result:", apiResult);
socket.emit("api_response", apiResult.data);
} catch (error) {
console.error("API Error:", error);
socket.emit("api_error", "ERROR ON API SIDE, SORRY...");
}
}
// ๐งช Optional Server Test Function
// Uncomment to test server connections
/*
async function testServers() {
try {
const grapiTest = await gradioClient("https://gradio-hello-world.hf.space");
const apiTestResult = await grapiTest.predict("/predict", ["John"]);
console.log("Test API Result:", apiTestResult);
} catch (error) {
console.error("Test Error:", error);
}
}
// testServers();
*/
|