File size: 4,242 Bytes
de19cbd
ee49611
 
 
 
 
 
 
 
 
 
 
 
541820f
ee49611
541820f
 
 
 
 
 
 
dca1ca2
067d472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e22e2c
 
0740c85
0e22e2c
08104f0
0740c85
0e22e2c
 
067d472
 
 
0bd32c4
 
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
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>
        // Check for browser support
        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; // Keep listening even after getting a result
        recognition.interimResults = true; // Show interim results
        let liveOutput = document.getElementById('transcription');
        let lastTranscription = '';
        let intervalId = null;
        recognition.onresult = (event) => {
            let currentTranscription = '';
            // Concatenate all the transcriptions
            for (const result of event.results) {
                currentTranscription += result[0].transcript;
            }
            liveOutput.textContent = currentTranscription;
            
            // Check if there's a new transcription
            if (lastTranscription !== currentTranscription) {
                lastTranscription = currentTranscription;
                console.log(currentTranscription);
            }
        };
        recognition.onerror = (event) => {
            console.error('Speech recognition error', event.error);
        };
        function sendTranscriptionToServer(transcription) {
			let username = document.getElementById('username').value;
            $.ajax({
                url: 'https://johnpaulbin-api-test-stt.hf.space/' + username, // Your server endpoint
                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);
                }
            });
        }
		
		function clearTranscription() {
            recognition.stop();
			liveOutput.textContent = '';
			lastTranscription = '';
            recognition.start();
		}
		
        // Start and stop functions
        document.getElementById('start').addEventListener('click', () => {
            recognition.start();
            intervalId = setInterval(() => sendTranscriptionToServer(lastTranscription), 350);
			setInterval(clearTranscription, 3500); // Clear transcription every 2 seconds
        });
        document.getElementById('stop').addEventListener('click', () => {
            recognition.stop();
            clearInterval(intervalId);
        });
    </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))