Compare commits
14 Commits
galleryApi
...
tmp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d18f173ef6 | ||
|
|
10a797d450 | ||
|
|
3a64479fd4 | ||
|
|
d4d2a32a71 | ||
|
|
e8c29025aa | ||
|
|
b909a85225 | ||
|
|
b69bc6200f | ||
|
|
6d03408495 | ||
|
|
552cfbe574 | ||
|
|
ed657df69a | ||
|
|
3b75e915e3 | ||
|
|
02cc60f824 | ||
|
|
c2ee394195 | ||
|
|
7dda27e86e |
@@ -2,12 +2,11 @@ const mongoose = require("mongoose");
|
||||
|
||||
const gameSchema = new mongoose.Schema({
|
||||
gameName: { type: String, required: true },
|
||||
userId: { type: String, required: true },
|
||||
childId: { type: String, required: true },
|
||||
gameID: { type: String, required: true },
|
||||
gameTime: { type: String, required: false },
|
||||
score: { type: String},
|
||||
gameStar: { type: Number},
|
||||
screenshotUrl: { type: String }, // Store the S3 URL of the screenshot
|
||||
});
|
||||
gameStar: { type: Number },
|
||||
screenshotUrl: { type: String },
|
||||
}, { collection: "gameData" });
|
||||
|
||||
module.exports = mongoose.model("Game", gameSchema);
|
||||
|
||||
@@ -2,7 +2,7 @@ const mongoose = require('mongoose');
|
||||
|
||||
// Define the GalleryImage schema
|
||||
const galleryImageSchema = new mongoose.Schema({
|
||||
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, // Assuming you have a User model
|
||||
childId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, // Assuming you have a User model
|
||||
gameName: { type: String, required: true },
|
||||
gameID: { type: String, required: true },
|
||||
screenshotUrl: { type: String, required: true },
|
||||
|
||||
@@ -2,7 +2,7 @@ const mongoose = require("mongoose");
|
||||
|
||||
const galleryImageScema = new mongoose.Schema({
|
||||
gameName: { type: String, required: true },
|
||||
userId: { type: String, required: true },
|
||||
childId: { type: String, required: true },
|
||||
gameID: { type: String, required: true },
|
||||
screenshotUrl: { type: String },
|
||||
});
|
||||
|
||||
@@ -1,29 +1,27 @@
|
||||
const AWS = require('aws-sdk');
|
||||
const axios = require('axios');
|
||||
const mongoose = require('mongoose');
|
||||
const Game = require('../../models/gameModel'); // Ensure this path matches your project structure
|
||||
|
||||
const FormData = require('form-data');
|
||||
const Game = require('../../models/gameModel');
|
||||
const aiEvaluateImageToStar = async (req, res) => {
|
||||
try {
|
||||
const { userId, gameName, gameID, gameTime, gameStar, screenShot } = req.body;
|
||||
const { childId, gameName, gameID, gameTime, gameStar, screenShot } = req.body;
|
||||
if (!screenShot) {
|
||||
return res.status(400).json({ error: 'Screenshot is required' });
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('image', screenShot);
|
||||
const screenshotUrl = await axios.post('https://teachertrainingchennai.in/api/uploadBase64/', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
})
|
||||
// .then(response => {
|
||||
// console.log('Response:', response.data);
|
||||
// })
|
||||
// .catch(error => {
|
||||
// console.error('Error:', error);
|
||||
// });
|
||||
console.log(screenshotUrl.data.filePath)
|
||||
// Upload screenshot
|
||||
const formData = new FormData();
|
||||
formData.append('image', screenShot);
|
||||
|
||||
const screenshotUploadResponse = await axios.post('https://teachertrainingchennai.in/api/uploadBase64/', formData, {
|
||||
headers: {
|
||||
...formData.getHeaders(),
|
||||
},
|
||||
});
|
||||
|
||||
const screenshotUrl = screenshotUploadResponse.data.filePath;
|
||||
// The coloring was done by a 5-year-old kid. Give a score between 1 to 10 in JSON format.
|
||||
const openAiPayload = {
|
||||
model: 'gpt-4o-mini',
|
||||
messages: [
|
||||
@@ -32,12 +30,12 @@ const aiEvaluateImageToStar = async (req, res) => {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'The coloring was done by a 5-year-old kid. Give a score between 1 to 10 in JSON format.',
|
||||
text: 'The coloring was done by a 5-year-old kid. Give a score between 1 to 10 the Respond ONLY with a valid JSON object like {"score": 7}. No extra text, no formatting, just raw JSON.',
|
||||
},
|
||||
{
|
||||
type: 'image_url',
|
||||
image_url: {
|
||||
url: screenshotUrl.data.filePath,
|
||||
url: screenshotUrl,
|
||||
},
|
||||
},
|
||||
],
|
||||
@@ -46,7 +44,6 @@ const aiEvaluateImageToStar = async (req, res) => {
|
||||
max_tokens: 300,
|
||||
};
|
||||
|
||||
console.log('AAAAAA')
|
||||
const openAiResponse = await axios.post(
|
||||
'https://api.openai.com/v1/chat/completions',
|
||||
openAiPayload,
|
||||
@@ -58,18 +55,27 @@ const aiEvaluateImageToStar = async (req, res) => {
|
||||
}
|
||||
);
|
||||
|
||||
// Extract the score from OpenAI's response
|
||||
const scoreResponse = openAiResponse.data.choices[0].message.content;
|
||||
const parsedScore = JSON.parse(scoreResponse);
|
||||
|
||||
console.log(openAiResponse)
|
||||
if (!openAiResponse.data.choices || openAiResponse.data.choices.length === 0) {
|
||||
throw new Error('Invalid OpenAI response format');
|
||||
}
|
||||
|
||||
let scoreResponse = openAiResponse.data.choices[0].message.content.trim();
|
||||
|
||||
if (scoreResponse.startsWith('```json')) {
|
||||
scoreResponse = scoreResponse.replace(/```json/, '').replace(/```/, '').trim();
|
||||
}
|
||||
|
||||
|
||||
const parsedScore = JSON.parse(scoreResponse);
|
||||
// console.log('Parsed Score:', parsedScore);
|
||||
|
||||
const gameData = new Game({
|
||||
gameName,
|
||||
userId,
|
||||
childId,
|
||||
gameID,
|
||||
gameTime,
|
||||
score: parsedScore.score || 'N/A', // Adjust key to match OpenAI response
|
||||
gameStar,
|
||||
gameStar: parsedScore.score || 'N/A',
|
||||
screenshotUrl,
|
||||
});
|
||||
|
||||
@@ -81,7 +87,7 @@ console.log(openAiResponse)
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
return res.status(500).json({ error: 'Something went wrong' });
|
||||
return res.status(500).json({ error: error.message || 'Something went wrong' });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ const GalleryImage = require('../../models/getGalleyImage');
|
||||
const getGalleryImage = async (req, res) => {
|
||||
try {
|
||||
// Extract query parameters for filtering (if provided)
|
||||
const { gameName, userId, gameID} = req.query;
|
||||
const { gameName, childId, gameID} = req.query;
|
||||
|
||||
// Build a filter object based on the query parameters
|
||||
const filter = {};
|
||||
@@ -12,15 +12,15 @@ const getGalleryImage = async (req, res) => {
|
||||
filter.gameName = gameName;
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
filter.userId = userId;
|
||||
if (childId) {
|
||||
filter.childId = childId;
|
||||
}
|
||||
if (gameID) {
|
||||
filter.gameID = gameID;
|
||||
}
|
||||
|
||||
// Fetch gallery images with filters and selected fields
|
||||
const galleryImages = await GalleryImage.find(filter).select('gameName userId gameID screenshotUrl'); // Select only these fields
|
||||
const galleryImages = await GalleryImage.find(filter).select('gameName childId gameID screenshotUrl'); // Select only these fields
|
||||
|
||||
if (!galleryImages.length) {
|
||||
return res.status(404).json({ message: 'No images found' });
|
||||
|
||||
@@ -15,15 +15,19 @@ async function main() {
|
||||
const collection = database.collection('gameData'); // Replace with your collection name
|
||||
|
||||
// Find a single document
|
||||
// const user = await collection.findOne({ userId: 'dsfdfgffgfgeg' });
|
||||
|
||||
const { userId, gameName, gameID } = req.body;
|
||||
const gameData = await collection.findOne({
|
||||
userId: userId,
|
||||
gameName: gameName,
|
||||
gameID: gameID
|
||||
});
|
||||
// const user = await collection.findOne({ childId: 'dsfdfgffgfgeg' });
|
||||
|
||||
const { childId, gameName, gameID } = req.body;
|
||||
const gameData = await collection.findOne(
|
||||
{
|
||||
childId: childId,
|
||||
gameName: gameName,
|
||||
gameID: gameID
|
||||
},{
|
||||
sort: {_id: -1}
|
||||
}
|
||||
);
|
||||
// console.log('gameData', gameData);
|
||||
if (gameData) {
|
||||
res.json({gameData});
|
||||
} else {
|
||||
|
||||
@@ -4,7 +4,7 @@ const FormData = require('form-data'); // Import form-data for Node.js
|
||||
|
||||
const saveGalleryImage = async (req, res) => {
|
||||
try {
|
||||
const { userId, gameName, gameID, screenShot } = req.body;
|
||||
const { childId, gameName, gameID, screenShot } = req.body;
|
||||
|
||||
// Validate base64 format
|
||||
const isValidBase64 = validateBase64(screenShot);
|
||||
@@ -41,12 +41,13 @@ const saveGalleryImage = async (req, res) => {
|
||||
throw new Error('Image upload failed: ' + uploadResponse.statusText);
|
||||
}
|
||||
|
||||
|
||||
// Extract and store the uploaded image URL from the response
|
||||
const screenshotUrl = uploadResponse.data.urls[0];
|
||||
|
||||
// Create and save gallery image data
|
||||
const galleryImageData = new GalleryImage({
|
||||
userId,
|
||||
childId,
|
||||
gameName,
|
||||
gameID,
|
||||
screenshotUrl,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const AWS = require("aws-sdk");
|
||||
const mongoose = require("mongoose");
|
||||
const Game = require("../../models/gameModel");
|
||||
|
||||
const s3 = new AWS.S3({
|
||||
@@ -7,35 +8,55 @@ const s3 = new AWS.S3({
|
||||
region: process.env.AWS_REGION,
|
||||
});
|
||||
|
||||
// MongoDB Connection
|
||||
const mongoURI = process.env.MONGODB_URL;
|
||||
const dbName = process.env.MONGO_DB_NAME;
|
||||
|
||||
mongoose.connect(mongoURI, {
|
||||
dbName: dbName,
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
})
|
||||
.then(() =>
|
||||
console.log("Connected to MongoDB"))
|
||||
.catch((err) => console.error("MongoDB connection error:", err));
|
||||
|
||||
const saveGameScore2 = async (req, res) => {
|
||||
const { userId, gameName, gameID, gameTime, score, gameStar, screenShot } = req.body;
|
||||
const { childId, gameName, gameID, gameTime, score, gameStar, screenShot } = req.body;
|
||||
|
||||
try {
|
||||
if (!gameName || !userId || !gameID) {
|
||||
if (!gameName || !childId || !gameID) {
|
||||
return res.status(400).json({ error: "Missing required fields" });
|
||||
}
|
||||
|
||||
const newGame = new Game({
|
||||
gameName,
|
||||
userId,
|
||||
childId,
|
||||
gameID,
|
||||
gameTime,
|
||||
score,
|
||||
gameStar,
|
||||
});
|
||||
|
||||
// Save to MongoDB
|
||||
const result = await newGame.save();
|
||||
// Upload screenshot to S3 if provided
|
||||
if (screenShot) {
|
||||
let base64Image = screenShot.split(";base64,").pop();
|
||||
const buffer = Buffer.from(base64Image, 'base64');
|
||||
// const buffer = Buffer.from(screenShot, "base64");
|
||||
let base64Image = screenShot.split(";base64,").pop();
|
||||
const buffer = Buffer.from(base64Image, "base64");
|
||||
|
||||
const uploadParams = {
|
||||
Bucket: process.env.S3_BUCKET_NAME,
|
||||
Key: `images/${result._id}.png`,
|
||||
Body: buffer,
|
||||
ContentType: "image/png",
|
||||
};
|
||||
|
||||
const s3Response = await s3.upload(uploadParams).promise();
|
||||
newGame.screenshotUrl = s3Response.Location;
|
||||
await newGame.save();
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
message: "Game data saved successfully",
|
||||
data: newGame,
|
||||
|
||||
@@ -31,9 +31,10 @@ const aiMarkDrawing = require("../api/aiMarkDrawing");
|
||||
const aiFeedbackOnReportWithFollowup = require("../api/aiFeedbackOnReportWithFollowup");
|
||||
const aiTextToSpeech = require("../api/aiTextToSpeech");
|
||||
const aiEvaluateImageToStar = require("../api/aiEvaluateImageToStar");
|
||||
// const getGameInfo = require("../api/getGameInfo");
|
||||
const saveGalleryImage = require("../api/saveGalleryImage");
|
||||
const getGalleryImage = require("../api/getGalleryImage");
|
||||
|
||||
// const aiTest = require("../api/aiTest");
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
@@ -193,8 +194,12 @@ router.get("/ping", (req, res) => {
|
||||
aiEvaluateImageToStar(req, res);
|
||||
});
|
||||
|
||||
// getGameInfo.
|
||||
// router.get("/getGameInfo", (req, res) => {
|
||||
// getGameInfo(req, res);
|
||||
// });
|
||||
|
||||
// Save Drawing Game Gallery Image
|
||||
// Save Drawing Game Gallery Image
|
||||
router.post("/saveGalleryImage", (req, res) => {
|
||||
saveGalleryImage(req, res);
|
||||
});
|
||||
@@ -204,6 +209,10 @@ router.get("/ping", (req, res) => {
|
||||
getGalleryImage(req, res);
|
||||
});
|
||||
|
||||
// Get Drawing Game Gallery Image
|
||||
// router.post("/aiTest", (req, res) => {
|
||||
// aiTest(req, res);
|
||||
// });
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user