wjbmattingly commited on
Commit
17abae8
·
1 Parent(s): 0873af8

changed app design

Browse files
Files changed (2) hide show
  1. .DS_Store +0 -0
  2. app.py +28 -30
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
app.py CHANGED
@@ -1,48 +1,46 @@
1
  import gradio as gr
2
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
3
  import torch
4
- import librosa
5
  import spaces
6
 
 
7
  # Load the model and processor
8
- model_name = "TheirStory/whisper-small-xhosa"
9
- processor = WhisperProcessor.from_pretrained(model_name)
10
- model = WhisperForConditionalGeneration.from_pretrained(model_name)
11
 
12
- @spaces.GPU
13
- def transcribe_audio(audio):
14
- if torch.cuda.is_available():
15
- model = model.to("cuda")
16
-
17
- # Load the audio file
18
- if isinstance(audio, str): # If it's a file path
19
- audio_array, sampling_rate = librosa.load(audio, sr=16000)
20
- else: # If it's a tuple (audio_array, sampling_rate)
21
- audio_array, sampling_rate = audio
22
-
23
- # Process the audio
24
- input_features = processor(audio_array, sampling_rate=sampling_rate, return_tensors="pt").input_features
25
 
26
- if torch.cuda.is_available():
27
- input_features = input_features.to("cuda")
 
 
 
 
28
 
29
- # Generate token ids
30
- generated_ids = model.generate(input_features)
31
 
32
- # Decode token ids to text
33
- transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
 
34
 
35
- return transcription
 
36
 
37
- # Create the Gradio interface
38
- iface = gr.Interface(
39
- fn=transcribe_audio,
40
  inputs=[
41
- gr.Audio(type="filepath", label="Upload or Record an Audio File")
 
42
  ],
43
  outputs="text",
44
- title="Xhosa Audio Transcription",
45
- description="Record or upload Xhosa audio to get its transcription using the TheirStory/whisper-small-xhosa model."
 
 
 
 
 
 
46
  )
47
 
48
  # Launch the app
 
1
  import gradio as gr
2
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
3
  import torch
4
+ from transformers import pipeline
5
  import spaces
6
 
7
+ BATCH_SIZE = 8
8
  # Load the model and processor
9
+ MODEL_NAME = "TheirStory/whisper-small-xhosa"
 
 
10
 
11
+ device = 0 if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ pipe = pipeline(
14
+ task="automatic-speech-recognition",
15
+ model=MODEL_NAME,
16
+ chunk_length_s=30,
17
+ device=device,
18
+ )
19
 
 
 
20
 
21
+ @spaces.GPU
22
+ def transcribe(inputs, task):
23
+ if inputs is None:
24
+ raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
25
 
26
+ text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": "transcribe"}, return_timestamps=True)["text"]
27
+ return text
28
 
29
+ file_transcribe = gr.Interface(
30
+ fn=transcribe,
 
31
  inputs=[
32
+ gr.Audio(type="filepath", label="Audio file"),
33
+ # gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
34
  ],
35
  outputs="text",
36
+ theme="huggingface",
37
+ title="Whisper App",
38
+ description=(
39
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
40
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
41
+ " of arbitrary length."
42
+ ),
43
+ allow_flagging="never",
44
  )
45
 
46
  # Launch the app