Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- myinfer_latest.py +7 -9
- space.py +172 -0
myinfer_latest.py
CHANGED
|
@@ -30,7 +30,7 @@ from queue import Empty
|
|
| 30 |
from pydub import AudioSegment
|
| 31 |
from flask_dance.contrib.google import make_google_blueprint, google
|
| 32 |
import io
|
| 33 |
-
|
| 34 |
import boto3
|
| 35 |
|
| 36 |
|
|
@@ -49,14 +49,7 @@ split_model="htdemucs"
|
|
| 49 |
convert_voice_lock = Lock()
|
| 50 |
#concurrent= os.getenv('concurrent', '')
|
| 51 |
# Define the maximum number of concurrent requests
|
| 52 |
-
|
| 53 |
-
try:
|
| 54 |
-
# Try to convert the environment variable to an integer, defaulting to 5 if not set.
|
| 55 |
-
MAX_CONCURRENT_REQUESTS = int(os.getenv('concurrent', '5'))
|
| 56 |
-
except ValueError:
|
| 57 |
-
# If conversion fails, log an error or use a fallback value.
|
| 58 |
-
print("Warning: Environment variable 'concurrent' is not a valid integer. Using default value of 5.")
|
| 59 |
-
MAX_CONCURRENT_REQUESTS = 5
|
| 60 |
|
| 61 |
|
| 62 |
# Initialize the semaphore with the maximum number of concurrent requests
|
|
@@ -226,6 +219,10 @@ def download_file(filename):
|
|
| 226 |
def list_weights():
|
| 227 |
directory = 'weights'
|
| 228 |
files = os.listdir(directory)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
# Extract filenames without their extensions
|
| 230 |
filenames = [os.path.splitext(file)[0] for file in files if os.path.isfile(os.path.join(directory, file))]
|
| 231 |
return jsonify(filenames)
|
|
@@ -383,6 +380,7 @@ def get_processed_audio(audio_id):
|
|
| 383 |
|
| 384 |
def worker(spk_id, input_audio_path, voice_transform, unique_id, output_queue):
|
| 385 |
try:
|
|
|
|
| 386 |
output_audio_path = convert_voice(spk_id, input_audio_path, voice_transform, unique_id)
|
| 387 |
print("output in worker for audio file", output_audio_path)
|
| 388 |
output_queue.put(output_audio_path)
|
|
|
|
| 30 |
from pydub import AudioSegment
|
| 31 |
from flask_dance.contrib.google import make_google_blueprint, google
|
| 32 |
import io
|
| 33 |
+
from space import list_models
|
| 34 |
import boto3
|
| 35 |
|
| 36 |
|
|
|
|
| 49 |
convert_voice_lock = Lock()
|
| 50 |
#concurrent= os.getenv('concurrent', '')
|
| 51 |
# Define the maximum number of concurrent requests
|
| 52 |
+
MAX_CONCURRENT_REQUESTS=10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
# Initialize the semaphore with the maximum number of concurrent requests
|
|
|
|
| 219 |
def list_weights():
|
| 220 |
directory = 'weights'
|
| 221 |
files = os.listdir(directory)
|
| 222 |
+
email = request.args.get('email', default='')
|
| 223 |
+
if not email:
|
| 224 |
+
return jsonify({"error": "Email parameter is required"}), 400
|
| 225 |
+
list_models(email)
|
| 226 |
# Extract filenames without their extensions
|
| 227 |
filenames = [os.path.splitext(file)[0] for file in files if os.path.isfile(os.path.join(directory, file))]
|
| 228 |
return jsonify(filenames)
|
|
|
|
| 380 |
|
| 381 |
def worker(spk_id, input_audio_path, voice_transform, unique_id, output_queue):
|
| 382 |
try:
|
| 383 |
+
|
| 384 |
output_audio_path = convert_voice(spk_id, input_audio_path, voice_transform, unique_id)
|
| 385 |
print("output in worker for audio file", output_audio_path)
|
| 386 |
output_queue.put(output_audio_path)
|
space.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from boto3 import session
|
| 3 |
+
from botocore.client import Config
|
| 4 |
+
import os
|
| 5 |
+
ACCESS_ID = os.getenv('ACCESS_ID', '')
|
| 6 |
+
SECRET_KEY = os.getenv('SECRET_KEY', '')
|
| 7 |
+
|
| 8 |
+
LOCAL_WEIGHTS_DIR = 'weights'
|
| 9 |
+
DO_SPACE='sing'
|
| 10 |
+
def upload_to_do(file_path):
|
| 11 |
+
|
| 12 |
+
boto_session=session.Session()
|
| 13 |
+
client = boto_session.client('s3',
|
| 14 |
+
region_name='nyc3',
|
| 15 |
+
endpoint_url='https://nyc3.digitaloceanspaces.com',
|
| 16 |
+
aws_access_key_id=ACCESS_ID,
|
| 17 |
+
aws_secret_access_key=SECRET_KEY)
|
| 18 |
+
filename_only = os.path.basename(file_path)
|
| 19 |
+
# Upload a file to your Space
|
| 20 |
+
response=client.upload_file(file_path, 'sing', filename_only)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
return response
|
| 24 |
+
|
| 25 |
+
def download_from_do(file_key):
|
| 26 |
+
boto_session = session.Session()
|
| 27 |
+
client = boto_session.client('s3',
|
| 28 |
+
region_name='nyc3',
|
| 29 |
+
endpoint_url='https://nyc3.digitaloceanspaces.com',
|
| 30 |
+
aws_access_key_id=ACCESS_ID,
|
| 31 |
+
aws_secret_access_key=SECRET_KEY)
|
| 32 |
+
|
| 33 |
+
# Ensure the downloads directory exists
|
| 34 |
+
downloads_dir = 'downloads'
|
| 35 |
+
if not os.path.exists(downloads_dir):
|
| 36 |
+
os.makedirs(downloads_dir)
|
| 37 |
+
|
| 38 |
+
# Set the full local path for the download
|
| 39 |
+
full_local_path = os.path.join(downloads_dir, file_key)
|
| 40 |
+
|
| 41 |
+
# Download the file from your Space
|
| 42 |
+
client.download_file('sing', file_key, full_local_path)
|
| 43 |
+
|
| 44 |
+
# Verify the download
|
| 45 |
+
if os.path.exists(full_local_path):
|
| 46 |
+
print(f"File downloaded successfully to {full_local_path}")
|
| 47 |
+
return full_local_path
|
| 48 |
+
else:
|
| 49 |
+
print("Download failed.")
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
def get_local_models(prefix):
|
| 53 |
+
"""Get list of model files starting with prefix in the local directory."""
|
| 54 |
+
models = [f for f in os.listdir(LOCAL_WEIGHTS_DIR) if f.startswith(prefix) and f.endswith('.pth')]
|
| 55 |
+
return models
|
| 56 |
+
|
| 57 |
+
def get_do_models(client, prefix):
|
| 58 |
+
"""Get list of model files starting with prefix in the DO space."""
|
| 59 |
+
paginator = client.get_paginator('list_objects')
|
| 60 |
+
page_iterator = paginator.paginate(Bucket=DO_SPACE, Prefix=prefix)
|
| 61 |
+
|
| 62 |
+
models = []
|
| 63 |
+
for page in page_iterator:
|
| 64 |
+
models.extend([obj['Key'] for obj in page['Contents'] if obj['Key'].endswith('.pth')])
|
| 65 |
+
return models
|
| 66 |
+
|
| 67 |
+
def sync_missing_models(client, local_models, do_models):
|
| 68 |
+
"""Download missing model files from DO space."""
|
| 69 |
+
missing_models = set(do_models) - set(local_models)
|
| 70 |
+
print('missing models:',missing_models)
|
| 71 |
+
for model in missing_models:
|
| 72 |
+
client.download_file(DO_SPACE, model, os.path.join(LOCAL_WEIGHTS_DIR, model))
|
| 73 |
+
print(f"Downloaded {model} from DO space to local weights directory.")
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def list_models(email_prefix):
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
#ensure_local_directory_exists()
|
| 81 |
+
local_models = get_local_models(email_prefix)
|
| 82 |
+
|
| 83 |
+
# Initialize DO S3 client
|
| 84 |
+
boto_session = session.Session()
|
| 85 |
+
client = boto_session.client('s3',
|
| 86 |
+
region_name='nyc3',
|
| 87 |
+
endpoint_url='https://nyc3.digitaloceanspaces.com',
|
| 88 |
+
aws_access_key_id=ACCESS_ID,
|
| 89 |
+
aws_secret_access_key=SECRET_KEY)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
do_models = get_do_models(client, email_prefix)
|
| 93 |
+
sync_missing_models(client, local_models, do_models)
|
| 94 |
+
|
| 95 |
+
# Return the updated list of local models after syncing
|
| 96 |
+
updated_local_models = get_local_models(email_prefix)
|
| 97 |
+
print(updated_local_models)
|
| 98 |
+
#return jsonify(updated_local_models)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def download_from_do_with_prefix(prefix):
|
| 103 |
+
boto_session = session.Session()
|
| 104 |
+
client = boto_session.client('s3',
|
| 105 |
+
region_name='nyc3',
|
| 106 |
+
endpoint_url='https://nyc3.digitaloceanspaces.com',
|
| 107 |
+
aws_access_key_id=ACCESS_ID,
|
| 108 |
+
aws_secret_access_key=SECRET_KEY)
|
| 109 |
+
|
| 110 |
+
# Ensure the downloads directory exists
|
| 111 |
+
downloads_dir = 'downloads'
|
| 112 |
+
if not os.path.exists(downloads_dir):
|
| 113 |
+
os.makedirs(downloads_dir)
|
| 114 |
+
|
| 115 |
+
# List objects in the Space with the specified prefix
|
| 116 |
+
response = client.list_objects(Bucket='sing', Prefix=prefix)
|
| 117 |
+
print(response)
|
| 118 |
+
downloaded_files = []
|
| 119 |
+
if 'Contents' in response:
|
| 120 |
+
for obj in response['Contents']:
|
| 121 |
+
file_key = obj['Key']
|
| 122 |
+
|
| 123 |
+
# Set the full local path for the download
|
| 124 |
+
full_local_path = os.path.join(downloads_dir, os.path.basename(file_key))
|
| 125 |
+
|
| 126 |
+
# Download the file from your Space
|
| 127 |
+
client.download_file('sing', file_key, full_local_path)
|
| 128 |
+
|
| 129 |
+
# Verify the download and add to the list if successful
|
| 130 |
+
if os.path.exists(full_local_path):
|
| 131 |
+
print(f"File downloaded successfully to {full_local_path}")
|
| 132 |
+
downloaded_files.append(full_local_path)
|
| 133 |
+
else:
|
| 134 |
+
print(f"Download failed for {file_key}.")
|
| 135 |
+
else:
|
| 136 |
+
print("No files found with the specified prefix.")
|
| 137 |
+
|
| 138 |
+
return downloaded_files if downloaded_files else None
|
| 139 |
+
# Initiate session
|
| 140 |
+
|
| 141 |
+
def ensure_model_in_weights_dir(model_name):
|
| 142 |
+
weights_dir = 'weights'
|
| 143 |
+
model_path = os.path.join(weights_dir, model_name)
|
| 144 |
+
|
| 145 |
+
# Check if the model already exists
|
| 146 |
+
if os.path.exists(model_path):
|
| 147 |
+
print(f"Model {model_name} already exists in {weights_dir}.")
|
| 148 |
+
return True
|
| 149 |
+
|
| 150 |
+
# If the model does not exist, attempt to download it
|
| 151 |
+
print(f"Model {model_name} not found in {weights_dir}. Attempting to download...")
|
| 152 |
+
|
| 153 |
+
# Initialize a session using DigitalOcean Spaces
|
| 154 |
+
boto_session = session.Session()
|
| 155 |
+
client = boto_session.client('s3',
|
| 156 |
+
region_name='nyc3',
|
| 157 |
+
endpoint_url='https://nyc3.digitaloceanspaces.com',
|
| 158 |
+
aws_access_key_id=ACCESS_ID,
|
| 159 |
+
aws_secret_access_key=SECRET_KEY)
|
| 160 |
+
|
| 161 |
+
# Ensure the weights directory exists
|
| 162 |
+
if not os.path.exists(weights_dir):
|
| 163 |
+
os.makedirs(weights_dir)
|
| 164 |
+
|
| 165 |
+
# Attempt to download the model file
|
| 166 |
+
try:
|
| 167 |
+
client.download_file('sing', f"weights/{model_name}", model_path)
|
| 168 |
+
print(f"Model {model_name} downloaded successfully to {model_path}.")
|
| 169 |
+
return True
|
| 170 |
+
except Exception as e:
|
| 171 |
+
print(f"Failed to download {model_name}: {e}")
|
| 172 |
+
return False
|