UNSAFESUPERINTELLIGENCE commited on
Commit
220d65d
·
1 Parent(s): 271171c

Use lru cached model

Browse files
Files changed (1) hide show
  1. app.py +8 -2
app.py CHANGED
@@ -1,10 +1,15 @@
1
  """Minimum code to run OpenAI Whisper on Hugging Face (incl. ZeroGPU)"""
 
2
  import whisper
3
  import gradio as gr
4
  import spaces
5
 
6
- model = whisper.load_model("base")
7
- model.to("cuda")
 
 
 
 
8
 
9
  @spaces.GPU(duration=120)
10
  def transcribe(file_path: str):
@@ -13,6 +18,7 @@ def transcribe(file_path: str):
13
  Keyword arugments:
14
  file_path -- path to audio file (usually passed from Gradio)
15
  """
 
16
  result = model.transcribe(file_path)
17
  return result["text"]
18
 
 
1
  """Minimum code to run OpenAI Whisper on Hugging Face (incl. ZeroGPU)"""
2
+ from functools import lru_cache
3
  import whisper
4
  import gradio as gr
5
  import spaces
6
 
7
+ @lru_cache(maxsize=1)
8
+ def load_model():
9
+ """Returns a newly loaded or cached model"""
10
+ model = whisper.load_model("base").to("cuda")
11
+ return model
12
+
13
 
14
  @spaces.GPU(duration=120)
15
  def transcribe(file_path: str):
 
18
  Keyword arugments:
19
  file_path -- path to audio file (usually passed from Gradio)
20
  """
21
+ model = load_model()
22
  result = model.transcribe(file_path)
23
  return result["text"]
24