hswift commited on
Commit
2d5832a
·
verified ·
1 Parent(s): 1d43544

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -6,7 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware
6
  import io
7
  import torch
8
  from diffusers import AudioLDM2Pipeline
9
- from transformers import GPT2LMHeadModel # <-- IMPORT THE CORRECT MODEL TYPE
10
  from scipy.io.wavfile import write as write_wav
11
  import numpy as np
12
 
@@ -35,26 +35,28 @@ try:
35
 
36
  # --- FIX for Model Component Mismatch ---
37
  # 1. Manually load the correct GPT2 model variant (GPT2LMHeadModel).
38
- # The sub-model 'gpt2' is used by audioldm2 for prompt understanding.
39
  logger.info("Pre-loading the correct language model component (GPT2LMHeadModel)...")
40
  language_model = GPT2LMHeadModel.from_pretrained(
41
  "openai-community/gpt2", cache_dir=cache_dir
42
  ).to(device)
43
  logger.info("Language model component loaded successfully.")
44
 
 
 
 
 
 
45
  # 2. Load the main pipeline, injecting our pre-loaded component.
46
- # This forces the pipeline to use the correct model and avoid the AttributeError.
47
  pipeline = AudioLDM2Pipeline.from_pretrained(
48
  MODEL_REPO,
49
  torch_dtype=torch_dtype,
50
  cache_dir=cache_dir,
51
- language_model=language_model, # <-- INJECT THE CORRECT COMPONENT
52
  )
53
  pipeline = pipeline.to(device)
54
  logger.info("Model loaded successfully and moved to device.")
55
  except Exception as e:
56
  logger.error(f"Fatal error during model loading: {e}", exc_info=True)
57
- # If the model fails to load, the 'pipeline' variable will remain None.
58
 
59
  # --- CORS Middleware ---
60
  app.add_middleware(
 
6
  import io
7
  import torch
8
  from diffusers import AudioLDM2Pipeline
9
+ from transformers import GPT2LMHeadModel
10
  from scipy.io.wavfile import write as write_wav
11
  import numpy as np
12
 
 
35
 
36
  # --- FIX for Model Component Mismatch ---
37
  # 1. Manually load the correct GPT2 model variant (GPT2LMHeadModel).
 
38
  logger.info("Pre-loading the correct language model component (GPT2LMHeadModel)...")
39
  language_model = GPT2LMHeadModel.from_pretrained(
40
  "openai-community/gpt2", cache_dir=cache_dir
41
  ).to(device)
42
  logger.info("Language model component loaded successfully.")
43
 
44
+ # ########################################################################## #
45
+ # ############## THE FINAL FIX: Add the missing configuration ############## #
46
+ language_model.config.max_new_tokens = 32 # This line is the fix.
47
+ # ########################################################################## #
48
+
49
  # 2. Load the main pipeline, injecting our pre-loaded component.
 
50
  pipeline = AudioLDM2Pipeline.from_pretrained(
51
  MODEL_REPO,
52
  torch_dtype=torch_dtype,
53
  cache_dir=cache_dir,
54
+ language_model=language_model,
55
  )
56
  pipeline = pipeline.to(device)
57
  logger.info("Model loaded successfully and moved to device.")
58
  except Exception as e:
59
  logger.error(f"Fatal error during model loading: {e}", exc_info=True)
 
60
 
61
  # --- CORS Middleware ---
62
  app.add_middleware(