yalsaffar commited on
Commit
23761b4
1 Parent(s): c589ea6

Updated Dockerfile for Hugging Face Spaces deployment

Browse files
Files changed (1) hide show
  1. app/server.js +8 -17
app/server.js CHANGED
@@ -10,17 +10,8 @@ const app = express();
10
  const port = 3000;
11
 
12
  const uploadsDir = path.join(__dirname, 'uploads');
13
- const publicDir = path.join(__dirname, 'public');
14
- const audioDir = path.join(publicDir, 'audio');
15
-
16
  if (!fs.existsSync(uploadsDir)) {
17
- fs.mkdirSync(uploadsDir);
18
- }
19
- if (!fs.existsSync(publicDir)) {
20
- fs.mkdirSync(publicDir);
21
- }
22
- if (!fs.existsSync(audioDir)) {
23
- fs.mkdirSync(audioDir);
24
  }
25
 
26
  const storage = multer.memoryStorage();
@@ -50,7 +41,6 @@ app.post('/save-audio', upload.single('audio'), async (req, res) => {
50
  const transcriptionPath = path.join(folderPath, `transcription_${sentenceIndex}.txt`);
51
 
52
  fs.writeFileSync(rawAudioPath, req.file.buffer);
53
-
54
  fs.writeFileSync(transcriptionPath, req.body.transcript);
55
 
56
  const ffmpegCommand = `ffmpeg -i ${rawAudioPath} -ar 44100 -ac 1 ${wavAudioPath}`;
@@ -78,11 +68,12 @@ app.post('/save-audio', upload.single('audio'), async (req, res) => {
78
  if (response.ok) {
79
  const result = await response.json();
80
  console.log(result);
81
- const generatedAudioPath = path.join(audioDir, path.basename(result.audio_path));
82
- fs.renameSync(result.audio_path, generatedAudioPath); // Move the file to the public directory
83
- audioPaths.push(`/audio/${path.basename(generatedAudioPath)}`);
84
  sentenceIndex++;
85
- res.status(200).json({ audio_path: `/audio/${path.basename(generatedAudioPath)}`, translation: result.translation });
 
 
 
86
  } else {
87
  console.error('Failed to process the file via FastAPI');
88
  res.status(500).send('Failed to process the file via FastAPI');
@@ -96,7 +87,7 @@ app.post('/save-audio', upload.single('audio'), async (req, res) => {
96
 
97
  app.get('/concatenate-audio', (req, res) => {
98
  const folderPath = path.join(uploadsDir, getNextFolderNumber().toString());
99
- const finalAudioPath = path.join(audioDir, 'final_audio.wav');
100
  const concatCommand = `ffmpeg -y -i "concat:${audioPaths.join('|')}" -acodec copy ${finalAudioPath}`;
101
  exec(concatCommand, (concatError, concatStdout, concatStderr) => {
102
  if (concatError) {
@@ -104,7 +95,7 @@ app.get('/concatenate-audio', (req, res) => {
104
  return res.status(500).send('Error concatenating audio files');
105
  }
106
 
107
- res.status(200).json({ audio_path: `/audio/final_audio.wav` });
108
  });
109
  });
110
 
 
10
  const port = 3000;
11
 
12
  const uploadsDir = path.join(__dirname, 'uploads');
 
 
 
13
  if (!fs.existsSync(uploadsDir)) {
14
+ fs.mkdirSync(uploadsDir, { recursive: true });
 
 
 
 
 
 
15
  }
16
 
17
  const storage = multer.memoryStorage();
 
41
  const transcriptionPath = path.join(folderPath, `transcription_${sentenceIndex}.txt`);
42
 
43
  fs.writeFileSync(rawAudioPath, req.file.buffer);
 
44
  fs.writeFileSync(transcriptionPath, req.body.transcript);
45
 
46
  const ffmpegCommand = `ffmpeg -i ${rawAudioPath} -ar 44100 -ac 1 ${wavAudioPath}`;
 
68
  if (response.ok) {
69
  const result = await response.json();
70
  console.log(result);
71
+ audioPaths.push(result.audio_path);
 
 
72
  sentenceIndex++;
73
+ // Serve the audio file from the public directory
74
+ const publicAudioPath = path.join(__dirname, 'public', path.basename(result.audio_path));
75
+ fs.copyFileSync(result.audio_path, publicAudioPath);
76
+ res.status(200).json({ audio_path: `/public/${path.basename(result.audio_path)}`, translation: result.translation });
77
  } else {
78
  console.error('Failed to process the file via FastAPI');
79
  res.status(500).send('Failed to process the file via FastAPI');
 
87
 
88
  app.get('/concatenate-audio', (req, res) => {
89
  const folderPath = path.join(uploadsDir, getNextFolderNumber().toString());
90
+ const finalAudioPath = path.join(folderPath, 'final_audio.wav');
91
  const concatCommand = `ffmpeg -y -i "concat:${audioPaths.join('|')}" -acodec copy ${finalAudioPath}`;
92
  exec(concatCommand, (concatError, concatStdout, concatStderr) => {
93
  if (concatError) {
 
95
  return res.status(500).send('Error concatenating audio files');
96
  }
97
 
98
+ res.status(200).json({ audio_path: finalAudioPath });
99
  });
100
  });
101