File size: 3,057 Bytes
325f41a
f9f0fec
a227ed8
f9f0fec
2767dda
a227ed8
325f41a
 
 
 
f9f0fec
 
 
325f41a
f9f0fec
 
325f41a
 
f9f0fec
9a70daf
325f41a
 
 
 
 
 
 
 
 
 
f9f0fec
325f41a
a227ed8
325f41a
a227ed8
325f41a
 
f9f0fec
 
 
325f41a
a227ed8
325f41a
 
 
 
 
 
 
 
f3c1998
f9f0fec
325f41a
a227ed8
325f41a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2767dda
a227ed8
325f41a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a227ed8
325f41a
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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"));