aimeri commited on
Commit
c98fc82
·
1 Parent(s): 9f83467

Update process_input function in app.py to handle audio generation output more robustly, introducing a fallback mechanism for text generation in case of unexpected output formats. Improve error handling during audio and text generation processes. Additionally, update requirements.txt to include flash-attn for enhanced performance.

Browse files
Files changed (2) hide show
  1. app.py +42 -12
  2. requirements.txt +2 -1
app.py CHANGED
@@ -22,7 +22,7 @@ def get_model():
22
  device_map="auto",
23
  enable_audio_output=True,
24
  low_cpu_mem_usage=True,
25
- # attn_implementation="flash_attention_2" if torch.cuda.is_available() else None
26
  )
27
  return model
28
 
@@ -123,11 +123,12 @@ def process_input(image, audio, video, text, chat_history, voice_type, enable_au
123
  try:
124
  text_ids = None
125
  audio_path = None
 
126
 
127
  if enable_audio_output:
128
  voice_type_value = VOICE_OPTIONS.get(voice_type, "Chelsie")
129
  try:
130
- text_ids, audio = model.generate(
131
  **inputs,
132
  use_audio_in_video=False,
133
  return_audio=True,
@@ -139,18 +140,47 @@ def process_input(image, audio, video, text, chat_history, voice_type, enable_au
139
  streamer=TextStreamer(processor, skip_prompt=True)
140
  )
141
 
142
- if audio is not None:
143
- # Save audio to temporary file
144
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
145
- sf.write(
146
- tmp_file.name,
147
- audio.reshape(-1).detach().cpu().numpy(),
148
- samplerate=24000,
149
- )
150
- audio_path = tmp_file.name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  except Exception as e:
152
  print(f"Error during audio generation: {str(e)}")
153
- text_ids = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  else:
155
  try:
156
  text_ids = model.generate(
 
22
  device_map="auto",
23
  enable_audio_output=True,
24
  low_cpu_mem_usage=True,
25
+ attn_implementation="flash_attention_2" if torch.cuda.is_available() else None
26
  )
27
  return model
28
 
 
123
  try:
124
  text_ids = None
125
  audio_path = None
126
+ generation_output = None
127
 
128
  if enable_audio_output:
129
  voice_type_value = VOICE_OPTIONS.get(voice_type, "Chelsie")
130
  try:
131
+ generation_output = model.generate(
132
  **inputs,
133
  use_audio_in_video=False,
134
  return_audio=True,
 
140
  streamer=TextStreamer(processor, skip_prompt=True)
141
  )
142
 
143
+ if generation_output is not None and isinstance(generation_output, tuple) and len(generation_output) == 2:
144
+ text_ids, audio = generation_output
145
+ if audio is not None:
146
+ # Save audio to temporary file
147
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
148
+ sf.write(
149
+ tmp_file.name,
150
+ audio.reshape(-1).detach().cpu().numpy(),
151
+ samplerate=24000,
152
+ )
153
+ audio_path = tmp_file.name
154
+ else:
155
+ print("Warning: Unexpected generation output format")
156
+ # Fall back to text-only generation
157
+ text_ids = model.generate(
158
+ **inputs,
159
+ use_audio_in_video=False,
160
+ return_audio=False,
161
+ max_new_tokens=512,
162
+ do_sample=True,
163
+ temperature=0.7,
164
+ top_p=0.9,
165
+ streamer=TextStreamer(processor, skip_prompt=True)
166
+ )
167
  except Exception as e:
168
  print(f"Error during audio generation: {str(e)}")
169
+ # Fall back to text-only generation
170
+ try:
171
+ text_ids = model.generate(
172
+ **inputs,
173
+ use_audio_in_video=False,
174
+ return_audio=False,
175
+ max_new_tokens=512,
176
+ do_sample=True,
177
+ temperature=0.7,
178
+ top_p=0.9,
179
+ streamer=TextStreamer(processor, skip_prompt=True)
180
+ )
181
+ except Exception as e:
182
+ print(f"Error during fallback text generation: {str(e)}")
183
+ text_ids = None
184
  else:
185
  try:
186
  text_ids = model.generate(
requirements.txt CHANGED
@@ -5,4 +5,5 @@ torch
5
  gradio
6
  torchvision
7
  torchaudio
8
- accelerate
 
 
5
  gradio
6
  torchvision
7
  torchaudio
8
+ accelerate
9
+ flash-attn