futurespyhi commited on
Commit
966b1a5
·
1 Parent(s): 6cfc4a3

Improve app.py: unified text input experience and performance optimizations

Browse files

- Unify text and voice input processing to use conversational generation logic
- Optimize flash-attn compilation with dynamic CPU core allocation using min(4, cpu_count())
- Update performance notice to reflect realistic generation time (6-8 minutes)
- Add multiprocessing import for CPU count detection

Files changed (1) hide show
  1. app.py +14 -30
app.py CHANGED
@@ -4,6 +4,7 @@ MiloMusic - Hugging Face Spaces Version
4
  AI-powered music generation platform optimized for cloud deployment with high-performance configuration.
5
  """
6
 
 
7
  import os
8
  import sys
9
  import subprocess
@@ -25,7 +26,7 @@ def setup_spaces_environment():
25
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
26
  os.environ["HF_HUB_CACHE"] = "/tmp/hf_hub_cache"
27
 
28
- # PyTorch CUDA memory optimization
29
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
30
 
31
  # Set temp directory for audio files
@@ -34,7 +35,7 @@ def setup_spaces_environment():
34
  print("🚀 Environment setup complete for Spaces")
35
 
36
  # Install flash-attn if not already installed
37
- def install_flash_attn():
38
  """Install flash-attn from source with proper compilation flags"""
39
  try:
40
  import flash_attn
@@ -54,7 +55,8 @@ def install_flash_attn():
54
 
55
  # Use more parallel jobs for faster compilation in Spaces
56
  env = os.environ.copy()
57
- env["MAX_JOBS"] = "4" # Utilize more CPU cores
 
58
  env["NVCC_PREPEND_FLAGS"] = "-ccbin /usr/bin/gcc"
59
 
60
  result = subprocess.run(cmd, env=env, capture_output=True, text=True, timeout=1800) # 30 min timeout
@@ -527,46 +529,28 @@ def generate_music_spaces(lyrics: str, genre: str, mood: str, progress=gr.Progre
527
  return f"Error during music generation: {str(e)}"
528
 
529
  def respond(message, state):
530
- """Enhanced response function for lyrics generation"""
531
  try:
532
  # Add user message to conversation
533
  state.conversation.append({"role": "user", "content": message})
534
 
535
- # Generate response using your existing lyrics generation logic
536
- song_structure = generate_structured_lyrics(
537
- state.conversation,
538
- state.genre,
539
- state.mood,
540
- state.theme
541
- )
542
-
543
- # Format the structured lyrics for display
544
- response = format_lyrics(song_structure)
545
 
546
  # Add assistant response
547
  state.conversation.append({"role": "assistant", "content": response})
548
 
549
- # Update lyrics if this looks like final lyrics
550
- if any(marker in response.lower() for marker in ["[verse", "[chorus", "[bridge"]):
551
  state.lyrics = response
552
 
553
- # Return conversation for display in messages format
554
- conversation_display = []
555
- for msg in state.conversation:
556
- conversation_display.append({"role": msg["role"], "content": msg["content"]})
557
-
558
- return "", conversation_display, state
559
 
560
  except Exception as e:
561
  error_response = f"Sorry, I encountered an error: {str(e)}"
562
  state.conversation.append({"role": "assistant", "content": error_response})
563
-
564
- # Format conversation for display in messages format
565
- conversation_display = []
566
- for msg in state.conversation:
567
- conversation_display.append({"role": msg["role"], "content": msg["content"]})
568
-
569
- return "", conversation_display, state
570
 
571
  def build_interface():
572
  """Build the Gradio interface optimized for Spaces with high performance"""
@@ -591,7 +575,7 @@ def build_interface():
591
  gr.Markdown("""
592
  <div class="performance-notice">
593
  🚀 <strong>High-Performance Mode:</strong> Running on Spaces GPU with optimized settings for best quality.
594
- Generation time: ~3-5 minutes for professional-grade music with vocals and instruments.
595
  </div>
596
  """)
597
 
 
4
  AI-powered music generation platform optimized for cloud deployment with high-performance configuration.
5
  """
6
 
7
+ import multiprocessing
8
  import os
9
  import sys
10
  import subprocess
 
26
  os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
27
  os.environ["HF_HUB_CACHE"] = "/tmp/hf_hub_cache"
28
 
29
+ # 1.PyTorch CUDA memory optimization 2.用PyTorch的可扩展内存段分配, 提高GPU内存使用效率, 减少内存碎片问题
30
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
31
 
32
  # Set temp directory for audio files
 
35
  print("🚀 Environment setup complete for Spaces")
36
 
37
  # Install flash-attn if not already installed
38
+ def install_flash_attn() -> bool:
39
  """Install flash-attn from source with proper compilation flags"""
40
  try:
41
  import flash_attn
 
55
 
56
  # Use more parallel jobs for faster compilation in Spaces
57
  env = os.environ.copy()
58
+ max_jobs = min(4, multiprocessing.cpu_count()) # Utilize more CPU cores
59
+ env["MAX_JOBS"] = str(max_jobs)
60
  env["NVCC_PREPEND_FLAGS"] = "-ccbin /usr/bin/gcc"
61
 
62
  result = subprocess.run(cmd, env=env, capture_output=True, text=True, timeout=1800) # 30 min timeout
 
529
  return f"Error during music generation: {str(e)}"
530
 
531
  def respond(message, state):
532
+ """Enhanced response function for conversational lyrics generation"""
533
  try:
534
  # Add user message to conversation
535
  state.conversation.append({"role": "user", "content": message})
536
 
537
+ # Use conversational generation logic (same as voice input)
538
+ response = generate_chat_completion(groq_client, state.conversation, state.genre, state.mood, state.theme)
 
 
 
 
 
 
 
 
539
 
540
  # Add assistant response
541
  state.conversation.append({"role": "assistant", "content": response})
542
 
543
+ # Update lyrics with improved format recognition
544
+ if any(marker in response.lower() for marker in ["[verse", "[chorus", "[bridge", "**verse", "**chorus", "sectiontype.verse", "verse:"]):
545
  state.lyrics = response
546
 
547
+ # Format conversation for display
548
+ return "", [{"role": msg["role"], "content": msg["content"]} for msg in state.conversation], state
 
 
 
 
549
 
550
  except Exception as e:
551
  error_response = f"Sorry, I encountered an error: {str(e)}"
552
  state.conversation.append({"role": "assistant", "content": error_response})
553
+ return "", [{"role": msg["role"], "content": msg["content"]} for msg in state.conversation], state
 
 
 
 
 
 
554
 
555
  def build_interface():
556
  """Build the Gradio interface optimized for Spaces with high performance"""
 
575
  gr.Markdown("""
576
  <div class="performance-notice">
577
  🚀 <strong>High-Performance Mode:</strong> Running on Spaces GPU with optimized settings for best quality.
578
+ Generation time: ~6-8 minutes for professional-grade music with vocals and instruments.
579
  </div>
580
  """)
581