Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import numpy as np
|
4 |
+
from ner import perform_ner
|
5 |
+
from intent import perform_intent_classification
|
6 |
+
|
7 |
+
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
|
8 |
+
|
9 |
+
def transcribe(stream, new_chunk):
|
10 |
+
transcription = ""
|
11 |
+
sentence_buffer = ""
|
12 |
+
results = []
|
13 |
+
sr, y = new_chunk
|
14 |
+
y = y.astype(np.float32)
|
15 |
+
y /= np.max(np.abs(y))
|
16 |
+
|
17 |
+
if stream is not None:
|
18 |
+
stream = np.concatenate([stream, y])
|
19 |
+
else:
|
20 |
+
stream = y
|
21 |
+
print(transcriber({"sampling_rate": sr, "raw": stream})["text"])
|
22 |
+
transcription=transcriber({"sampling_rate": sr, "raw": stream})["text"]
|
23 |
+
# Check for sentence boundaries
|
24 |
+
sentence_boundary = "." in transcription or "?" in transcription
|
25 |
+
|
26 |
+
if sentence_boundary:
|
27 |
+
sentence = sentence_buffer + transcription.split(transcription[-1])[0]
|
28 |
+
print("Sentence Buffer :",sentence_buffer)
|
29 |
+
print("Sentence :",sentence)
|
30 |
+
ner_result = perform_ner(sentence)
|
31 |
+
intent_result = perform_intent_classification(sentence)
|
32 |
+
print("NER Result (sentence):", ner_result)
|
33 |
+
print("Intent Result (sentence):", intent_result)
|
34 |
+
sentence_buffer = transcription[-1] # Start a new sentence buffer
|
35 |
+
transcription = "" # Reset transcription for the new sentence
|
36 |
+
return stream, transcriber({"sampling_rate": sr, "raw": stream})["text"], ner_result, intent_result
|
37 |
+
|
38 |
+
demo = gr.Interface(
|
39 |
+
transcribe,["state", gr.Audio(sources=["microphone"], streaming=True),
|
40 |
+
],
|
41 |
+
["state", gr.Text(label="Transcribe"), gr.Text(label="NER"), gr.Text(label="Intent")],
|
42 |
+
live=True,
|
43 |
+
)
|
44 |
+
demo.launch(share=True)
|