Spaces:
Sleeping
Sleeping
Upload myinfer_latest.py
Browse files- myinfer_latest.py +51 -49
myinfer_latest.py
CHANGED
@@ -28,11 +28,13 @@ from threading import Lock
|
|
28 |
from multiprocessing import Process, SimpleQueue, set_start_method,get_context
|
29 |
from queue import Empty
|
30 |
from pydub import AudioSegment
|
|
|
31 |
import io
|
32 |
|
33 |
|
34 |
|
35 |
|
|
|
36 |
app = Flask(__name__)
|
37 |
app.secret_key = 'smjain_6789'
|
38 |
now_dir = os.getcwd()
|
@@ -50,6 +52,12 @@ MAX_CONCURRENT_REQUESTS = 2 # Adjust this number as needed
|
|
50 |
request_semaphore = Semaphore(MAX_CONCURRENT_REQUESTS)
|
51 |
|
52 |
task_status_tracker = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
#set_start_method('spawn', force=True)
|
55 |
from lib.infer_pack.models import (
|
@@ -71,18 +79,16 @@ hubert_model = None
|
|
71 |
f0method_mode = ["pm", "harvest", "crepe"]
|
72 |
f0method_info = "PM is fast, Harvest is good but extremely slow, and Crepe effect is good but requires GPU (Default: PM)"
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
82 |
|
83 |
-
def hash_array1(arr):
|
84 |
-
arr_str = np.array2string(arr)
|
85 |
-
return hashlib.md5(arr_str.encode()).hexdigest()
|
86 |
|
87 |
if os.path.isfile("rmvpe.pt"):
|
88 |
f0method_mode.insert(2, "rmvpe")
|
@@ -149,6 +155,35 @@ def cleanup_files(file_paths):
|
|
149 |
except Exception as e:
|
150 |
print(f"Error deleting {path}: {e}")
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
@app.route('/status/<audio_id>', methods=['GET'])
|
153 |
def get_status(audio_id):
|
154 |
# Retrieve the task status using the unique ID
|
@@ -209,7 +244,7 @@ def api_convert_voice():
|
|
209 |
audio_length_minutes = len(audio) / 60000.0 # pydub returns length in milliseconds
|
210 |
|
211 |
if audio_length_minutes > 5:
|
212 |
-
return jsonify({"error": "Audio length exceeds
|
213 |
|
214 |
#created_files = []
|
215 |
# Save the file to a temporary path
|
@@ -431,15 +466,12 @@ def vc_single(
|
|
431 |
protect,
|
432 |
f0_file=f0_file
|
433 |
)
|
434 |
-
|
435 |
|
436 |
# Get the current thread's name or ID
|
437 |
-
thread_name = threading.current_thread().name
|
438 |
-
print(f"Thread {thread_name}: Hash {hash_val}")
|
439 |
-
|
440 |
|
441 |
-
|
442 |
-
|
443 |
if resample_sr >= 16000 and tgt_sr != resample_sr:
|
444 |
tgt_sr = resample_sr
|
445 |
index_info = (
|
@@ -447,7 +479,7 @@ def vc_single(
|
|
447 |
if os.path.exists(file_index)
|
448 |
else "Index not used."
|
449 |
)
|
450 |
-
|
451 |
print("writing to FS")
|
452 |
#output_file_path = os.path.join("output", f"converted_audio_{sid}.wav") # Adjust path as needed
|
453 |
# Assuming 'unique_id' is passed to convert_voice function along with 'sid'
|
@@ -472,37 +504,7 @@ def vc_single(
|
|
472 |
|
473 |
return info, (None, None)
|
474 |
|
475 |
-
def save_audio_with_thread_id(audio_opt, tgt_sr,output_dir="output"):
|
476 |
-
# Ensure the output directory exists
|
477 |
-
os.makedirs(output_dir, exist_ok=True)
|
478 |
-
|
479 |
-
# Get the current thread ID or name
|
480 |
-
thread_id = threading.current_thread().name
|
481 |
-
|
482 |
-
# Construct the filename using the thread ID
|
483 |
-
filename = f"audio_{thread_id}.wav"
|
484 |
-
output_path = os.path.join(output_dir, filename)
|
485 |
-
|
486 |
-
# Assuming the target sample rate is defined elsewhere; replace as necessary
|
487 |
-
#tgt_sr = 16000 # Example sample rate, adjust according to your needs
|
488 |
-
|
489 |
-
# Write the audio file
|
490 |
-
sf.write(output_path, audio_opt, tgt_sr)
|
491 |
-
|
492 |
-
print(f"Saved {output_path}")
|
493 |
|
494 |
-
def sample_and_print(array, thread_name):
|
495 |
-
# Ensure the array has more than 10 elements; otherwise, use the array length
|
496 |
-
num_samples = 10 if len(array) > 10 else len(array)
|
497 |
-
|
498 |
-
# Calculate indices to sample; spread them evenly across the array
|
499 |
-
indices = np.linspace(0, len(array) - 1, num=num_samples, dtype=int)
|
500 |
-
|
501 |
-
# Sample elements
|
502 |
-
sampled_elements = array[indices]
|
503 |
-
|
504 |
-
# Print sampled elements with thread ID
|
505 |
-
print(f"Thread {thread_name}: Sampled Elements: {sampled_elements}")
|
506 |
|
507 |
|
508 |
def get_vc(sid, to_return_protect0):
|
|
|
28 |
from multiprocessing import Process, SimpleQueue, set_start_method,get_context
|
29 |
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 |
|
35 |
|
36 |
|
37 |
+
|
38 |
app = Flask(__name__)
|
39 |
app.secret_key = 'smjain_6789'
|
40 |
now_dir = os.getcwd()
|
|
|
52 |
request_semaphore = Semaphore(MAX_CONCURRENT_REQUESTS)
|
53 |
|
54 |
task_status_tracker = {}
|
55 |
+
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" # ONLY FOR TESTING, REMOVE IN PRODUCTION
|
56 |
+
os.environ["OAUTHLIB_RELAX_TOKEN_SCOPE"] = "1"
|
57 |
+
app.config["GOOGLE_OAUTH_CLIENT_ID"] = "144930881143-n3e3ubers3vkq7jc9doe4iirasgimdt2.apps.googleusercontent.com"
|
58 |
+
app.config["GOOGLE_OAUTH_CLIENT_SECRET"] = "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"
|
59 |
+
google_blueprint = make_google_blueprint(scope=["profile", "email"])
|
60 |
+
app.register_blueprint(google_blueprint, url_prefix="/login")
|
61 |
|
62 |
#set_start_method('spawn', force=True)
|
63 |
from lib.infer_pack.models import (
|
|
|
79 |
f0method_mode = ["pm", "harvest", "crepe"]
|
80 |
f0method_info = "PM is fast, Harvest is good but extremely slow, and Crepe effect is good but requires GPU (Default: PM)"
|
81 |
|
82 |
+
@app.route("/")
|
83 |
+
def index():
|
84 |
+
# Check if user is logged in
|
85 |
+
if google.authorized:
|
86 |
+
return render_template("index.html", logged_in=True)
|
87 |
+
else:
|
88 |
+
return render_template("index.html", logged_in=False)
|
89 |
+
|
90 |
+
|
91 |
|
|
|
|
|
|
|
92 |
|
93 |
if os.path.isfile("rmvpe.pt"):
|
94 |
f0method_mode.insert(2, "rmvpe")
|
|
|
155 |
except Exception as e:
|
156 |
print(f"Error deleting {path}: {e}")
|
157 |
|
158 |
+
@app.route("/create_song")
|
159 |
+
def create_song():
|
160 |
+
if not google.authorized:
|
161 |
+
return redirect(url_for("google.login"))
|
162 |
+
resp = google.get("/oauth2/v2/userinfo")
|
163 |
+
assert resp.ok, resp.text
|
164 |
+
email = resp.json()["email"]
|
165 |
+
user_info = resp.json()
|
166 |
+
user_id = user_info.get("id")
|
167 |
+
name = user_info.get("name")
|
168 |
+
|
169 |
+
#if not user_exists(email):
|
170 |
+
# user_data = {'user_id': user_id, 'user_name': name, 'email': email, 'model_created': 'No', 'time_used': '0','model_id':''}
|
171 |
+
# add_user(user_data)
|
172 |
+
|
173 |
+
#models = get_user_models(email)
|
174 |
+
|
175 |
+
# Assuming we're interested in whether any model has been created
|
176 |
+
#model_exists = len(models) > 0
|
177 |
+
return render_template("ui.html", email=email)
|
178 |
+
@app.route("/logout")
|
179 |
+
def logout():
|
180 |
+
# Clear the session
|
181 |
+
session.clear()
|
182 |
+
#if "google_oauth_token" in session:
|
183 |
+
# del session["google_oauth_token"]
|
184 |
+
return redirect(url_for("index"))
|
185 |
+
|
186 |
+
|
187 |
@app.route('/status/<audio_id>', methods=['GET'])
|
188 |
def get_status(audio_id):
|
189 |
# Retrieve the task status using the unique ID
|
|
|
244 |
audio_length_minutes = len(audio) / 60000.0 # pydub returns length in milliseconds
|
245 |
|
246 |
if audio_length_minutes > 5:
|
247 |
+
return jsonify({"error": "Audio length exceeds 5 minutes"}), 400
|
248 |
|
249 |
#created_files = []
|
250 |
# Save the file to a temporary path
|
|
|
466 |
protect,
|
467 |
f0_file=f0_file
|
468 |
)
|
469 |
+
|
470 |
|
471 |
# Get the current thread's name or ID
|
|
|
|
|
|
|
472 |
|
473 |
+
|
474 |
+
|
475 |
if resample_sr >= 16000 and tgt_sr != resample_sr:
|
476 |
tgt_sr = resample_sr
|
477 |
index_info = (
|
|
|
479 |
if os.path.exists(file_index)
|
480 |
else "Index not used."
|
481 |
)
|
482 |
+
|
483 |
print("writing to FS")
|
484 |
#output_file_path = os.path.join("output", f"converted_audio_{sid}.wav") # Adjust path as needed
|
485 |
# Assuming 'unique_id' is passed to convert_voice function along with 'sid'
|
|
|
504 |
|
505 |
return info, (None, None)
|
506 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
507 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
508 |
|
509 |
|
510 |
def get_vc(sid, to_return_protect0):
|