File size: 3,632 Bytes
bf756e1
 
15bf7ba
bf756e1
15bf7ba
bf756e1
 
15bf7ba
 
bf756e1
 
15bf7ba
 
 
 
 
 
 
bf756e1
 
 
 
 
 
 
 
 
 
 
 
15bf7ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a6d723
15bf7ba
 
 
6cb8d02
 
 
 
 
 
 
 
 
 
 
 
 
16fbeae
 
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
import gradio as gr
from huggingface_hub import InferenceClient
import difflib

# Load Hugging Face Inference client
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

# Load the speech-to-text model from Hugging Face
s2t = gr.Interface.load('huggingface/facebook/s2t-medium-librispeech-asr')


def generate_text_with_huggingface(system_message, max_tokens, temperature, top_p):
    """
    Function to generate text using Hugging Face Inference API
    based on the system message, max tokens, temperature, and top-p.
    """
    messages = [{"role": "system", "content": system_message}]
    message = ""

    response = ""

    for message in client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
    ):
        token = message.choices[0].delta.content
        response += token

    return response.strip()  # Return the generated text


def pronunciation_feedback(transcription, reference_text):
    """
    Function to provide feedback on pronunciation based on differences
    between the transcription and the reference (expected) text.
    """
    diff = difflib.ndiff(reference_text.split(), transcription.split())
    # Identify words that are incorrect or missing in the transcription
    errors = [word for word in diff if word.startswith('- ')]

    if errors:
        feedback = "Mispronounced words: " + ', '.join([error[2:] for error in errors])
    else:
        feedback = "Great job! Your pronunciation is spot on."

    return feedback


def transcribe_and_feedback(audio, system_message, max_tokens, temperature, top_p):
    """
    Transcribe the audio and provide pronunciation feedback using the generated text.
    """
    # Generate the reference text using Hugging Face Inference API
    reference_text = generate_text_with_huggingface(system_message, max_tokens, temperature, top_p)

    # Transcribe the audio using the speech-to-text model
    transcription = s2t(audio)

    # Provide pronunciation feedback based on the transcription and the generated text
    feedback = pronunciation_feedback(transcription, reference_text)

    return transcription, feedback, reference_text


# Gradio interface
demo = gr.Interface(
    fn=transcribe_and_feedback,  # The function that transcribes audio and provides feedback
    inputs=[
        gr.Audio(type="filepath", label="Record Audio"),  # Microphone input for recording
        gr.Textbox(value="Please read a simple sentence.", label="System message"),  # Message used to generate text
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),  # Controls max token length for the generated text
        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),  # Temperature control for text generation
        gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")  # Top-p control for text generation
    ],
    outputs=[
        gr.Textbox(label="Transcription"),  # Display transcription of the audio
        gr.Textbox(label="Pronunciation Feedback"),  # Feedback on pronunciation
        gr.Textbox(label="Generated Text (What You Were Supposed to Read)")  # Display the text generated by the API
    ],
    title="Speech-to-Text with Pronunciation Feedback",
    description="Record an audio sample and the system will transcribe it, "
                "compare your transcription to the generated text, and give pronunciation feedback.",
    live=True  # Real-time interaction
)

# Enable queuing and launch the app
demo.queue().launch(show_error=True)