Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
110 lines (96 loc) · 2.2 KB
/
Copy pathserver.js
File metadata and controls
110 lines (96 loc) · 2.2 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const express = require("express");
const dotenv = require("dotenv");
const path = require("path");
const { fal } = require("@fal-ai/client");
const {
getMoisture,
getConductivity,
getTemperature,
getLight,
} = require("./js/functions.cjs");
dotenv.config({
quiet: true,
});
const EXPRESS_PORT = 3000;
const FAL_QWEN_IMAGE_EDIT = "fal-ai/qwen-image-edit";
const VOLUME_FOLDER = path.join(__dirname, "volume");
const app = express();
app.use(express.json({ limit: "10mb" }));
app.use("/volume", express.static(VOLUME_FOLDER));
fal.config({
credentials: process.env.FAL_KEY,
});
app.get("/sensor", (_, res) => {
const fn = (num) => +num.toFixed(2);
const moisture = getMoisture(fn);
const conductivity = getConductivity(fn);
const temperature = getTemperature(fn);
const light = getLight(fn);
res.json({
moisture: {
value: moisture,
unit: "%",
},
conductivity: {
value: conductivity,
unit: "mS/cm",
},
temperature: {
value: temperature,
unit: "°C",
},
light: {
value: light,
unit: "Lux",
},
});
});
app.post("/fal", async (req, res) => {
try {
const { prompt, image_url } = req.body;
const { fal_webhook } = req.query;
if (!prompt || !image_url) {
return res.status(400).json({ error: "/fal: Missing parameters" });
}
const { data, requestId } = await fal.subscribe(FAL_QWEN_IMAGE_EDIT, {
input: { prompt, image_url },
onQueueUpdate: async (update) => {
const {
status,
request_id,
response_url,
status_url,
cancel_url,
metrics,
} = update;
switch (status) {
case "IN_QUEUE":
break;
case "IN_PROGRESS":
console.log("In progress ...", request_id);
break;
case "COMPLETED":
console.log("Inference time: ", metrics.inference_time);
break;
}
},
});
if (fal_webhook) {
await fetch(fal_webhook, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
data,
requestId,
}),
});
}
res.json({ data, requestId });
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
});
app.listen(EXPRESS_PORT, () => {
console.log(`Server running at http://localhost:${EXPRESS_PORT}`);
});