Julienxu commited on
Commit
2a9f0bb
·
verified ·
1 Parent(s): 0ff5307

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Build a shareable app with Gradio
2
+ #"E:\machine_learning\nlp\speech.py"
3
+ import torch
4
+ from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
5
+ #from datasets import load_dataset
6
+
7
+
8
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
10
+
11
+ model_id = "E:/machine_learning/models/distil-whisperdistil-small.en"
12
+
13
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(
14
+ model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
15
+ )
16
+ model.to(device)
17
+
18
+ processor = AutoProcessor.from_pretrained(model_id)
19
+
20
+ asr = pipeline(
21
+ "automatic-speech-recognition",
22
+ model=model,
23
+ tokenizer=processor.tokenizer,
24
+ feature_extractor=processor.feature_extractor,
25
+ max_new_tokens=128,
26
+ torch_dtype=torch_dtype,
27
+ device=device,
28
+ )
29
+
30
+ '''#pipeline also works
31
+ asr = pipeline(task="automatic-speech-recognition",
32
+ model=model_id)
33
+ '''
34
+
35
+ import os
36
+ import gradio as gr
37
+
38
+ demo = gr.Blocks()
39
+
40
+ def transcribe_speech(filepath):
41
+ if filepath is None:
42
+ gr.Warning("No audio found, please retry.")
43
+ return ""
44
+ output = asr(filepath)
45
+ return output["text"]
46
+
47
+ mic_transcribe = gr.Interface(
48
+ fn=transcribe_speech,
49
+ inputs=gr.Audio(sources="microphone",
50
+ type="filepath"),
51
+ outputs=gr.Textbox(label="Transcription",
52
+ lines=3),
53
+ allow_flagging="never")
54
+
55
+ file_transcribe = gr.Interface(
56
+ fn=transcribe_speech,
57
+ inputs=gr.Audio(sources="upload",
58
+ type="filepath"),
59
+ outputs=gr.Textbox(label="Transcription",
60
+ lines=3),
61
+ allow_flagging="never",
62
+ )
63
+
64
+ with demo:
65
+ gr.TabbedInterface(
66
+ [mic_transcribe,
67
+ file_transcribe],
68
+ ["Transcribe Microphone",
69
+ "Transcribe Audio File"],
70
+ )
71
+
72
+ demo.launch(share=True,
73
+ server_port=int(os.environ.get('PORT1',8080)))