Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,48 @@
|
|
| 1 |
|
| 2 |
|
| 3 |
-
|
| 4 |
-
import gradio as gr
|
| 5 |
import whisper
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 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
|
| 23 |
-
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
transcription_result = whisper_model.transcribe(audio)
|
| 29 |
-
transcription = transcription_result['text']
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
summary_text = summary[0]['summary_text']
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Define the
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
-
# Launch the Gradio
|
| 47 |
-
|
|
|
|
| 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()
|