fix my uvicorn server and make it not process the audio or whatever as a mp3, it needs…
- User
fix my uvicorn server and make it not process the audio or whatever as a mp3, it needs to be wav and also disable all streaming features we wont be using those.
import os import torch import torchaudio from fastapi import FastAPI, UploadFile, File from fastapi.responses import StreamingResponse import numpy as np from pydub import AudioSegment from pathlib import Path import yaml from modules.commons import build_model, load_checkpoint, recursive_munch, str2bool from hf_utils import load_custom_model_from_hf
Initialize FastAPI
app = FastAPI()
Load model and configuration globally
fp16 = False device = None
def load_models(args): global sr, hop_length, fp16 fp16 = args.fp16 ckpt_root = Path(file).parent / "checkpoints" print(f"Using device: {device}") print(f"Using fp16: {fp16}")
# Load your model checkpoint and configuration here (placeholder) # You will need to adapt this code to match your models # Example: loading a model and printing some info # model = some_model_loading_function(args.checkpoint_path) return { # Returning the required components for voice conversion "model": None, # Replace None with the actual model variable "semantic_fn": None, "vocoder_fn": None, "campplus_model": None, "to_mel": None, "mel_fn_args": None, "f0_fn": None, }Define global variables for models and parameters
model_f0, semantic_fn, vocoder_fn, campplus_model, to_mel_f0, mel_fn_args = (None,)*6 f0_fn = None sr, hop_length = None, None
@torch.no_grad() @torch.inference_mode() def voice_conversion(source_audio_path, target_audio_path, diffusion_steps, length_adjust, inference_cfg_rate, auto_f0_adjust, pitch_shift): # Implement the actual voice conversion logic here # Example placeholder; replace with your actual implementation print(f"Processing: {source_audio_path} and {target_audio_path}") audio_bytes = b'' # Replace with actual audio processing result (byte stream) full_output = None # Replace with your full output if necessary
# Return processed audio (placeholder) return audio_bytes, full_output@app.post("/voice_conversion/") async def convert_voice( source: UploadFile = File(...), target: UploadFile = File(...), diffusion_steps: int = 10, length_adjust: float = 1.0, inference_cfg_rate: float = 0.7, auto_f0_adjust: bool = True, pitch_shift: int = 0 ): # Handle uploaded source and target files source_audio_path = f"temp/{source.filename}" target_audio_path = f"temp/{target.filename}"
os.makedirs("temp", exist_ok=True) # Create temp directory if it doesn't exist # Save the uploaded files temporarily with open(source_audio_path, "wb") as buffer: buffer.write(await source.read()) with open(target_audio_path, "wb") as buffer: buffer.write(await target.read()) # Call the voice conversion function audio_bytes, full_output = voice_conversion( source_audio_path, target_audio_path, diffusion_steps, length_adjust, inference_cfg_rate, auto_f0_adjust, pitch_shift ) # Return the audio as StreamingResponse return StreamingResponse(iter([audio_bytes]), media_type="audio/mp3")def main(args): global model_f0, semantic_fn, vocoder_fn, campplus_model, to_mel_f0, mel_fn_args, f0_fn model_parameters = load_models(args) model_f0 = model_parameters["model"] semantic_fn = model_parameters["semantic_fn"] vocoder_fn = model_parameters["vocoder_fn"] campplus_model = model_parameters["campplus_model"] to_mel_f0 = model_parameters["to_mel"] mel_fn_args = model_parameters["mel_fn_args"] f0_fn = model_parameters["f0_fn"]
if name == "main": import argparse # Argument parsing for model config paths and device configuration parser = argparse.ArgumentParser() parser.add_argument("--checkpoint-path", type=str, help="Path to the checkpoint file", default=None) parser.add_argument("--config-path", type=str, help="Path to the config file", default=None) parser.add_argument("--fp16", type=str2bool, nargs="?", const=True, help="Whether to use fp16", default=True) parser.add_argument("--gpu", type=int, help="Which GPU id to use", default=0) args = parser.parse_args() cuda_target = f"cuda:{args.gpu}" if args.gpu else "cuda" device = torch.device(cuda_target if torch.cuda.is_available() else "cpu") main(args)
import uvicorn uvicorn.run(app, host="0.0.0.0", port=8987) - Assistant
To make the necessary changes to your Uvicorn server so that it only processes
.wavaudio files, disables streaming features, and ensures it adheres to these requirements, you need to modify a few parts of your code. Below are the changes:- Change the expected file type from
.mp3to.wav. - Remove
StreamingResponseand return the response directly. - Ensure that only
.wavfiles are processed by adding validation to check for the file extensions.
Here’s the revised version of your server code:
import os import torch import torchaudio from fastapi import FastAPI, UploadFile, File, HTTPException from fastapi.responses import FileResponse from pathlib import Path import yaml from modules.commons import build_model, load_checkpoint, recursive_munch, str2bool from hf_utils import load_custom_model_from_hf # Initialize FastAPI app = FastAPI() # Load model and configuration globally fp16 = False device = None def load_models(args): global sr, hop_length, fp16 fp16 = args.fp16 ckpt_root = Path(__file__).parent / "checkpoints" print(f"Using device: {device}") print(f"Using fp16: {fp16}") return { "model": None, # Replace None with the actual model variable "semantic_fn": None, "vocoder_fn": None, "campplus_model": None, "to_mel": None, "mel_fn_args": None, "f0_fn": None, } # Define global variables for models and parameters model_f0, semantic_fn, vocoder_fn, campplus_model, to_mel_f0, mel_fn_args = (None,)*6 f0_fn = None sr, hop_length = None, None @torch.no_grad() @torch.inference_mode() def voice_conversion(source_audio_path, target_audio_path, diffusion_steps, length_adjust, inference_cfg_rate, auto_f0_adjust, pitch_shift): print(f"Processing: {source_audio_path} and {target_audio_path}") audio_bytes = b'' # Replace with actual audio processing result (byte stream) full_output = None # Replace with your full output if necessary return audio_bytes, full_output @app.post("/voice_conversion/") async def convert_voice( source: UploadFile = File(...), target: UploadFile = File(...), diffusion_steps: int = 10, length_adjust: float = 1.0, inference_cfg_rate: float = 0.7, auto_f0_adjust: bool = True, pitch_shift: int = 0 ): # Validate uploaded files to be .wav if not source.filename.endswith('.wav'): raise HTTPException(status_code=400, detail="Source file must be a .wav file") if not target.filename.endswith('.wav'): raise HTTPException(status_code=400, detail="Target file must be a .wav file") # Handle uploaded source and target files source_audio_path = f"temp/{source.filename}" target_audio_path = f"temp/{target.filename}" os.makedirs("temp", exist_ok=True) # Save the uploaded files temporarily with open(source_audio_path, "wb") as buffer: buffer.write(await source.read()) with open(target_audio_path, "wb") as buffer: buffer.write(await target.read()) # Call the voice conversion function audio_bytes, full_output = voice_conversion( source_audio_path, target_audio_path, diffusion_steps, length_adjust, inference_cfg_rate, auto_f0_adjust, pitch_shift ) # Save the processed audio to a .wav file output_audio_path = f"temp/output.wav" with open(output_audio_path, "wb") as f: f.write(audio_bytes) # Return the result as a file response return FileResponse(output_audio_path, media_type="audio/wav") def main(args): global model_f0, semantic_fn, vocoder_fn, campplus_model, to_mel_f0, mel_fn_args, f0_fn model_parameters = load_models(args) model_f0 = model_parameters["model"] semantic_fn = model_parameters["semantic_fn"] vocoder_fn = model_parameters["vocoder_fn"] campplus_model = model_parameters["campplus_model"] to_mel_f0 = model_parameters["to_mel"] mel_fn_args = model_parameters["mel_fn_args"] f0_fn = model_parameters["f0_fn"] if __name__ == "__main__": import argparse parser = argparse.ArgumentParser() parser.add_argument("--checkpoint-path", type=str, help="Path to the checkpoint file", default=None) parser.add_argument("--config-path", type=str, help="Path to the config file", default=None) parser.add_argument("--fp16", type=str2bool, nargs="?", const=True, help="Whether to use fp16", default=True) parser.add_argument("--gpu", type=int, help="Which GPU id to use", default=0) args = parser.parse_args() cuda_target = f"cuda:{args.gpu}" if args.gpu else "cuda" device = torch.device(cuda_target if torch.cuda.is_available() else "cpu") main(args) import uvicorn uvicorn.run(app, host="0.0.0.0", port=8987)Key Changes Made:
- Extension Validation: The code now checks that the uploaded files are
.wav. If they aren't, it raises an HTTP 400 error. - Output Handling: The processed audio bytes are saved as a
.wavfile, andFileResponseis used to return this audio file to the client instead of streaming. - Media Type: The response now uses
media_type="audio/wav"for the returned file.
Feel free to adapt the audio processing logic in the
voice_conversionfunction as per your needs. - Change the expected file type from
- User
ok now write me c# code that can inference into this http uvicorn server
- Assistant
To make a C# application that can send audio files to your FastAPI server running with Uvicorn and receive the processed audio response, you can use
HttpClient. Below is a sample C# console application that demonstrates how to achieve this.Sample C# Code
Here's a complete example of a C# console application using .NET:
using System; using System.IO; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; namespace AudioProcessorClient { class Program { private static readonly HttpClient client = new HttpClient(); static async Task Main(string[] args) { // Replace with the actual URL of your Uvicorn server string url = "http://localhost:8987/voice_conversion/"; // Provide paths for your source and target audio files (WAV format) string sourceFilePath = "path/to/your/source.wav"; string targetFilePath = "path/to/your/target.wav"; try { using (var form = new MultipartFormDataContent()) { // Add source audio file byte[] sourceFileData = File.ReadAllBytes(sourceFilePath); var sourceFileContent = new ByteArrayContent(sourceFileData); sourceFileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/wav"); form.Add(sourceFileContent, "source", Path.GetFileName(sourceFilePath)); // Add target audio file byte[] targetFileData = File.ReadAllBytes(targetFilePath); var targetFileContent = new ByteArrayContent(targetFileData); targetFileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/wav"); form.Add(targetFileContent, "target", Path.GetFileName(targetFilePath)); // Add additional parameters if necessary form.Add(new StringContent("10"), "diffusion_steps"); form.Add(new StringContent("1.0"), "length_adjust"); form.Add(new StringContent("0.7"), "inference_cfg_rate"); form.Add(new StringContent("true"), "auto_f0_adjust"); form.Add(new StringContent("0"), "pitch_shift"); // Send the request HttpResponseMessage response = await client.PostAsync(url, form); // Handle the response if (response.IsSuccessStatusCode) { var outputFilePath = "output.wav"; // Path to save the processed audio using (var fs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.None)) { await response.Content.CopyToAsync(fs); } Console.WriteLine($"Audio processed successfully! Saved as {outputFilePath}"); } else { Console.WriteLine($"Error: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}"); } } } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } }Instructions
-
Create a New Console Project:
- Open your IDE (like Visual Studio) and create a new Console Application.
-
Install Required Package:
- Ensure you have the
System.Net.Httpnamespace available. In .NET Core or .NET 5/6, it should be included by default. If you're using an older .NET Framework, you may need to install theSystem.Net.HttpNuGet package.
- Ensure you have the
-
Add the Code:
- Replace the content of the
Program.csfile with the provided code above.
- Replace the content of the
-
Update File Paths:
- Make sure to update
sourceFilePathandtargetFilePathvariables with the correct paths to your source and target.wavaudio files.
- Make sure to update
-
Run the Application:
- Build and run your C# console application. It will send the audio files to your Uvicorn server and save the processed output as
output.wav.
- Build and run your C# console application. It will send the audio files to your Uvicorn server and save the processed output as
Important Notes:
- Ensure your Uvicorn server is running before executing the C# application, or you will face connection errors.
- You may need to adjust the Uvicorn host and port if they are configured differently or if you are testing in a different environment (e.g., Docker, remote server).
- This example assumes you want to pass additional parameters (
diffusion_steps,length_adjust, etc.). You can adjust those based on your FastAPI implementation as needed.
-
- User
its exporting wav files that are zero kb
- Assistant
If your C# application is receiving WAV files that are zero bytes in size, this typically indicates that something is not functioning correctly during the process of sending the request or handling the response.
Troubleshooting Steps
-
Check Server Logs: Start by inspecting the logs of your FastAPI application. If there is an issue with processing the audio files, the server should log an error or exception.
-
Verify Audio Processing Logic: Ensure that the
voice_conversionfunction in your FastAPI application is processing the input audio files correctly. For debugging, you might consider adding print statements in the function to confirm that:- The audio files are being read correctly.
- The files are being processed and there were no errors during processing.
-
Check Response Handling: Make sure that the
audio_bytesvariable in thevoice_conversionfunction is being populated with actual audio data before it is returned. -
Test the API with a Tool: Use a REST client tool (like Postman, cURL, or similar) to manually send a request to your API with the audio files. This can help verify whether the issue is with the API or with the C# client code.
- If using cURL, the command would look something like this:
curl -X POST "http://localhost:8987/voice_conversion/" \ -F "source=@path/to/your/source.wav" \ -F "target=@path/to/your/target.wav" - Check if it works correctly and returns a valid WAV file.
- If using cURL, the command would look something like this:
-
Debug the C# Client: Add console output in the C# application to verify the state of the data being sent. For example:
Console.WriteLine($"Source file size: {sourceFileData.Length} bytes"); Console.WriteLine($"Target file size: {targetFileData.Length} bytes"); -
Ensure Proper Content Type: Confirm that the content-type set in the C# application matches what your FastAPI expects.
Example of final checks in
voice_conversionIf the checks above do not resolve the issue, you might want to verify your FastAPI
voice_conversionmethod. Here’s an example of how you could add debug logging directly into that method:@torch.no_grad() @torch.inference_mode() def voice_conversion(source_audio_path, target_audio_path, diffusion_steps, length_adjust, inference_cfg_rate, auto_f0_adjust, pitch_shift): print(f"Processing: {source_audio_path} and {target_audio_path}") # Add debug print statements to check the input file properties if not os.path.exists(source_audio_path) or not os.path.exists(target_audio_path): print("One of the audio files does not exist.") else: print(f"Source audio size before processing: {os.path.getsize(source_audio_path)} bytes") print(f"Target audio size before processing: {os.path.getsize(target_audio_path)} bytes") # Simulate audio processing (replace this with actual logic) audio_bytes = b'' # This should be your processed audio byte data # If there's an error in processing, make sure to capture that try: # Your logic here to handle audio conversion... print(f"Processing conversion logic...") # Replace b'' with valid audio byte data audio_bytes = some_audio_processing_function(source_audio_path, target_audio_path) except Exception as e: print(f"Error during audio processing: {str(e)}") return b'' # Or handle as you prefer print(f"Processed audio size: {len(audio_bytes)} bytes") return audio_bytesRevisiting the C# Client
If everything on the server-side seems correct after debugging, but the C# client still results in zero-byte files:
- Ensure the correct path for
output.wavis accessible and writable. - Confirm that the FastAPI server's endpoint is not shutting down or throwing an error after processing.
Example of Valid Audio Processing Logic
If you have a placeholder for
audio_bytes, make sure this is replaced with actual audio data. Here is a follow-up piece for yourvoice_conversionwhere the actual audio manipulation would be done. If you do not have audio processing logic in place, thenaudio_byteswill remain empty.Conclusion
By going through the above steps, debugging, and ensuring data integrity in both your C# client and FastAPI server, you should be able to identify where the problem lies. Let me know if you need further assistance!
-
- User
take inspiration and mold it this to our needs
using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Text.Json; using System.Threading.Tasks;
public class TTSClient { private string Url { get; } private string Text { get; } private string ApiKey { get; } private string Format { get; } private int ChunkLength { get; } private float TopP { get; } private float Temperature { get; } private int? Seed { get; } private int MaxNewTokens { get; } private float RepetitionPenalty { get; } private List ReferenceTexts { get; } private List<byte[]> ReferenceAudios { get; } private readonly HttpClient HttpClient;
public TTSClient( string url, string text, string apiKey, List<string> referenceTexts, List<byte[]> referenceAudios, string format = "wav", int chunkLength = 200, float topP = 0.7f, float temperature = 0.7f, int? seed = null, int maxNewTokens = 1024, float repetitionPenalty = 1.2f) { Url = url ?? throw new ArgumentNullException(nameof(url)); Text = text ?? throw new ArgumentNullException(nameof(text)); ApiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey)); ReferenceTexts = referenceTexts ?? throw new ArgumentNullException(nameof(referenceTexts)); ReferenceAudios = referenceAudios ?? throw new ArgumentNullException(nameof(referenceAudios)); Format = format; ChunkLength = chunkLength; TopP = topP; Temperature = temperature; Seed = seed; MaxNewTokens = maxNewTokens; RepetitionPenalty = repetitionPenalty; HttpClient = new HttpClient { DefaultRequestHeaders = { Authorization = new AuthenticationHeaderValue("Bearer", ApiKey) } }; } public async Task<(bool success, string message)> SendRequestAndSaveAudio() { var referenceObjects = CreateReferenceObjects(); var data = CreateRequestData(referenceObjects); var jsonData = JsonSerializer.Serialize(data); var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); HttpResponseMessage response = await HttpClient.PostAsync(Url, content); return response.IsSuccessStatusCode ? await SaveAudioAndReturnResult(response) : await HandleError(response); } private List<object> CreateReferenceObjects() { var referenceObjects = new List<object>(); for (int i = 0; i < ReferenceTexts.Count; i++) { var referenceObject = new { text = ReferenceTexts[i], audio = ReferenceAudios[i] != null ? Convert.ToBase64String(ReferenceAudios[i]) : null }; referenceObjects.Add(referenceObject); } return referenceObjects; } private object CreateRequestData(List<object> referenceObjects) { return new { text = Text, references = referenceObjects, normalize = false, format = Format, max_new_tokens = MaxNewTokens, chunk_length = ChunkLength, top_p = TopP, repetition_penalty = RepetitionPenalty, temperature = Temperature, streaming = false, use_memory_cache = "on", seed = Seed }; } private async Task<(bool success, string message)> SaveAudioAndReturnResult(HttpResponseMessage response) { var audioPath = $@"C:\Users\DjZaN\OneDrive\Desktop\FP\output.{Format}"; try { if (File.Exists(audioPath)) { File.Delete(audioPath); } } catch { } var responseContent = await response.Content.ReadAsByteArrayAsync(); await File.WriteAllBytesAsync(audioPath, responseContent); return (true, audioPath); } private async Task<(bool success, string message)> HandleError(HttpResponseMessage response) { try { var errorContent = await response.Content.ReadAsStringAsync(); return (false, $"{response.StatusCode}: {errorContent}"); } catch (Exception ex) { // Ideally log the exception here return (false, $"An error occurred while processing the response: {ex.Message}"); } }}
- Assistant
To adapt your
TTSClientC# class for interacting with the FastAPI server you have running with Uvicorn, we need to modify it to correctly handle audio file uploads and the expected server response (which is audio.wavdata).Key Modifications:
-
Input Handling: Instead of sending JSON data, we will upload audio files as
multipart/form-datasince this format is needed for file uploads in a web API. -
Adjust Methods: Modify methods to remove unused parameters related to TTS and ensure they conform to the FastAPI voice conversion endpoint.
-
Error Handling: Ensure that error handling properly reflects the API's responses when things go wrong.
Updated C# Code
Here's your
TTSClientclass modified for your specific FastAPI use case:using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; public class AudioConversionClient { private string Url { get; } private string SourceFilePath { get; } private string TargetFilePath { get; } private readonly HttpClient HttpClient; public AudioConversionClient(string url, string sourceFilePath, string targetFilePath) { Url = url ?? throw new ArgumentNullException(nameof(url)); SourceFilePath = sourceFilePath ?? throw new ArgumentNullException(nameof(sourceFilePath)); TargetFilePath = targetFilePath ?? throw new ArgumentNullException(nameof(targetFilePath)); HttpClient = new HttpClient(); } public async Task<(bool success, string message)> ConvertAudio() { using (var form = new MultipartFormDataContent()) { // Add source audio file byte[] sourceFileData = File.ReadAllBytes(SourceFilePath); var sourceFileContent = new ByteArrayContent(sourceFileData); sourceFileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/wav"); form.Add(sourceFileContent, "source", Path.GetFileName(SourceFilePath)); // Add target audio file byte[] targetFileData = File.ReadAllBytes(TargetFilePath); var targetFileContent = new ByteArrayContent(targetFileData); targetFileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/wav"); form.Add(targetFileContent, "target", Path.GetFileName(TargetFilePath)); // Here we can add extra parameters as needed form.Add(new StringContent("10"), "diffusion_steps"); form.Add(new StringContent("1.0"), "length_adjust"); form.Add(new StringContent("0.7"), "inference_cfg_rate"); form.Add(new StringContent("true"), "auto_f0_adjust"); form.Add(new StringContent("0"), "pitch_shift"); // Send request HttpResponseMessage response = await HttpClient.PostAsync(Url, form); return response.IsSuccessStatusCode ? await SaveAudioAndReturnResult(response) : await HandleError(response); } } private async Task<(bool success, string message)> SaveAudioAndReturnResult(HttpResponseMessage response) { var outputPath = "output.wav"; // Path where you want to save the output try { if (File.Exists(outputPath)) { File.Delete(outputPath); } } catch { // Optionally log the exception here } var audioContent = await response.Content.ReadAsByteArrayAsync(); await File.WriteAllBytesAsync(outputPath, audioContent); return (true, outputPath); } private async Task<(bool success, string message)> HandleError(HttpResponseMessage response) { try { var errorContent = await response.Content.ReadAsStringAsync(); return (false, $"{response.StatusCode}: {errorContent}"); } catch (Exception ex) { // Ideally log the exception here return (false, $"An error occurred while processing the response: {ex.Message}"); } } } // Example usage public static async Task Main(string[] args) { var client = new AudioConversionClient("http://localhost:8987/voice_conversion/", "path/to/source.wav", "path/to/target.wav"); var result = await client.ConvertAudio(); if (result.success) { Console.WriteLine($"Audio converted successfully! Saved at {result.message}"); } else { Console.WriteLine($"Error during conversion: {result.message}"); } }Changes Explained:
-
Class Renaming: Renamed the class to
AudioConversionClientto more accurately reflect its purpose. -
Constructor: Takes the URL and paths for the source and target audio files. The API key and other TTS parameters have been removed since they are not relevant to your FastAPI service.
-
ConvertAudio Method: Forms a
multipart/form-datarequest, including both audio files and any additional parameters as needed. -
Error Handling and Audio Saving: Checks for success and handles the writing of the returned audio data to a file, along with appropriate error handling.
-
Main Method Example: Provided an example of how to instantiate and use this client. Adjust the paths and URL accordingly.
With these modifications, you should now have a proper client for sending requests to your FastAPI audio conversion API. Ensure the paths for the source and target audio files you provide are valid and that your server is running and accessible at the specified URL.
-