api-test-stt / app.py
johnpaulbin's picture
Update app.py
54536f2 verified
raw
history blame
4.07 kB
from flask import Flask, request, jsonify, Response
import asyncio
from hypercorn.asyncio import serve
from hypercorn.config import Config
import os
os.environ['CURL_CA_BUNDLE'] = ''
app = Flask(__name__)
transcriptions = {}
@app.route('/<username>', methods=['POST', 'GET'])
def handle_transcription(username):
if request.method == 'POST':
data = request.get_json()
transcription = data.get('transcription', '')
transcriptions[username] = transcription # Store the transcription
return jsonify({"status": "success", "message": "Transcription received"})
elif request.method == 'GET':
transcription = transcriptions.get(username, 'N/A')
return transcription
@app.route('/')
def home():
html_content = """
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Speech to Text</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Speech Recognition</h1>
<button id="start">Start Listening</button>
<button id="stop">Stop Listening</button>
<div>
<label for="username">Username:</label>
<input type="text" id="username" placeholder="Enter your username">
</div>
<div id="transcription" style="border: 1px solid #ccc; padding: 10px; margin-top: 5px; min-height: 50px;"></div>
<script>
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!window.SpeechRecognition) {
alert("Your browser does not support Speech Recognition. Try Google Chrome.");
}
let recognition = new window.SpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
let liveOutput = document.getElementById('transcription');
let lastTranscription = '';
let intervalId = null;
let delimiterId = null;
recognition.onresult = (event) => {
let currentTranscription = '';
for (const result of event.results) {
currentTranscription += result[0].transcript;
}
liveOutput.textContent = currentTranscription;
if (lastTranscription !== currentTranscription) {
lastTranscription = currentTranscription;
console.log(currentTranscription);
}
};
recognition.onerror = (event) => {
console.error('Speech recognition error', event.error);
};
function sendTranscriptionToServer() {
let username = document.getElementById('username').value;
// Extract the latest entry after the last delimiter
let latestEntry = lastTranscription.substring(lastTranscription.lastIndexOf('DELIMIN') + 7);
$.ajax({
url: 'https://johnpaulbin-api-test-stt.hf.space/' + username,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ transcription: latestEntry }),
success: function(response) {
console.log('Transcription sent', response);
},
error: function(error) {
console.error('Error sending transcription', error);
}
});
}
function appendDelimiter() {
lastTranscription += 'DELIMIN';
}
document.getElementById('start').addEventListener('click', () => {
intervalId = setInterval(sendTranscriptionToServer, 350);
delimiterId = setInterval(appendDelimiter, 3500);
});
document.getElementById('stop').addEventListener('click', () => {
recognition.stop();
clearInterval(intervalId);
clearInterval(delimiterId);
});
</script>
</body>
</html>
"""
return Response(html_content, mimetype='text/html')
if __name__ == "__main__":
config = Config()
config.bind = ["0.0.0.0:7860"] # You can specify the host and port here
asyncio.run(serve(app, config))