rec2text : voice recorder transcription using whisper API
This commit is contained in:
parent
ba1f7146f9
commit
47211ca4b1
|
@ -0,0 +1,85 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Treasure Hunt App</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Treasure Hunt</h1>
|
||||
<p id="status">Waiting for coordinates...</p>
|
||||
|
||||
<script>
|
||||
// Function to calculate the distance between two points using Haversine formula
|
||||
function calculateDistance(lat1, lon1, lat2, lon2) {
|
||||
const R = 6371; // Radius of the Earth in kilometers
|
||||
const dLat = (lat2 - lat1) * (Math.PI / 180);
|
||||
const dLon = (lon2 - lon1) * (Math.PI / 180);
|
||||
const a =
|
||||
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos(lat1 * (Math.PI / 180)) * Math.cos(lat2 * (Math.PI / 180)) *
|
||||
Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
const distance = R * c;
|
||||
return distance;
|
||||
}
|
||||
|
||||
// Function to check if the user is at the treasure spot
|
||||
function checkTreasure(latitude, longitude) {
|
||||
// Replace these values with the actual coordinates of the treasures
|
||||
const treasureCoordinates = [
|
||||
{ lat: 37.7749, lon: -122.4194 }, // Example coordinates (San Francisco, CA)
|
||||
// Add more coordinates as needed
|
||||
];
|
||||
|
||||
for (const treasure of treasureCoordinates) {
|
||||
const distance = calculateDistance(latitude, longitude, treasure.lat, treasure.lon);
|
||||
if (distance < 0.1) {
|
||||
// User is at the treasure spot
|
||||
document.getElementById('status').innerText = 'You found the treasure!';
|
||||
// Ask for email and send a request to a GET URL
|
||||
const userEmail = prompt('Congratulations! Enter your email to claim the treasure:');
|
||||
sendRequest(userEmail);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// User is not at any treasure spot
|
||||
document.getElementById('status').innerText = 'Waiting for coordinates...';
|
||||
}
|
||||
|
||||
// Function to send a request to a GET URL with the user's email
|
||||
function sendRequest(email) {
|
||||
// Replace 'YOUR_GET_URL' with the actual GET URL
|
||||
const apiUrl = 'YOUR_GET_URL';
|
||||
|
||||
// Construct the final URL with the user's email
|
||||
const finalUrl = `${apiUrl}?email=${encodeURIComponent(email)}`;
|
||||
|
||||
// Use fetch to send the GET request
|
||||
fetch(finalUrl)
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
console.log('Request sent successfully.');
|
||||
} else {
|
||||
console.error('Error sending request:', response.status);
|
||||
}
|
||||
})
|
||||
.catch(error => console.error('Error sending request:', error));
|
||||
}
|
||||
|
||||
// Dummy function to simulate getting the user's current location
|
||||
function getUserLocation() {
|
||||
// Replace with actual geolocation API calls for a mobile app
|
||||
// In a real-world scenario, you would request the user's location using the Geolocation API
|
||||
// For this example, we'll use hardcoded values for testing
|
||||
const userLatitude = 37.7749; // Example latitude (San Francisco, CA)
|
||||
const userLongitude = -122.4194; // Example longitude (San Francisco, CA)
|
||||
checkTreasure(userLatitude, userLongitude);
|
||||
}
|
||||
|
||||
// Simulate checking for user location every few seconds (for demonstration purposes)
|
||||
setInterval(getUserLocation, 5000);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,46 @@
|
|||
#!/bin/python3
|
||||
import os
|
||||
import time
|
||||
from fastapi import FastAPI, File, UploadFile
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import whisper
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# Enable CORS for all origins
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # Set this to ["*"] to allow all origins, or specify your trusted origins
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
whisper = whisper.load_model("medium.en")
|
||||
|
||||
@app.post("/speechToText")
|
||||
async def speech_to_text(file: UploadFile = File(...)):
|
||||
try:
|
||||
# Save the uploaded audio file locally
|
||||
audio_path = "temp_audio.wav"
|
||||
with open(audio_path, "wb") as audio_file:
|
||||
audio_file.write(file.file.read())
|
||||
|
||||
# Wait for the file to be fully written
|
||||
while os.path.getsize(audio_path) == 0:
|
||||
time.sleep(0.1) # Adjust the sleep duration as needed
|
||||
|
||||
# Use whisper to convert speech to text
|
||||
text = whisper.transcribe(audio_path, language="en")['text']
|
||||
|
||||
# Return the transcribed text
|
||||
return text
|
||||
|
||||
except Exception as e:
|
||||
return JSONResponse(content={"error": str(e)}, status_code=500)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=9000)
|
|
@ -0,0 +1,110 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Audio Recorder</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Audio Recorder</h1>
|
||||
<p>Press the button to start recording. Click again to stop recording and send the audio.</p>
|
||||
|
||||
<button id="recordButton">Start Recording</button>
|
||||
<span id="countdown"></span>
|
||||
<div id="result"></div>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
let recording = false;
|
||||
let audioChunks = [];
|
||||
let mediaRecorder;
|
||||
let timeoutId;
|
||||
let countdownValue = 5; // Initial countdown value in seconds
|
||||
|
||||
const recordButton = document.getElementById('recordButton');
|
||||
const countdownElement = document.getElementById('countdown');
|
||||
|
||||
recordButton.addEventListener('click', () => {
|
||||
if (!recording) {
|
||||
startRecording();
|
||||
countdown();
|
||||
recordButton.textContent = 'Stop Recording';
|
||||
} else {
|
||||
stopRecording();
|
||||
recordButton.textContent = 'Sending Audio...';
|
||||
sendAudio();
|
||||
}
|
||||
recording = !recording;
|
||||
});
|
||||
|
||||
function startRecording() {
|
||||
navigator.mediaDevices.getUserMedia({ audio: true })
|
||||
.then((stream) => {
|
||||
audioChunks = [];
|
||||
mediaRecorder = new MediaRecorder(stream);
|
||||
|
||||
mediaRecorder.ondataavailable = (event) => {
|
||||
if (event.data.size > 0) {
|
||||
audioChunks.push(event.data);
|
||||
}
|
||||
};
|
||||
|
||||
mediaRecorder.onstop = () => {
|
||||
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
||||
const audioUrl = URL.createObjectURL(audioBlob);
|
||||
const audio = new Audio(audioUrl);
|
||||
audio.play();
|
||||
};
|
||||
|
||||
mediaRecorder.start();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error accessing microphone:', error);
|
||||
});
|
||||
}
|
||||
|
||||
function stopRecording() {
|
||||
if (mediaRecorder.state === 'recording') {
|
||||
mediaRecorder.stop();
|
||||
}
|
||||
}
|
||||
|
||||
async function sendAudio() {
|
||||
const formData = new FormData();
|
||||
formData.append('file', new Blob(audioChunks, { type: 'audio/wav' }));
|
||||
|
||||
try {
|
||||
const response = await fetch('http://alienware.local:9000/speechToText', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
const result = await response.text();
|
||||
console.log('Speech to text result:', result);
|
||||
// Display the result on the page
|
||||
resultElement.textContent = 'Speech to text result: ' + result;
|
||||
|
||||
recordButton.textContent = 'Start Recording';
|
||||
countdownElement.textContent = '';
|
||||
} catch (error) {
|
||||
console.error('Error sending audio:', error);
|
||||
recordButton.textContent = 'Error Sending Audio';
|
||||
}
|
||||
}
|
||||
|
||||
function countdown() {
|
||||
countdownElement.textContent = `Recording: ${countdownValue} seconds remaining`;
|
||||
timeoutId = setInterval(() => {
|
||||
countdownValue--;
|
||||
countdownElement.textContent = `Recording: ${countdownValue} seconds remaining`;
|
||||
|
||||
if (countdownValue <= 0) {
|
||||
clearInterval(timeoutId);
|
||||
stopRecording();
|
||||
recordButton.textContent = 'Sending Audio...';
|
||||
sendAudio();
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue