Spaces:
Sleeping

mindSoftPro commited on
Commit
0b56da9
·
verified ·
1 Parent(s): e45e60a

Move load models at start project

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -21,6 +21,18 @@ logging.basicConfig(
21
  )
22
  logger = logging.getLogger(__name__)
23
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  app = FastAPI()
25
 
26
  # Environment variables
@@ -31,8 +43,6 @@ DEFAULT_ENV = os.getenv('DEFAULT_ENVIRONMENT', 'dev') # Default to dev environm
31
  if not HUGGINGFACE_ACCESS_TOKEN:
32
  raise EnvironmentError("Hugging Face access token not found.")
33
 
34
- openai_client = openai.Client(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
35
-
36
  progress_store = {}
37
 
38
  AUTH_TOKEN = os.getenv("API_AUTH_TOKEN")
@@ -181,10 +191,6 @@ def process_diarization(
181
 
182
  # Rest of the function remains the same
183
  update_progress(job_id, 25, "Loading diarization model")
184
- pipeline = Pipeline.from_pretrained(
185
- "pyannote/speaker-diarization-3.1",
186
- use_auth_token=HUGGINGFACE_ACCESS_TOKEN
187
- )
188
 
189
  update_progress(job_id, 40, "Performing speaker diarization")
190
  diarization_start = time.perf_counter()
@@ -195,9 +201,7 @@ def process_diarization(
195
  transcription_start = time.perf_counter()
196
  dialogue = []
197
 
198
- if speech_to_text_source == 'internal':
199
- model = whisper.load_model("turbo")
200
- elif speech_to_text_source == 'openai_api' and not openai_client:
201
  raise ValueError("OpenAI API key not configured")
202
 
203
  segments = list(diarization.itertracks(yield_label=True))
 
21
  )
22
  logger = logging.getLogger(__name__)
23
 
24
+ openai_client = openai.Client(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
25
+
26
+ # Initialize models at server startup
27
+ logger.info("Loading diarization pipeline...")
28
+ pipeline = Pipeline.from_pretrained(
29
+ "pyannote/speaker-diarization-3.1",
30
+ use_auth_token=HUGGINGFACE_ACCESS_TOKEN
31
+ )
32
+
33
+ logger.info("Loading Whisper model...")
34
+ whisper_model = whisper.load_model("turbo")
35
+
36
  app = FastAPI()
37
 
38
  # Environment variables
 
43
  if not HUGGINGFACE_ACCESS_TOKEN:
44
  raise EnvironmentError("Hugging Face access token not found.")
45
 
 
 
46
  progress_store = {}
47
 
48
  AUTH_TOKEN = os.getenv("API_AUTH_TOKEN")
 
191
 
192
  # Rest of the function remains the same
193
  update_progress(job_id, 25, "Loading diarization model")
 
 
 
 
194
 
195
  update_progress(job_id, 40, "Performing speaker diarization")
196
  diarization_start = time.perf_counter()
 
201
  transcription_start = time.perf_counter()
202
  dialogue = []
203
 
204
+ if speech_to_text_source == 'openai_api' and not openai_client:
 
 
205
  raise ValueError("OpenAI API key not configured")
206
 
207
  segments = list(diarization.itertracks(yield_label=True))