Alimubariz124 commited on
Commit
c8d4343
·
verified ·
1 Parent(s): c1e4c84

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
4
+
5
+ # Check if CUDA is available, and choose device accordingly
6
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
7
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
8
+
9
+ # Load the model and tokenizer
10
+ model_id = "openai/whisper-large-v3"
11
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(
12
+ model_id, torch_dtype=torch_dtype, use_safetensors=True
13
+ )
14
+ model.to(device)
15
+ processor = AutoProcessor.from_pretrained(model_id)
16
+
17
+ # Define a function to transcribe audio
18
+ pipe = pipeline(
19
+ "automatic-speech-recognition",
20
+ model=model,
21
+ tokenizer=processor.tokenizer,
22
+ feature_extractor=processor.feature_extractor,
23
+ max_new_tokens=128,
24
+ chunk_length_s=30,
25
+ batch_size=16,
26
+ return_timestamps=True,
27
+ torch_dtype=torch_dtype,
28
+ device=device,
29
+ )
30
+ def transcribe_audio(audio_file):
31
+ # Use the pipeline to transcribe audio
32
+ result = pipe(audio_file, generate_kwargs={"language": "english"})
33
+ transcribed_text = result["text"]
34
+ return transcribed_text
35
+
36
+ # Create a Gradio interface
37
+ audio_input = gr.inputs.Audio(label="Upload Audio File")
38
+ output_text = gr.outputs.Textbox(label="Transcribed Text")
39
+
40
+ # Instantiate the Gradio interface
41
+ app = gr.Interface(
42
+ fn=transcribe_audio,
43
+ inputs=audio_input,
44
+ outputs=[
45
+ "textbox"
46
+ ],
47
+ title="Audio Transcription with Whisper Model",
48
+ description="Upload an audio file to transcribe it into text using the Whisper model.",
49
+ theme="compact"
50
+ )
51
+
52
+ # Launch the Gradio interface
53
+ app.launch(debug=True,inline=False)