Spaces:
Sleeping
Sleeping
File size: 6,379 Bytes
de19cbd ee49611 541820f ee49611 541820f c827239 84342a4 c827239 541820f c827239 067d472 54536f2 067d472 1e6726d ddc3a0a 1e6726d 54536f2 067d472 1e6726d 067d472 54536f2 5571210 067d472 1e6726d c54478b 067d472 a2f843a 067d472 c54478b 067d472 c54478b 067d472 4ce2779 067d472 54536f2 c54478b 54536f2 067d472 54536f2 067d472 c54478b 067d472 54536f2 067d472 601c3c6 067d472 54536f2 c54478b 067d472 ee49611 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
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()
new_word = data.get('transcription', '')
# Append new word to the user's transcription list
transcriptions.setdefault(username, []).append(new_word)
# Remove the 20th last word if more than 20 words are present
if len(transcriptions[username]) > 1:
transcriptions[username].pop(0)
# Join the words to form the updated transcription
updated_transcription = ' '.join(transcriptions[username])
return jsonify({"status": "success", "message": "Word added", "transcription": updated_transcription})
elif request.method == 'GET':
transcription = ' '.join(transcriptions.get(username, []))
return transcription if transcription else 'N/A'
@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="language">Language:</label>
<select id="language">
<option value="en-US">English (US)</option>
<option value="es-ES">Spanish (Spain)</option>
<option value="fr-FR">French</option>
<option value="de-DE">German (Germany)</option>
<option value="it-IT">Italian (Italy)</option>
<option value="ja-JP">Japanese (Japan)</option>
<option value="ko-KR">Korean (South Korea)</option>
<option value="pt-PT">Portuguese (Portugal)</option>
<option value="pt-BR">Portuguese (Brazil)</option>
<option value="ru-RU">Russian (Russia)</option>
<option value="sv-SE">Swedish (Sweden)</option>
<option value="tr-TR">Turkish (Turkey)</option>
<option value="hi-IN">Hindi (India)</option>
<option value="nl-NL">Dutch (Netherlands)</option>
<option value="el-GR">Greek (Greece)</option>
<option value="he-IL">Hebrew (Israel)</option>
<option value="hu-HU">Hungarian (Hungary)</option>
<option value="id-ID">Indonesian (Indonesia)</option>
<option value="no-NO">Norwegian (Norway)</option>
<option value="pl-PL">Polish (Poland)</option>
<option value="ro-RO">Romanian (Romania)</option>
<option value="th-TH">Thai (Thailand)</option>
<option value="vi-VN">Vietnamese (Vietnam)</option>
<option value="uk-UA">Ukrainian (Ukraine)</option>
<option value="ur-PK">Urdu (Pakistan)</option>
<!-- Add more languages as needed -->
</select>
</div>
<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.");
}
// ... existing code ...
let recognition = new window.SpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
let liveOutput = document.getElementById('transcription');
let lastTranscription = '';
// Function to update language
function updateLanguage() {
let selectedLanguage = document.getElementById('language').value;
recognition.lang = selectedLanguage;
}
// Set initial language
updateLanguage();
// Update language when user changes selection
document.getElementById('language').addEventListener('change', updateLanguage);
// ... rest of your existing code ...
recognition.onresult = (event) => {
let currentTranscription = '';
for (const result of event.results) {
currentTranscription = result[0].transcript;
}
liveOutput.textContent = currentTranscription;
if (lastTranscription !== currentTranscription) {
lastTranscription = currentTranscription;
sendTranscriptionToServer(currentTranscription);
}
};
recognition.onerror = (event) => {
console.error('Speech recognition error', event.error);
recognition.start();
};
recognition.onend = () => {
// Automatically restart the recognition when it ends
recognition.start();
};
function sendTranscriptionToServer(transcription) {
let username = document.getElementById('username').value;
$.ajax({
url: 'https://johnpaulbin-api-test-stt.hf.space/' + username,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ transcription: transcription }),
success: function(response) {
console.log('Transcription sent', response);
},
error: function(error) {
console.error('Error sending transcription', error);
}
});
}
document.getElementById('start').addEventListener('click', () => {
recognition.start();
});
document.getElementById('stop').addEventListener('click', () => {
recognition.stop();
});
</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)) |