# %% #%pip install transformers #%pip install torch #%pip install torchaudio #%pip install fuzzywuzzy #%pip install pdfkit #%pip install jinja2 #%pip install wkhtmltopdf # %% import gradio as gr from transformers import pipeline import numpy as np import string from fuzzywuzzy import fuzz transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en") with gr.Blocks() as demo: def welcome(rank, name): global g_rank global g_name g_rank = rank g_name = name welcome_message= "## Welcome "+rank+" "+name+"!" return {welcome_message_markdown: gr.Markdown(value=welcome_message, visible=True), welcome_inputs_col: gr.Column(visible=False), section_1_col: gr.Column(visible = True)} def transcribe(audio): global g_result_clean sr, y = audio y = y.astype(np.float32) y /= np.max(np.abs(y)) result = transcriber({"sampling_rate": sr, "raw": y})["text"] # Create a translation table to remove punctuation translator = str.maketrans('', '', string.punctuation) # Remove punctuation and convert to lowercase result_clean = result.translate(translator).lower() g_result_clean = result_clean return result_clean def check_answer(): global g_result_clean similarity_ratio = fuzz.ratio(g_result_clean, " hello zero this is two charlie radio check over") if similarity_ratio >= 90: return {right_answer: gr.Markdown("# Congratulations! You have passed this E-learning course", visible = True), wrong_answer: gr.Markdown(" That isn't quite right. Please try again.", visible = False), section_1_practise_col: gr.Column(visible = False), section_1_col: gr.Column(visible = False)} else: return {right_answer: gr.Markdown("# Congratulations! You have passed this E-learning course", visible = False), wrong_answer: gr.Markdown(" That isn't quite right. Please try again.", visible = True)} def practise_col(): return {section_1_practise_col: gr.Column(visible = True), next_button_3: gr.Button("Practise", visible=False)} gr.Markdown("# Communications ITR E-learning Demonstrator") with gr.Column() as welcome_inputs_col: gr.Markdown("""Welcome to the Communications ITR E-learning. Please complete the following details so that your ITR can be signed off.""") #Should add validation for these inputs for product. See https://www.gradio.app/guides/controlling-layout#visibility for an example of this name_box = gr.Textbox(label="Name") #name_error_box = gr.Textbox(label="Error", visible=False) rank_box = gr.Textbox(label="Rank") number_box = gr.Textbox(label="Number") next_button = gr.Button("Submit") welcome_message_markdown= gr.Markdown("Placeholder", visible = False) with gr.Column(visible = False) as section_1_col: gr.Markdown("## Establish communications (TO 14.1.1)") gr.Markdown("### Calling Transmission") gr.Markdown("#### Initial Call") gr.Markdown("Unlike mobile phones, radios transmit onto a frequency (or range of frequencies) where anyone listening can hear. Similarly, they also do not transmit the caller ID. For this reason, when establishing communications it is important to first make an inital call as part of your message, which tells the net (everyone listening) who you wish to talk to, and who is talking:") gr.Markdown("""HELLO \ THIS IS \""") gr.Markdown("Example: HELLO 0 THIS IS 1C") gr.Markdown("#### Message Text") gr.Markdown("Now that we have let the net know who we are and who we want to talk to, we can now send our message information. We will go into this in more detail later in this E-learning package.") gr.Markdown("#### Ending") gr.Markdown("""This will always be one of the following: 1. OUT - meaning ‘This is the end of my transmission to you and no answer is required or expected’. OUT should be used whenever possible. 2. OVER - meaning ‘This is the end of my transmission to you and a response is necessary. Go ahead and transmit’. 3. OUT TO YOU - meaning ‘This is the end of my transmission and no answer is required or expected. A transmission to another station follows immediately’.""") gr.Markdown("#### Together") gr.Markdown("""These three parts together form the whole transmission as below:""") gr.Markdown(""" Format: Initial call - Message text - Ending Example: HELLO 1D THIS IS 3C, message to send, OVER""") next_button_3 = gr.Button("Practise") with gr.Column(visible = False) as section_1_practise_col: gr.Markdown("### Practice:") gr.Markdown("""Your callsign is: 2C (2- CHARLIE) and you wish to talk to 0 (ZERO). Your message to send is: "radio check", and you want a reply to your message so the message ending is "OVER". """) gr.Interface(transcribe,gr.Audio(sources=["microphone"]), "text") next_button_2 = gr.Button("Check Answer") wrong_answer = gr.Markdown(" That isn't quite right. Please try again.", visible = False) right_answer = gr.Markdown("# Congratulations! You have passed this E-learning course", visible = False) next_button.click(welcome, [rank_box, name_box], [welcome_message_markdown, welcome_inputs_col, section_1_col]) next_button_2.click(check_answer, [], [wrong_answer, right_answer, section_1_practise_col, section_1_col]) next_button_3.click(practise_col, [], [section_1_practise_col, next_button_3]) demo.launch(debug=True, show_api=False)