Spaces:
Sleeping
Sleeping
// Import necessary libraries | |
import express from "express"; | |
import 'dotenv/config.js' | |
import { createServer } from "http"; | |
import { Server } from "socket.io"; | |
import { client } from "@gradio/client"; | |
import { createRequire } from 'node:module'; | |
import fs from 'fs'; | |
import path from 'path'; | |
const require = createRequire(import.meta.url); | |
global.EventSource = require('eventsource'); | |
// Initialize the Express app and HTTP server | |
const app = express(); | |
const httpServer = createServer(app); | |
// Serve static files from 'public' directory | |
app.use(express.static('public')); | |
// Initialize Socket.IO for real-time web socket communication | |
const io = new Server(httpServer, {}); | |
// Directory for saving inference files | |
const outputDir = 'inference_outputs'; | |
if (!fs.existsSync(outputDir)){ | |
fs.mkdirSync(outputDir); | |
} | |
// Handle new socket connection | |
io.on("connection", (socket) => { | |
console.log("π New socket connection"); | |
// Listen for 'ask_api' events from the client | |
socket.on("ask_api", (client_data) => { | |
console.log("πΈ Received data from client"); | |
asyncAPICall(client_data, socket); | |
}); | |
}); | |
// Example function to test server communication with a Gradio API | |
async function test_servers(){ | |
try{ | |
const grapi_test = await client("https://gradio-hello-world.hf.space"); | |
const apitest_result = await grapi_test.predict("/predict", ["John"]); | |
console.log(apitest_result); | |
} | |
catch(e){ | |
console.log("β Error testing servers:", e); | |
} | |
} | |
// Function to call the Gradio API asynchronously | |
async function asyncAPICall(data, socket) { | |
const grapi = await client("fffiloni/mndrm-call"); | |
try{ | |
// Perform the API call with the provided image blob and question | |
const api_result = await grapi.predict("/infer", [data[0], data[1]]); | |
console.log("β API Result:", api_result); | |
// Emit the API response back to the client | |
socket.emit("api_response", api_result.data); | |
// Save the inference input and output | |
saveInference(data, api_result.data); | |
} | |
catch(e){ | |
console.log("β API Call Error:", e); | |
socket.emit("api_error", "ERROR ON API SIDE, SORRY..."); | |
} | |
} | |
// Save the inference input (image and question) and output (API response) to files | |
function saveInference(data, apiResponse) { | |
const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); | |
const basePath = path.join(outputDir, timestamp); | |
// Save image | |
const imageData = data[0].split(',')[1]; // Assuming data[0] is a base64 image string | |
fs.writeFileSync(`${basePath}_image.png`, imageData, 'base64'); | |
// Save question and API response in a text file | |
const textContent = `Question: ${data[1]}\nAPI Response: ${apiResponse}`; | |
fs.writeFileSync(`${basePath}_info.txt`, textContent); | |
console.log("π Inference data saved:", basePath); | |
} | |
// Start the HTTP server | |
httpServer.listen(7860, () => console.log("π App running on localhost:7860")); | |