Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,18 @@
|
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
import torchaudio
|
4 |
import soundfile as sf
|
@@ -6,19 +20,32 @@ from flask import Flask, request, jsonify, send_file
|
|
6 |
from flask_cors import CORS
|
7 |
from transformers import Wav2Vec2ForCTC, AutoProcessor, VitsModel, AutoTokenizer
|
8 |
|
9 |
-
# Set cache directories
|
10 |
-
os.environ["HF_HOME"] = "/tmp/hf_home"
|
11 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
|
12 |
-
os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/huggingface_cache"
|
13 |
-
os.environ["TORCH_HOME"] = "/tmp/torch_home"
|
14 |
-
|
15 |
app = Flask(__name__)
|
16 |
CORS(app)
|
17 |
|
18 |
-
# ASR Model
|
19 |
ASR_MODEL_ID = "Coco-18/mms-asr-tgl-en-safetensor"
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Language-specific configurations
|
24 |
LANGUAGE_CODES = {
|
@@ -38,8 +65,14 @@ tts_models = {}
|
|
38 |
tts_processors = {}
|
39 |
for lang, model_id in TTS_MODELS.items():
|
40 |
try:
|
41 |
-
tts_models[lang] = VitsModel.from_pretrained(
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
print(f"β
TTS Model loaded: {lang}")
|
44 |
except Exception as e:
|
45 |
print(f"β Error loading {lang} TTS model: {e}")
|
@@ -47,7 +80,7 @@ for lang, model_id in TTS_MODELS.items():
|
|
47 |
|
48 |
# Constants
|
49 |
SAMPLE_RATE = 16000
|
50 |
-
OUTPUT_DIR = "/tmp/"
|
51 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
52 |
|
53 |
|
@@ -149,5 +182,4 @@ def download_audio(filename):
|
|
149 |
|
150 |
|
151 |
if __name__ == "__main__":
|
152 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
153 |
-
|
|
|
1 |
+
# Set cache directories first, before other imports
|
2 |
import os
|
3 |
+
|
4 |
+
# Set all cache directories to locations within /tmp
|
5 |
+
os.environ["HF_HOME"] = "/tmp/hf_home"
|
6 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
|
7 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/huggingface_hub_cache"
|
8 |
+
os.environ["TORCH_HOME"] = "/tmp/torch_home"
|
9 |
+
os.environ["XDG_CACHE_HOME"] = "/tmp/xdg_cache"
|
10 |
+
|
11 |
+
# Create necessary directories
|
12 |
+
for path in ["/tmp/hf_home", "/tmp/transformers_cache", "/tmp/huggingface_hub_cache", "/tmp/torch_home", "/tmp/xdg_cache"]:
|
13 |
+
os.makedirs(path, exist_ok=True)
|
14 |
+
|
15 |
+
# Now import the rest of the libraries
|
16 |
import torch
|
17 |
import torchaudio
|
18 |
import soundfile as sf
|
|
|
20 |
from flask_cors import CORS
|
21 |
from transformers import Wav2Vec2ForCTC, AutoProcessor, VitsModel, AutoTokenizer
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
app = Flask(__name__)
|
24 |
CORS(app)
|
25 |
|
26 |
+
# ASR Model
|
27 |
ASR_MODEL_ID = "Coco-18/mms-asr-tgl-en-safetensor"
|
28 |
+
print(f"Loading ASR model: {ASR_MODEL_ID}")
|
29 |
+
|
30 |
+
try:
|
31 |
+
asr_processor = AutoProcessor.from_pretrained(
|
32 |
+
ASR_MODEL_ID,
|
33 |
+
cache_dir="/tmp/transformers_cache" # Explicitly set cache_dir
|
34 |
+
)
|
35 |
+
asr_model = Wav2Vec2ForCTC.from_pretrained(
|
36 |
+
ASR_MODEL_ID,
|
37 |
+
cache_dir="/tmp/transformers_cache" # Explicitly set cache_dir
|
38 |
+
)
|
39 |
+
print("β
ASR Model loaded successfully")
|
40 |
+
except Exception as e:
|
41 |
+
print(f"β Error loading ASR model: {str(e)}")
|
42 |
+
# Provide more debugging information
|
43 |
+
import sys
|
44 |
+
print(f"Python version: {sys.version}")
|
45 |
+
print(f"Current working directory: {os.getcwd()}")
|
46 |
+
print(f"Temp directory exists: {os.path.exists('/tmp')}")
|
47 |
+
print(f"Temp directory writeable: {os.access('/tmp', os.W_OK)}")
|
48 |
+
# Let's continue anyway to see if we can at least start the API
|
49 |
|
50 |
# Language-specific configurations
|
51 |
LANGUAGE_CODES = {
|
|
|
65 |
tts_processors = {}
|
66 |
for lang, model_id in TTS_MODELS.items():
|
67 |
try:
|
68 |
+
tts_models[lang] = VitsModel.from_pretrained(
|
69 |
+
model_id,
|
70 |
+
cache_dir="/tmp/transformers_cache" # Explicitly set cache_dir
|
71 |
+
)
|
72 |
+
tts_processors[lang] = AutoTokenizer.from_pretrained(
|
73 |
+
model_id,
|
74 |
+
cache_dir="/tmp/transformers_cache" # Explicitly set cache_dir
|
75 |
+
)
|
76 |
print(f"β
TTS Model loaded: {lang}")
|
77 |
except Exception as e:
|
78 |
print(f"β Error loading {lang} TTS model: {e}")
|
|
|
80 |
|
81 |
# Constants
|
82 |
SAMPLE_RATE = 16000
|
83 |
+
OUTPUT_DIR = "/tmp/audio_outputs"
|
84 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
85 |
|
86 |
|
|
|
182 |
|
183 |
|
184 |
if __name__ == "__main__":
|
185 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|
|