File size: 2,317 Bytes
22ba507
db9a501
22ba507
 
a95b4f8
22ba507
 
 
a95b4f8
22ba507
 
a95b4f8
22ba507
 
 
a95b4f8
22ba507
 
 
 
 
 
 
 
 
 
 
a95b4f8
22ba507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2edd588
22ba507
2edd588
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
import gradio as gr
from transformers import pipeline
import numpy as np
import time

# Initialize the pipelines
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-tiny.en")
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli")

candidate_labels = ["dim the light", "turn on light fully", "turn off light fully", "raise the light", "not about lighting"]
last_update_time = time.time() - 5  # Initialize with a value to ensure immediate first update

# Buffer to hold the last updated values
last_transcription = ""
last_classification = ""

def transcribe_and_classify(stream, new_chunk):
    global last_update_time, last_transcription, last_classification
    sr, y = new_chunk
    y = y.astype(np.float32)
    y /= np.max(np.abs(y))

    # Concatenate new audio chunk to the stream
    if stream is not None:
        stream = np.concatenate([stream, y])
    else:
        stream = y

    # Keep only the last 10 seconds of audio
    num_samples_last_10_seconds = 5 * sr
    if len(stream) > num_samples_last_10_seconds:
        stream = stream[-num_samples_last_10_seconds:]

    current_time = time.time()
    
    # Update every 5 seconds
    if current_time - last_update_time >= 5:
        last_update_time = current_time
        # Transcribe the last 10 seconds of audio
        transcription = transcriber({"sampling_rate": sr, "task": "transcribe", "language": "english", "raw": stream})["text"]
        last_transcription = transcription  # Update the buffer
        
        # Classify the transcribed text
        if transcription.strip():
            output = classifier(transcription, candidate_labels, multi_label=False)
            top_label = output['labels'][0]
            top_score = output['scores'][0]
            last_classification = f"{top_label.upper()}, score: {top_score:.2f}"
    
    # Return the last updated transcription and classification
    return stream, last_transcription, last_classification

# Define the Gradio interface
demo = gr.Interface(
    fn=transcribe_and_classify,
    inputs=[
        "state", 
        gr.Audio(sources=["microphone"], streaming=True)
    ],
    outputs=[
        "state", 
        "text", 
        "text"
    ],
    live=True
)

# Launch the demo
demo.launch(debug=True)