Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- Dockerfile +24 -0
- index.html +202 -0
- main.py +94 -0
- requirements.txt +7 -0
- run.sh +1 -0
Dockerfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.8-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the current directory contents into the container at /app
|
| 8 |
+
COPY . /app
|
| 9 |
+
|
| 10 |
+
# Install any needed packages specified in requirements.txt
|
| 11 |
+
RUN apt-get update && \
|
| 12 |
+
apt-get install -y ffmpeg portaudio19-dev && \
|
| 13 |
+
rm -rf /var/lib/apt/lists/* && \
|
| 14 |
+
pip install --no-cache-dir -r requirements.txt && \
|
| 15 |
+
pip install python-multipart
|
| 16 |
+
|
| 17 |
+
# Make port 7860 available to the world outside this container
|
| 18 |
+
EXPOSE 7860
|
| 19 |
+
|
| 20 |
+
# Define environment variable for FastAPI
|
| 21 |
+
ENV PYTHONUNBUFFERED 1
|
| 22 |
+
|
| 23 |
+
# Command to run the application
|
| 24 |
+
CMD ["uvicorn", "soundscripter_fastAPI:app", "--host", "0.0.0.0", "--port", "7860"]
|
index.html
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Speech-to-Text Translator</title>
|
| 5 |
+
<!-- Add Bootstrap CSS link here -->
|
| 6 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css">
|
| 7 |
+
<style>
|
| 8 |
+
* {
|
| 9 |
+
margin: 0;
|
| 10 |
+
padding: 0;
|
| 11 |
+
box-sizing: border-box;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
body {
|
| 15 |
+
font-family: Arial, sans-serif;
|
| 16 |
+
background-color: #f8f9fa;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
#header {
|
| 20 |
+
background-color: #4D2B8F;
|
| 21 |
+
color: #fff;
|
| 22 |
+
padding: 20px;
|
| 23 |
+
text-align: center;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
#title {
|
| 27 |
+
font-size: 24px;
|
| 28 |
+
margin-bottom: 10px;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
#subtitle {
|
| 32 |
+
font-size: 20px;
|
| 33 |
+
font-weight: 500;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
.container {
|
| 37 |
+
max-width: 600px;
|
| 38 |
+
margin: 20px auto;
|
| 39 |
+
background-color: #fff;
|
| 40 |
+
border-radius: 8px;
|
| 41 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
|
| 42 |
+
padding: 20px;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
.form-group {
|
| 46 |
+
margin-bottom: 20px;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
.btn {
|
| 50 |
+
width: 100%;
|
| 51 |
+
padding: 10px;
|
| 52 |
+
font-size: 18px;
|
| 53 |
+
font-weight: bold;
|
| 54 |
+
border-radius: 5px;
|
| 55 |
+
cursor: pointer;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
#outputText {
|
| 59 |
+
width: 100%;
|
| 60 |
+
min-height: 100px;
|
| 61 |
+
padding: 10px;
|
| 62 |
+
font-size: 16px;
|
| 63 |
+
border-radius: 5px;
|
| 64 |
+
resize: vertical;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
</style>
|
| 68 |
+
</head>
|
| 69 |
+
<body>
|
| 70 |
+
|
| 71 |
+
<div id="header">
|
| 72 |
+
<h1 id="title">Automatic Speech Recognition</h1>
|
| 73 |
+
<p id="subtitle">Group 27</p>
|
| 74 |
+
</div>
|
| 75 |
+
|
| 76 |
+
<div class="container">
|
| 77 |
+
<div class="form-group">
|
| 78 |
+
<label for="audioUpload">Upload Audio File:</label>
|
| 79 |
+
<input type="file" class="form-control" id="audioUpload">
|
| 80 |
+
</div>
|
| 81 |
+
|
| 82 |
+
<div class="form-group">
|
| 83 |
+
<label for="languageSelect">Select Language:</label>
|
| 84 |
+
<select class="form-control" id="languageSelect">
|
| 85 |
+
<option value="en">English</option>
|
| 86 |
+
</select>
|
| 87 |
+
</div>
|
| 88 |
+
|
| 89 |
+
<div class="form-group">
|
| 90 |
+
<button class="btn btn-primary" id="recordButton">Start Recording</button>
|
| 91 |
+
<button class="btn btn-secondary" id="submitButton">Transcribe</button>
|
| 92 |
+
</div>
|
| 93 |
+
|
| 94 |
+
<div class="form-group">
|
| 95 |
+
<textarea class="form-control" id="outputText" rows="5" readonly style="display: none;"></textarea>
|
| 96 |
+
</div>
|
| 97 |
+
</div>
|
| 98 |
+
|
| 99 |
+
<!-- Add Bootstrap JS link and any other required scripts here -->
|
| 100 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
|
| 101 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.min.js"></script>
|
| 102 |
+
<script>
|
| 103 |
+
let isRecording = false;
|
| 104 |
+
let recordedChunks = [];
|
| 105 |
+
let uploadedFile;
|
| 106 |
+
let recordedAudioBlob;
|
| 107 |
+
let mediaRecorder;
|
| 108 |
+
|
| 109 |
+
const recordButton = document.getElementById("recordButton");
|
| 110 |
+
const submitButton = document.getElementById("submitButton");
|
| 111 |
+
const languageSelect = document.getElementById("languageSelect");
|
| 112 |
+
|
| 113 |
+
const outputText = document.getElementById("outputText");
|
| 114 |
+
|
| 115 |
+
recordButton.addEventListener("click", function () {
|
| 116 |
+
if (!isRecording) {
|
| 117 |
+
navigator.mediaDevices.getUserMedia({ audio: true }).then(function (stream) {
|
| 118 |
+
mediaRecorder = new MediaRecorder(stream);
|
| 119 |
+
recordedChunks = [];
|
| 120 |
+
|
| 121 |
+
mediaRecorder.ondataavailable = function (e) {
|
| 122 |
+
recordedChunks.push(e.data);
|
| 123 |
+
};
|
| 124 |
+
|
| 125 |
+
mediaRecorder.onstop = function () {
|
| 126 |
+
recordedAudioBlob = new Blob(recordedChunks, { type: "audio/wav" });
|
| 127 |
+
};
|
| 128 |
+
|
| 129 |
+
mediaRecorder.start();
|
| 130 |
+
isRecording = true;
|
| 131 |
+
recordButton.innerText = "Stop Recording";
|
| 132 |
+
}).catch(function (err) {
|
| 133 |
+
console.error("Error accessing microphone:", err);
|
| 134 |
+
});
|
| 135 |
+
} else {
|
| 136 |
+
mediaRecorder.stop();
|
| 137 |
+
isRecording = false;
|
| 138 |
+
recordButton.innerText = "Start Recording";
|
| 139 |
+
}
|
| 140 |
+
});
|
| 141 |
+
|
| 142 |
+
const audioUpload = document.getElementById("audioUpload");
|
| 143 |
+
|
| 144 |
+
audioUpload.addEventListener("change", function (event) {
|
| 145 |
+
uploadedFile = event.target.files[0];
|
| 146 |
+
if (uploadedFile) {
|
| 147 |
+
const fileReader = new FileReader();
|
| 148 |
+
fileReader.onload = function () {
|
| 149 |
+
uploadedArrayBuffer = fileReader.result;
|
| 150 |
+
};
|
| 151 |
+
fileReader.readAsArrayBuffer(uploadedFile);
|
| 152 |
+
}
|
| 153 |
+
});
|
| 154 |
+
|
| 155 |
+
submitButton.addEventListener("click", function () {
|
| 156 |
+
if (recordedAudioBlob || uploadedFile) {
|
| 157 |
+
const formData = new FormData();
|
| 158 |
+
|
| 159 |
+
if (recordedAudioBlob) {
|
| 160 |
+
formData.append("audio", recordedAudioBlob, "recorded_audio.wav");
|
| 161 |
+
} else if (uploadedFile) {
|
| 162 |
+
formData.append("audio", uploadedFile, uploadedFile.name);
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
outputText.style.display="block";
|
| 166 |
+
|
| 167 |
+
formData.append("language", languageSelect.value);
|
| 168 |
+
|
| 169 |
+
fetch("/process_audio", {
|
| 170 |
+
method: "POST",
|
| 171 |
+
body: formData,
|
| 172 |
+
})
|
| 173 |
+
.then((response) => response.json())
|
| 174 |
+
.then((data) => {
|
| 175 |
+
if (data.success) {
|
| 176 |
+
outputText.value = "Translated Text : " + data.language;
|
| 177 |
+
} else {
|
| 178 |
+
outputText.value = data.message;
|
| 179 |
+
}
|
| 180 |
+
})
|
| 181 |
+
.catch((error) => {
|
| 182 |
+
console.error("Error communicating with the backend:", error);
|
| 183 |
+
outputText.value = "Backend communication failed.";
|
| 184 |
+
});
|
| 185 |
+
|
| 186 |
+
if (recordedAudioBlob) {
|
| 187 |
+
const downloadLink = document.createElement("a");
|
| 188 |
+
downloadLink.href = URL.createObjectURL(recordedAudioBlob);
|
| 189 |
+
downloadLink.download = "recorded_audio.wav";
|
| 190 |
+
downloadLink.style.display = "none";
|
| 191 |
+
document.body.appendChild(downloadLink);
|
| 192 |
+
downloadLink.click();
|
| 193 |
+
URL.revokeObjectURL(downloadLink.href);
|
| 194 |
+
document.body.removeChild(downloadLink);
|
| 195 |
+
}
|
| 196 |
+
} else {
|
| 197 |
+
outputText.value = "Please record audio or upload a file and select a language before submitting.";
|
| 198 |
+
}
|
| 199 |
+
});
|
| 200 |
+
</script>
|
| 201 |
+
</body>
|
| 202 |
+
</html>
|
main.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import subprocess
|
| 4 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 5 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 6 |
+
from fastapi.responses import HTMLResponse
|
| 7 |
+
from pydub import AudioSegment
|
| 8 |
+
import shutil
|
| 9 |
+
import tempfile
|
| 10 |
+
import speech_recognition as sr
|
| 11 |
+
import os
|
| 12 |
+
r = sr.Recognizer()
|
| 13 |
+
|
| 14 |
+
app = FastAPI()
|
| 15 |
+
|
| 16 |
+
def resample_audio(input_path, output_path, target_sample_rate):
|
| 17 |
+
ffmpeg_cmd = [
|
| 18 |
+
"ffmpeg",
|
| 19 |
+
"-i", input_path,
|
| 20 |
+
"-ar", str(target_sample_rate),
|
| 21 |
+
output_path
|
| 22 |
+
]
|
| 23 |
+
subprocess.run(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 24 |
+
|
| 25 |
+
@app.get("/", response_class=HTMLResponse)
|
| 26 |
+
async def read_root():
|
| 27 |
+
# Provide the path to the HTML file containing the front-end code
|
| 28 |
+
with open("index.html", "r") as file:
|
| 29 |
+
html_content = file.read()
|
| 30 |
+
return html_content
|
| 31 |
+
|
| 32 |
+
def get_sampling_rate(audio_file_path):
|
| 33 |
+
audio = AudioSegment.from_file(audio_file_path)
|
| 34 |
+
return audio.frame_rate
|
| 35 |
+
|
| 36 |
+
@app.post("/process_audio")
|
| 37 |
+
async def process_audio(audio: UploadFile = File(...), language: str = Form(...)):
|
| 38 |
+
if not audio or not language:
|
| 39 |
+
return JSONResponse(content={"success": False}, status_code=400)
|
| 40 |
+
|
| 41 |
+
# Check if the uploaded file is in WAV format
|
| 42 |
+
if audio.content_type != "audio/wav":
|
| 43 |
+
return JSONResponse(content={"success": False, "message": "Audio must be in WAV format."}, status_code=400)
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
# Save the received audio to a temporary file
|
| 47 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
|
| 48 |
+
temp_file_path = temp_file.name
|
| 49 |
+
shutil.copyfileobj(audio.file, temp_file)
|
| 50 |
+
|
| 51 |
+
# Print the file path for debugging
|
| 52 |
+
output_path = tempfile.mktemp(suffix=".wav")
|
| 53 |
+
|
| 54 |
+
# Resample the audio to 16000 Hz
|
| 55 |
+
resample_audio(temp_file_path, output_path, target_sample_rate=16000)
|
| 56 |
+
|
| 57 |
+
# Get the sampling rate of the received audio
|
| 58 |
+
sampling_rate = get_sampling_rate(output_path)
|
| 59 |
+
|
| 60 |
+
# Resample the audio to 16 kHz if needed
|
| 61 |
+
if sampling_rate != 16000:
|
| 62 |
+
return JSONResponse(content={"success": False, "message": "Sample rate is not 16000Hz."}, status_code=500)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print("Error processing audio:", e)
|
| 69 |
+
return JSONResponse(content={"success": False, "message": "Error processing audio."}, status_code=500)
|
| 70 |
+
# finally:
|
| 71 |
+
# # Cleanup: remove the temporary received audio file
|
| 72 |
+
# if os.path.exists(audio_file_path):
|
| 73 |
+
# os.remove(audio_file_path)
|
| 74 |
+
return JSONResponse(content={"success": True, "language":calling_asr(output_path,language)})
|
| 75 |
+
|
| 76 |
+
def calling_asr(wav_file,lid):
|
| 77 |
+
AUDIO_FILE=wav_file
|
| 78 |
+
# aud_name=AUDIO_FILE.split('/')[-1].split('.')[0]
|
| 79 |
+
file=open(wav_file+".txt","w")
|
| 80 |
+
text="cant read wav file"
|
| 81 |
+
try:
|
| 82 |
+
with sr.AudioFile(AUDIO_FILE) as source:
|
| 83 |
+
audio = r.record(source)
|
| 84 |
+
text = r.recognize_google(audio, language=lid)
|
| 85 |
+
#file.write(aud_name +"\t"+text)
|
| 86 |
+
return text
|
| 87 |
+
except:
|
| 88 |
+
#file.write(" "+"Error in segement"+" ")
|
| 89 |
+
return text
|
| 90 |
+
#file.close()
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
import uvicorn
|
| 94 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
pydub
|
| 4 |
+
SpeechRecognition
|
| 5 |
+
numpy
|
| 6 |
+
librosa
|
| 7 |
+
sounddevice
|
run.sh
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
python -m uvicorn main:app --reload
|