-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from SLIIT-24-25J-047-Research/IT21319792-Bosh…
…itha_Gunarathna It21319792 boshitha gunarathna
- Loading branch information
Showing
16 changed files
with
395 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
const FormData = require('form-data'); | ||
const fs = require('fs'); | ||
const ffmpeg = require('fluent-ffmpeg'); | ||
const axios = require('axios'); | ||
const path = require('path'); | ||
const Question = require('../models/employer/Question'); | ||
|
||
|
||
exports.processAudio = async (req, res) => { | ||
try { | ||
// Check if audio file is provided | ||
if (!req.file) { | ||
console.error('No audio file provided.'); | ||
return res.status(400).json({ | ||
success: false, | ||
message: 'Audio file not provided', | ||
}); | ||
} | ||
|
||
console.log('Audio file received:', { | ||
originalname: req.file.originalname, | ||
mimetype: req.file.mimetype, | ||
size: req.file.size, | ||
path: req.file.path, | ||
}); | ||
|
||
// Check if questionId is provided | ||
if (!req.body.questionId) { | ||
console.error('No question ID provided.'); | ||
return res.status(400).json({ | ||
success: false, | ||
message: 'Question ID not provided', | ||
}); | ||
} | ||
|
||
const questionId = req.body.questionId; | ||
|
||
// Fetch the question to get the actual answer | ||
const question = await Question.findById(questionId); | ||
|
||
if (!question) { | ||
console.error(`Question not found for ID: ${questionId}`); | ||
return res.status(404).json({ | ||
success: false, | ||
message: 'Question not found', | ||
}); | ||
} | ||
|
||
console.log('Question retrieved successfully:', question); | ||
|
||
|
||
const filePath = req.file.path; | ||
const tempFilePath = path.join(__dirname, 'temp_audio.wav'); | ||
|
||
|
||
ffmpeg(filePath) | ||
.toFormat('wav') | ||
.on('start', (commandLine) => { | ||
console.log('FFmpeg command:', commandLine); | ||
}) | ||
.on('progress', (progress) => { | ||
console.log('Conversion progress:', progress); | ||
}) | ||
.on('end', async () => { | ||
console.log('Audio conversion completed.'); | ||
|
||
|
||
const form = new FormData(); | ||
form.append('audio', fs.createReadStream(tempFilePath)); | ||
|
||
console.log('FormData prepared for microservice.'); | ||
|
||
try { | ||
// transcription | ||
const response = await axios.post('http://voice-confidence:3000/transcribe', form, { | ||
headers: { | ||
...form.getHeaders(), | ||
}, | ||
}); | ||
|
||
|
||
const transcription = response.data.candidate_answer; | ||
|
||
console.log('Transcription result:', transcription); | ||
|
||
fs.unlink(tempFilePath, (err) => { | ||
if (err) { | ||
console.error('Error deleting converted file:', err.message); | ||
} else { | ||
console.log('Converted file deleted.'); | ||
} | ||
}); | ||
|
||
|
||
fs.unlink(filePath, (err) => { | ||
if (err) { | ||
console.error('Error deleting uploaded file:', err.message); | ||
} else { | ||
console.log('Uploaded file deleted.'); | ||
} | ||
}); | ||
|
||
// compare the transcribed answer with the actual answers | ||
const actualAnswers = question.answers; | ||
const comparisonResponse = await axios.post('http://voice-confidence:3000/compare', { | ||
candidate_answer: transcription, | ||
actual_answer: question.answers, | ||
}); | ||
|
||
|
||
const { similarity_scores, is_correct } = comparisonResponse.data; | ||
|
||
console.log('Similarity Scores:', similarity_scores); | ||
console.log('Is the answer correct?', is_correct); | ||
|
||
|
||
return res.status(200).json({ | ||
success: true, | ||
transcription: transcription, | ||
similarity: similarity_scores, | ||
isCorrect: is_correct, | ||
}); | ||
} catch (error) { | ||
console.error('Error in Flask response:', error.message); | ||
return res.status(500).json({ | ||
success: false, | ||
message: 'Error in processing transcription', | ||
}); | ||
} | ||
}) | ||
.on('error', (err) => { | ||
console.error('Error during conversion:', err.message); | ||
return res.status(500).json({ | ||
success: false, | ||
message: 'Error in converting audio file', | ||
error: err.message, | ||
}); | ||
}) | ||
.save(tempFilePath); | ||
} catch (error) { | ||
console.error('Unexpected error:', error.message); | ||
return res.status(500).json({ | ||
success: false, | ||
message: 'An unexpected error occurred', | ||
error: error.message, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const express = require('express'); | ||
const multer = require('multer'); | ||
const path = require('path'); | ||
const { processAudio } = require('../controllers/audioController'); | ||
|
||
const router = express.Router(); | ||
|
||
const storage = multer.diskStorage({ | ||
destination: function (req, file, cb) { | ||
cb(null, 'uploads'); | ||
}, | ||
filename: function (req, file, cb) { | ||
cb(null, Date.now() + path.extname(file.originalname)); | ||
} | ||
}); | ||
|
||
const upload = multer({ storage: storage }); | ||
|
||
|
||
router.post('/audio', upload.single('audio'), processAudio); | ||
|
||
module.exports = router; |
Oops, something went wrong.