third part

This commit is contained in:
2026-01-30 17:55:29 +04:00
parent 7e9af29a42
commit 5211d954ca
4 changed files with 104 additions and 10 deletions

View File

@@ -63,11 +63,11 @@ async function pepe(){
console.log(response.text) console.log(response.text)
} }
async function shneine(){ async function shneine(fileName, mimeType){
const myfile = await ai.files.upload({ const myfile = await ai.files.upload({
file:'./pedik.ogg', file:`./uploads/${fileName}`,
config:{ config:{
mimeType:'audio/ogg', mimeType:mimeType,
} }
}); });
@@ -80,7 +80,8 @@ async function shneine(){
}) })
console.log(response.text) console.log(response.text)
} }
//await shneine();
async function fawatafa() { async function fawatafa() {
@@ -103,5 +104,3 @@ async function fawatafa() {
const fileName = 'outMarat5.wav'; const fileName = 'outMarat5.wav';
await saveWaveFile(fileName, audioBuffer); await saveWaveFile(fileName, audioBuffer);
} }
await fawatafa();

View File

@@ -3,9 +3,9 @@
"version": "1.0.0", "version": "1.0.0",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"dev": "nodemon src/server.js", "dev": "nodemon src/server.js",
"start": "node src/server.js", "start": "node src/server.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
@@ -14,9 +14,11 @@
"dependencies": { "dependencies": {
"@google/genai": "^1.38.0", "@google/genai": "^1.38.0",
"axios": "^1.13.2", "axios": "^1.13.2",
"cors": "^2.8.6",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"express": "^5.2.1", "express": "^5.2.1",
"jsonwebtoken": "^9.0.3", "jsonwebtoken": "^9.0.3",
"multer": "^2.0.2",
"node-telegram-bot-api": "^0.67.0", "node-telegram-bot-api": "^0.67.0",
"pg": "^8.17.2", "pg": "^8.17.2",
"wav": "^1.0.2" "wav": "^1.0.2"

86
src/routes/upload.js Normal file
View File

@@ -0,0 +1,86 @@
import { Router } from "express";
import multer from "multer";
import path from "path";
import fs from "fs";
import { authMiddleware } from "../middleware/auth.js";
import { GoogleGenAI, createUserContent, createPartFromUri } from "@google/genai";
import dotenv from "dotenv";
dotenv.config();
import wav from 'wav';
import { text } from "stream/consumers";
async function shneine(fileName, mimeType){
const myfile = await ai.files.upload({
file:`./uploads/${fileName}`,
config:{
mimeType:mimeType,
}
});
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
contents: createUserContent([
createPartFromUri(myfile.uri, myfile.mimeType),
'Нужно сделать анализ аудиофайла, краткую выжимку самого важного, вывести тезисы.'
])
})
return response.text;
}
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
const ai = new GoogleGenAI({apiKey: process.env.GEMINI_KEY});
const router = Router();
// создаём папку uploads, если её нет
const uploadDir = "uploads";
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir);
}
// настройка хранилища
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadDir);
},
filename: (req, file, cb) => {
const uniqueName =
Date.now() + "-" + Math.round(Math.random() * 1e9);
const ext = path.extname(file.originalname);
cb(null, uniqueName + ext);
},
});
const upload = multer({
storage,
limits: {
fileSize: 50 * 1024 * 1024, // 5 MB
},
});
// POST /upload
router.post(
"/",
//authMiddleware, // 🔒 если нужен Bearer token
upload.single("file"), // имя поля = file
async (req, res) => {
if (!req.file) {
return res.status(400).json({ message: "Файл не передан" });
}
const aiText = await shneine(req.file.filename, req.file.mimetype);
res.json({
message: "Файл загружен",
text: aiText,
});
}
);
export default router;

View File

@@ -1,23 +1,30 @@
import express from "express"; import express from "express";
import dotenv from "dotenv"; import dotenv from "dotenv";
import cors from 'cors'
import authRoutes from "./routes/auth.js"; import authRoutes from "./routes/auth.js";
import userRoutes from "./routes/user.js"; import userRoutes from "./routes/user.js";
import uploadRoutes from "./routes/upload.js";
dotenv.config(); dotenv.config();
const app = express(); const app = express();
app.use(express.json()); app.use(express.json());
app.use(cors());
app.use("/auth", authRoutes); app.use("/auth", authRoutes);
app.use("/user", userRoutes); app.use("/user", userRoutes);
app.use("/upload", uploadRoutes);
app.get("/health", (req, res) => { app.get("/health", (req, res) => {
res.json({ status: "ok" }); res.json({ status: "ok" });
}); });
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
app.listen(PORT, () => { const HOST = "0.0.0.0"
app.listen(PORT,"0.0.0.0", () => {
console.log(`🚀 Server started on http://localhost:${PORT}`); console.log(`🚀 Server started on http://localhost:${PORT}`);
}); });