feat: add fetch image
Some checks failed
ms-devops/pipeline/head There was a failure building this commit
Some checks failed
ms-devops/pipeline/head There was a failure building this commit
This commit is contained in:
parent
1cb586f55a
commit
6794b01ac8
91
server/routers/dry-wash/image.js
Normal file
91
server/routers/dry-wash/image.js
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
const API_URL = "https://gigachat.devices.sberbank.ru/api/v1"
|
||||||
|
const router = require('express').Router()
|
||||||
|
const { v4: uuidv4 } = require("uuid");
|
||||||
|
|
||||||
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
|
||||||
|
|
||||||
|
const token = 'MTQwMmNmZjgtZjA5OC00OGMxLWI0OTUtNWU3ZTU4YzMzZjdjOmU5OGFiYmNiLThmMDItNGVmOC1hNjhhLTA4Y2QxYjVmOGRmMA=='
|
||||||
|
|
||||||
|
|
||||||
|
const getToken = async (req, res) => {
|
||||||
|
|
||||||
|
const rqUID = uuidv4()
|
||||||
|
const body = new URLSearchParams({
|
||||||
|
scope: "GIGACHAT_API_PERS",
|
||||||
|
})
|
||||||
|
|
||||||
|
const response = await fetch("https://ngw.devices.sberbank.ru:9443/api/v2/oauth", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Basic ${token}`,
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
Accept: "application/json",
|
||||||
|
RqUID: rqUID,
|
||||||
|
},
|
||||||
|
body,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json();
|
||||||
|
console.error(" Ошибка запроса:", errorData);
|
||||||
|
return res.status(response.status).json(errorData);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function analyzeImage(fileId,token) {
|
||||||
|
const response = await fetch(`${API_URL}/chat/completions`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: "GigaChat-Max",
|
||||||
|
stream: false,
|
||||||
|
update_interval: 0,
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: "system",
|
||||||
|
content:
|
||||||
|
"Ты эксперт по оценке чистоты автомобилей. Твоя задача — анализировать фотографии машин и определять степень их чистоты по 10-балльной шкале, где 1 — очень грязная, 10 — полностью чистая. Отвечай только числом от 1 до 10, без пояснений и дополнительных слов.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: "Что с чистотой машины? Отвечай на основе приложенного документа",
|
||||||
|
attachments: [fileId],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json()
|
||||||
|
console.log(" Результат анализа:", data)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
router.post("/upload", async (req, res) => {
|
||||||
|
|
||||||
|
const {access_token} = await getToken(req, res)
|
||||||
|
|
||||||
|
const response = await fetch(`${API_URL}/files`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${access_token}`,
|
||||||
|
contentType: "multipart/form-data",
|
||||||
|
},
|
||||||
|
body: req.body,
|
||||||
|
})
|
||||||
|
|
||||||
|
const data = await response.json()
|
||||||
|
|
||||||
|
const analysisResponse = await analyzeImage(data.id,access_token)
|
||||||
|
|
||||||
|
res.json({ fileId: data.id, analysis: analysisResponse })
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = router
|
@ -2,11 +2,13 @@ const router = require('express').Router()
|
|||||||
const armMasterRouter = require('./arm-master')
|
const armMasterRouter = require('./arm-master')
|
||||||
const armOrdersRouter = require('./arm-orders')
|
const armOrdersRouter = require('./arm-orders')
|
||||||
const orderRouter = require('./order')
|
const orderRouter = require('./order')
|
||||||
|
const imageRouter = require('./image')
|
||||||
|
|
||||||
|
|
||||||
router.use('/arm', armMasterRouter)
|
router.use('/arm', armMasterRouter)
|
||||||
router.use('/arm', armOrdersRouter)
|
router.use('/arm', armOrdersRouter)
|
||||||
router.use('/order', orderRouter)
|
router.use('/order', orderRouter)
|
||||||
|
router.use('/image', imageRouter)
|
||||||
|
|
||||||
|
|
||||||
module.exports = router
|
module.exports = router
|
||||||
|
Loading…
Reference in New Issue
Block a user