mirror of
https://github.com/ParisNeo/lollms.git
synced 2025-02-10 12:51:25 +00:00
fixed error in MSG_TYPE
This commit is contained in:
parent
2d5468a204
commit
581a2157c4
@ -4,70 +4,76 @@
|
|||||||
# Description: This function generates music based on a given prompt and duration, saving the output to a unique file in the discussion folder.
|
# Description: This function generates music based on a given prompt and duration, saving the output to a unique file in the discussion folder.
|
||||||
|
|
||||||
# Import necessary libraries
|
# Import necessary libraries
|
||||||
import torchaudio
|
try:
|
||||||
from audiocraft.models import musicgen
|
import torchaudio
|
||||||
import torch
|
from audiocraft.models import musicgen
|
||||||
from pathlib import Path
|
import torch
|
||||||
from lollms.utilities import PackageManager
|
from pathlib import Path
|
||||||
from ascii_colors import trace_exception
|
from lollms.utilities import PackageManager
|
||||||
from functools import partial
|
from ascii_colors import trace_exception
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
# Check for required packages and install if necessary
|
# Check for required packages and install if necessary
|
||||||
if not PackageManager.check_package_installed("audiocraft"):
|
if not PackageManager.check_package_installed("audiocraft"):
|
||||||
PackageManager.install_package("audiocraft")
|
PackageManager.install_package("audiocraft")
|
||||||
|
|
||||||
# Function to generate music
|
# Function to generate music
|
||||||
def generate_music(processor, client, generation_prompt: str, duration: int, model_name: str = "facebook/musicgen-melody", device: str="cuda:0") -> str:
|
def generate_music(processor, client, generation_prompt: str, duration: int, model_name: str = "facebook/musicgen-melody", device: str="cuda:0") -> str:
|
||||||
"""
|
"""
|
||||||
Generates music based on the given prompt and duration, saving it to a unique file in the discussion folder.
|
Generates music based on the given prompt and duration, saving it to a unique file in the discussion folder.
|
||||||
|
|
||||||
Parameters:
|
|
||||||
- processor: The processor object used for managing the generation process.
|
|
||||||
- client: The client object containing discussion information.
|
|
||||||
- generation_prompt: The prompt for music generation.
|
|
||||||
- duration: The duration of the music in seconds.
|
|
||||||
- model_name: The name of the pretrained music generation model.
|
|
||||||
- device: The device to run the model on (e.g., 'cpu' or 'cuda').
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- The path of the saved music file.
|
|
||||||
"""
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Load the pretrained music generation model
|
|
||||||
music_model = musicgen.MusicGen.get_pretrained(model_name, device=device)
|
|
||||||
|
|
||||||
# Set generation parameters
|
Parameters:
|
||||||
music_model.set_generation_params(duration=duration)
|
- processor: The processor object used for managing the generation process.
|
||||||
|
- client: The client object containing discussion information.
|
||||||
|
- generation_prompt: The prompt for music generation.
|
||||||
|
- duration: The duration of the music in seconds.
|
||||||
|
- model_name: The name of the pretrained music generation model.
|
||||||
|
- device: The device to run the model on (e.g., 'cpu' or 'cuda').
|
||||||
|
|
||||||
# Generate music
|
Returns:
|
||||||
res = music_model.generate([generation_prompt])
|
- The path of the saved music file.
|
||||||
|
"""
|
||||||
|
|
||||||
# Create output folder if it doesn't exist
|
try:
|
||||||
output_folder = client.discussion.discussion_folder / "generated_music"
|
# Load the pretrained music generation model
|
||||||
output_folder.mkdir(parents=True, exist_ok=True)
|
music_model = musicgen.MusicGen.get_pretrained(model_name, device=device)
|
||||||
|
|
||||||
|
# Set generation parameters
|
||||||
|
music_model.set_generation_params(duration=duration)
|
||||||
|
|
||||||
|
# Generate music
|
||||||
|
res = music_model.generate([generation_prompt])
|
||||||
|
|
||||||
|
# Create output folder if it doesn't exist
|
||||||
|
output_folder = client.discussion.discussion_folder / "generated_music"
|
||||||
|
output_folder.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# Generate a unique file name
|
# Generate a unique file name
|
||||||
output_file = output_folder / f"music_generation_{len(list(output_folder.glob('*.wav')))}.wav"
|
output_file = output_folder / f"music_generation_{len(list(output_folder.glob('*.wav')))}.wav"
|
||||||
|
|
||||||
# Save the generated music to the specified file
|
# Save the generated music to the specified file
|
||||||
torchaudio.save(output_file, res.reshape(1, -1).cpu(), 32000)
|
torchaudio.save(output_file, res.reshape(1, -1).cpu(), 32000)
|
||||||
|
|
||||||
# Return the path of the saved file
|
# Return the path of the saved file
|
||||||
return str(output_file)
|
return str(output_file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return trace_exception(e)
|
return trace_exception(e)
|
||||||
|
|
||||||
# Metadata function for the music generation function
|
# Metadata function for the music generation function
|
||||||
def generate_music_function(processor, client):
|
def generate_music_function(processor, client):
|
||||||
return {
|
return {
|
||||||
"function_name": "generate_music", # The function name in string
|
"function_name": "generate_music", # The function name in string
|
||||||
"function": partial(generate_music, processor=processor, client=client), # The function to be called with preset parameters
|
"function": partial(generate_music, processor=processor, client=client), # The function to be called with preset parameters
|
||||||
"function_description": "Generates music based on a prompt and duration, saving it to a unique file in the discussion folder.", # Description of the function
|
"function_description": "Generates music based on a prompt and duration, saving it to a unique file in the discussion folder.", # Description of the function
|
||||||
"function_parameters": [ # Parameters needed for the function
|
"function_parameters": [ # Parameters needed for the function
|
||||||
{"name": "generation_prompt", "type": "str"},
|
{"name": "generation_prompt", "type": "str"},
|
||||||
{"name": "duration", "type": "int"},
|
{"name": "duration", "type": "int"},
|
||||||
{"name": "model_name", "type": "str"},
|
{"name": "model_name", "type": "str"},
|
||||||
{"name": "device", "type": "str"}
|
{"name": "device", "type": "str"}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
except:
|
||||||
|
def generate_music(processor, client, generation_prompt: str, duration: int, model_name: str = "facebook/musicgen-melody", device: str="cuda:0") -> str:
|
||||||
|
pass
|
||||||
|
def generate_music_function(processor, client):
|
||||||
|
pass
|
||||||
|
@ -137,7 +137,7 @@ async def lollms_generate(request: LollmsGenerateRequest):
|
|||||||
async def generate_chunks():
|
async def generate_chunks():
|
||||||
lk = threading.Lock()
|
lk = threading.Lock()
|
||||||
|
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
if elf_server.cancel_gen:
|
if elf_server.cancel_gen:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -305,7 +305,7 @@ async def lollms_generate_with_images(request: LollmsGenerateRequest):
|
|||||||
async def generate_chunks():
|
async def generate_chunks():
|
||||||
lk = threading.Lock()
|
lk = threading.Lock()
|
||||||
|
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
if elf_server.cancel_gen:
|
if elf_server.cancel_gen:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -359,7 +359,7 @@ async def lollms_generate_with_images(request: LollmsGenerateRequest):
|
|||||||
elf_server.cancel_gen = False
|
elf_server.cancel_gen = False
|
||||||
return StreamingResponse(generate_chunks(), media_type="text/plain", headers=headers)
|
return StreamingResponse(generate_chunks(), media_type="text/plain", headers=headers)
|
||||||
else:
|
else:
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
# Yield each chunk of data
|
# Yield each chunk of data
|
||||||
if chunk is None:
|
if chunk is None:
|
||||||
return True
|
return True
|
||||||
@ -509,7 +509,7 @@ async def v1_chat_completions(request: ChatGenerationRequest):
|
|||||||
async def generate_chunks():
|
async def generate_chunks():
|
||||||
lk = threading.Lock()
|
lk = threading.Lock()
|
||||||
|
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
if elf_server.cancel_gen:
|
if elf_server.cancel_gen:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -567,7 +567,7 @@ async def v1_chat_completions(request: ChatGenerationRequest):
|
|||||||
elf_server.cancel_gen = False
|
elf_server.cancel_gen = False
|
||||||
return StreamingResponse(generate_chunks(), media_type="application/json")
|
return StreamingResponse(generate_chunks(), media_type="application/json")
|
||||||
else:
|
else:
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
# Yield each chunk of data
|
# Yield each chunk of data
|
||||||
if chunk is None:
|
if chunk is None:
|
||||||
return True
|
return True
|
||||||
@ -651,7 +651,7 @@ async def ollama_chat_completion(request: ChatGenerationRequest):
|
|||||||
async def generate_chunks():
|
async def generate_chunks():
|
||||||
lk = threading.Lock()
|
lk = threading.Lock()
|
||||||
|
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
if elf_server.cancel_gen:
|
if elf_server.cancel_gen:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -709,7 +709,7 @@ async def ollama_chat_completion(request: ChatGenerationRequest):
|
|||||||
elf_server.cancel_gen = False
|
elf_server.cancel_gen = False
|
||||||
return StreamingResponse(generate_chunks(), media_type="application/json")
|
return StreamingResponse(generate_chunks(), media_type="application/json")
|
||||||
else:
|
else:
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
# Yield each chunk of data
|
# Yield each chunk of data
|
||||||
if chunk is None:
|
if chunk is None:
|
||||||
return True
|
return True
|
||||||
@ -805,7 +805,7 @@ async def ollama_generate(request: CompletionGenerationRequest):
|
|||||||
if stream:
|
if stream:
|
||||||
output = {"text":""}
|
output = {"text":""}
|
||||||
def generate_chunks():
|
def generate_chunks():
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
# Yield each chunk of data
|
# Yield each chunk of data
|
||||||
output["text"] += chunk
|
output["text"] += chunk
|
||||||
antiprompt = detect_antiprompt(output["text"], [start_header_id_template, end_header_id_template])
|
antiprompt = detect_antiprompt(output["text"], [start_header_id_template, end_header_id_template])
|
||||||
@ -826,7 +826,7 @@ async def ollama_generate(request: CompletionGenerationRequest):
|
|||||||
return StreamingResponse(generate_chunks())
|
return StreamingResponse(generate_chunks())
|
||||||
else:
|
else:
|
||||||
output = {"text":""}
|
output = {"text":""}
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
if chunk is None:
|
if chunk is None:
|
||||||
return
|
return
|
||||||
# Yield each chunk of data
|
# Yield each chunk of data
|
||||||
@ -891,7 +891,7 @@ async def ollama_completion(request: CompletionGenerationRequest):
|
|||||||
async def generate_chunks():
|
async def generate_chunks():
|
||||||
lk = threading.Lock()
|
lk = threading.Lock()
|
||||||
|
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
if elf_server.cancel_gen:
|
if elf_server.cancel_gen:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -944,7 +944,7 @@ async def ollama_completion(request: CompletionGenerationRequest):
|
|||||||
elf_server.cancel_gen = False
|
elf_server.cancel_gen = False
|
||||||
return StreamingResponse(generate_chunks(), media_type="text/plain")
|
return StreamingResponse(generate_chunks(), media_type="text/plain")
|
||||||
else:
|
else:
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
# Yield each chunk of data
|
# Yield each chunk of data
|
||||||
if chunk is None:
|
if chunk is None:
|
||||||
return True
|
return True
|
||||||
@ -995,7 +995,7 @@ async def v1_completion(request: CompletionGenerationRequest):
|
|||||||
if stream:
|
if stream:
|
||||||
output = {"text":""}
|
output = {"text":""}
|
||||||
def generate_chunks():
|
def generate_chunks():
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
# Yield each chunk of data
|
# Yield each chunk of data
|
||||||
output["text"] += chunk
|
output["text"] += chunk
|
||||||
antiprompt = detect_antiprompt(output["text"])
|
antiprompt = detect_antiprompt(output["text"])
|
||||||
@ -1016,7 +1016,7 @@ async def v1_completion(request: CompletionGenerationRequest):
|
|||||||
return StreamingResponse(generate_chunks())
|
return StreamingResponse(generate_chunks())
|
||||||
else:
|
else:
|
||||||
output = {"text":""}
|
output = {"text":""}
|
||||||
def callback(chunk, chunk_type:MSG_TYPE_CONTENT=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
def callback(chunk, chunk_type:MSG_OPERATION_TYPE=MSG_OPERATION_TYPE.MSG_OPERATION_TYPE_ADD_CHUNK):
|
||||||
# Yield each chunk of data
|
# Yield each chunk of data
|
||||||
output["text"] += chunk
|
output["text"] += chunk
|
||||||
antiprompt = detect_antiprompt(output["text"])
|
antiprompt = detect_antiprompt(output["text"])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user