File size: 4,089 Bytes
787a546
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
# coding: utf-8

# In[29]:


import ipywidgets as widgets
from IPython.display import display, clear_output
from threading import Thread
from queue import Queue
import time

messages = Queue()
recordings = Queue()

record_button = widgets.Button(
    description="Record",
    disabled=False,
    button_style="success",
    icon="microphone"
)

stop_button = widgets.Button(
    description="Stop",
    disabled=False,
    button_style="warning",
    icon="stop"
)

output = widgets.Output()

def record_microphone():
    while not messages.empty():
        time.sleep(1)  # Simulate recording
        recordings.put("Audio recorded.")  # Simulated recorded audio data

def speech_recognition(output_widget):
    while not messages.empty():
        time.sleep(2)  # Simulate transcription
        with output_widget:
            clear_output(wait=True)
            display("Transcription: Hello, how are you?")  # Simulated transcription result

def start_recording(data):
    if not messages.empty():
        return  # Recording already in progress

    messages.put(True)
    with output:
        clear_output(wait=True)
        display("Starting...")

    record = Thread(target=record_microphone)
    record.start()

    transcribe = Thread(target=speech_recognition, args=(output,))
    transcribe.start()

def stop_recording(data):
    if messages.empty():
        return  # No recording in progress

    messages.get()
    with output:
        clear_output(wait=True)
        display("Stopped.")

record_button.on_click(start_recording)
stop_button.on_click(stop_recording)

display(widgets.HBox([record_button, stop_button]), output)


# In[30]:


get_ipython().system('python -m pip install pyaudio')


# In[31]:


import pyaudio

p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
    print(p.get_device_info_by_index(i))

p.terminate()


# In[32]:


import pyaudio
from queue import Queue

CHANNELS = 1
FRAME_RATE = 16000
RECORD_SECONDS = 20
AUDIO_FORMAT = pyaudio.paInt16
SAMPLE_SIZE = 2

messages = Queue()
recordings = Queue()

def record_microphone(chunk=1024):
    p = pyaudio.PyAudio()

    stream = p.open(format=AUDIO_FORMAT,
                    channels=CHANNELS,
                    rate=FRAME_RATE,
                    input=True,
                    input_device_index=1,
                    frames_per_buffer=chunk)

    frames = []

    while not messages.empty():
        data = stream.read(chunk)
        frames.append(data)

        if len(frames) >= int(FRAME_RATE * RECORD_SECONDS / chunk):
            recordings.put(frames.copy())
            frames = []

    stream.stop_stream()
    stream.close()
    p.terminate()


# In[33]:


import subprocess
import json
from vosk import Model, KaldiRecognizer

model = Model(model_name="vosk-model-en-us-0.42-gigaspeech")
rec = KaldiRecognizer(model, FRAME_RATE)
rec.SetWords(True)

def speech_recognition(output):
    while not messages.empty():
        frames = recordings.get()

        rec.AcceptWaveform(b''.join(frames))
        result = rec.Result()
        text = json.loads(result)["text"]

        cased = subprocess.check_output("python recasepunc/recasepunc.py predict recasepunc/checkpoint", shell=True, text=True, input=text)
        output.append_stdout(cased)


# In[2]:


def my_function(input1, input2):
    # Process the inputs and generate the output
    output = f"Processed {input1} and {input2}"
    return output


# In[5]:


import gradio as gr

# Define the function you want to expose through Gradio
def my_function(input1, input2):
    output = f"Processed {input1} and {input2}"
    return output

# Create the Gradio interface
iface = gr.Interface(
    fn=my_function,
    inputs=[gr.Textbox(label="Input 1"), gr.Textbox(label="Input 2")],
    outputs=gr.Textbox(label="Output")
)

# Launch the interface with a public link
iface.launch(share=True)


# In[ ]: