Walid-Ahmed commited on
Commit
dc1d260
·
verified ·
1 Parent(s): 436d7be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -35
app.py CHANGED
@@ -1,47 +1,48 @@
1
 
2
 
3
-
4
- import gradio as gr
5
  import whisper
6
- from transformers import pipeline
7
-
8
- # Load the tiny Whisper model
9
-
10
 
11
- # Check if GPU is available and set device accordingly
12
- device = 0 if torch.cuda.is_available() else -1
13
- if device == 0:
14
- print("Running on GPU")
15
- else:
16
- print("Running on CPU")
17
-
18
- whisper_model = whisper.load_model("tiny", device=device)
19
- #model = whisper.load_model("base")
20
 
 
 
21
 
22
- # Load the text summarization model from Hugging Face
23
- summarizer = pipeline(task="summarization", model="facebook/bart-large-cnn", device=device)
24
 
25
- # Function to transcribe and summarize the audio file
26
- def transcribe_and_summarize(audio):
27
- # Step 1: Transcribe the audio using Whisper
28
- transcription_result = whisper_model.transcribe(audio)
29
- transcription = transcription_result['text']
30
 
31
- # Step 2: Summarize the transcription
32
- summary = summarizer(transcription, min_length=10, max_length=100)
33
- summary_text = summary[0]['summary_text']
34
 
35
- return transcription, summary_text
 
 
 
 
 
36
 
37
- # Define the Gradio interface
38
- interface = gr.Interface(
39
- fn=transcribe_and_summarize, # Function to run
40
- inputs=gr.Audio(type="filepath", label="Upload your audio file"), # Input audio field
41
- outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Summary")], # Output fields
42
- title="Whisper Tiny Transcription and Summarization",
43
- description="Upload an audio file, get the transcription from Whisper tiny model and the summarized version using Hugging Face."
 
 
 
 
 
 
44
  )
45
 
46
- # Launch the Gradio app
47
- interface.launch(debug=True)
 
1
 
2
 
 
 
3
  import whisper
4
+ import gradio as gr
5
+ from accelerate import init_empty_weights, load_checkpoint_and_dispatch
6
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
 
7
 
8
+ # Initialize the device map for ZeRO
9
+ from accelerate.utils import set_module_tensor_to_device
10
+ import torch
 
 
 
 
 
 
11
 
12
+ device_map = "auto" # Automatically allocate layers across available GPUs/CPUs
13
+ print(f"Using ZeRO-powered device map: {device_map}")
14
 
15
+ # Load the model using ZeRO
16
+ model_name = "openai/whisper-tiny"
17
 
18
+ # Load the Whisper model into ZeRO's memory-efficient mode
19
+ with init_empty_weights():
20
+ whisper_model = whisper.load_model(model_name)
 
 
21
 
22
+ # Load tokenizer
23
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
 
24
 
25
+ # Load model with Accelerate/ZeRO
26
+ whisper_model = load_checkpoint_and_dispatch(
27
+ whisper_model,
28
+ device_map=device_map,
29
+ dtype=torch.float16 # Optional: Use mixed precision for further optimization
30
+ )
31
 
32
+ # Define the transcription function
33
+ def transcribe(audio):
34
+ # Perform transcription using the Whisper model
35
+ result = whisper_model.transcribe(audio)
36
+ return result['text']
37
+
38
+ # Create the Gradio interface
39
+ demo = gr.Interface(
40
+ fn=transcribe, # The function to be called for transcription
41
+ inputs=gr.Audio(source="microphone", type="filepath", label="Speak into the microphone"), # Input audio
42
+ outputs=gr.Textbox(label="Transcription"), # Output transcription
43
+ title="Whisper Speech-to-Text with ZeRO", # Title of the interface
44
+ description="Record audio using your microphone and get a transcription using the Whisper model optimized by ZeRO."
45
  )
46
 
47
+ # Launch the Gradio interface
48
+ demo.launch()