Spaces:
Running
Running
Upload 2 files
Browse files- main.py +6 -1
- tts_service.py +45 -2
main.py
CHANGED
|
@@ -35,7 +35,12 @@ load_dotenv()
|
|
| 35 |
HOST = os.getenv("BACKEND_HOST", "0.0.0.0")
|
| 36 |
PORT = int(os.getenv("BACKEND_PORT", "7860")) # Default port for Hugging Face Spaces
|
| 37 |
BASE_URL = os.getenv("BASE_URL", f"http://{HOST}:{PORT}")
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# DeepSeek API Key check
|
| 41 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
|
|
|
| 35 |
HOST = os.getenv("BACKEND_HOST", "0.0.0.0")
|
| 36 |
PORT = int(os.getenv("BACKEND_PORT", "7860")) # Default port for Hugging Face Spaces
|
| 37 |
BASE_URL = os.getenv("BASE_URL", f"http://{HOST}:{PORT}")
|
| 38 |
+
|
| 39 |
+
# For Hugging Face Spaces, use a directory we know has write permissions
|
| 40 |
+
if os.path.exists("/code"): # Check if we're in Hugging Face Spaces environment
|
| 41 |
+
AUDIO_STORAGE_PATH = os.path.abspath("/tmp/audio_files")
|
| 42 |
+
else:
|
| 43 |
+
AUDIO_STORAGE_PATH = os.path.abspath(os.getenv("AUDIO_STORAGE_PATH", "./audio_files"))
|
| 44 |
|
| 45 |
# DeepSeek API Key check
|
| 46 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
tts_service.py
CHANGED
|
@@ -13,7 +13,12 @@ from ai_service import get_complete_story, generate_title_if_missing
|
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# الحصول على مسار تخزين ملفات الصوت
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
BASE_URL = os.getenv("BASE_URL", "http://localhost:8000")
|
| 18 |
|
| 19 |
# قاموس لتخزين معرفات الملفات الصوتية للقصص
|
|
@@ -64,7 +69,34 @@ async def ensure_storage_path():
|
|
| 64 |
"""
|
| 65 |
التأكد من وجود مجلد لتخزين ملفات الصوت
|
| 66 |
"""
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
async def text_to_speech(text: str, filename: str) -> str:
|
| 70 |
"""
|
|
@@ -125,6 +157,17 @@ def get_audio_url(filename: str, speed: float = 1.0) -> str:
|
|
| 125 |
"""
|
| 126 |
الحصول على رابط الملف الصوتي مع معلومات السرعة
|
| 127 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
# Always use the full URL with the domain for audio files
|
| 129 |
# This ensures the frontend can access it regardless of where it's hosted
|
| 130 |
base_url = f"{BASE_URL}/audio/{filename}"
|
|
|
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# الحصول على مسار تخزين ملفات الصوت
|
| 16 |
+
# For Hugging Face Spaces, use a directory we know has write permissions
|
| 17 |
+
if os.path.exists("/code"): # Check if we're in Hugging Face Spaces environment
|
| 18 |
+
AUDIO_STORAGE_PATH = os.path.abspath("/tmp/audio_files")
|
| 19 |
+
else:
|
| 20 |
+
AUDIO_STORAGE_PATH = os.path.abspath(os.getenv("AUDIO_STORAGE_PATH", "./audio_files"))
|
| 21 |
+
|
| 22 |
BASE_URL = os.getenv("BASE_URL", "http://localhost:8000")
|
| 23 |
|
| 24 |
# قاموس لتخزين معرفات الملفات الصوتية للقصص
|
|
|
|
| 69 |
"""
|
| 70 |
التأكد من وجود مجلد لتخزين ملفات الصوت
|
| 71 |
"""
|
| 72 |
+
try:
|
| 73 |
+
# Create directory if it doesn't exist
|
| 74 |
+
Path(AUDIO_STORAGE_PATH).mkdir(parents=True, exist_ok=True)
|
| 75 |
+
|
| 76 |
+
# Test write permissions by creating a temporary file
|
| 77 |
+
test_file_path = os.path.join(AUDIO_STORAGE_PATH, f"test_{uuid.uuid4().hex}.tmp")
|
| 78 |
+
with open(test_file_path, 'w') as f:
|
| 79 |
+
f.write("test")
|
| 80 |
+
|
| 81 |
+
# Clean up the test file
|
| 82 |
+
if os.path.exists(test_file_path):
|
| 83 |
+
os.remove(test_file_path)
|
| 84 |
+
|
| 85 |
+
print(f"Successfully verified write permissions to {AUDIO_STORAGE_PATH}")
|
| 86 |
+
return True
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"❌ Error ensuring audio storage path: {str(e)}")
|
| 89 |
+
# Try alternate path as fallback
|
| 90 |
+
alt_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "audio_files")
|
| 91 |
+
try:
|
| 92 |
+
Path(alt_path).mkdir(parents=True, exist_ok=True)
|
| 93 |
+
print(f"⚠️ Using alternate audio path: {alt_path}")
|
| 94 |
+
global AUDIO_STORAGE_PATH
|
| 95 |
+
AUDIO_STORAGE_PATH = alt_path
|
| 96 |
+
return True
|
| 97 |
+
except Exception as alt_e:
|
| 98 |
+
print(f"❌ Fatal error: Cannot create audio directory: {str(alt_e)}")
|
| 99 |
+
raise
|
| 100 |
|
| 101 |
async def text_to_speech(text: str, filename: str) -> str:
|
| 102 |
"""
|
|
|
|
| 157 |
"""
|
| 158 |
الحصول على رابط الملف الصوتي مع معلومات السرعة
|
| 159 |
"""
|
| 160 |
+
# Update BASE_URL if needed for Hugging Face Spaces
|
| 161 |
+
global BASE_URL
|
| 162 |
+
if os.path.exists("/code") and "localhost" in BASE_URL:
|
| 163 |
+
# We're in Hugging Face Spaces but using localhost URL
|
| 164 |
+
# This can happen if the environment variable wasn't set correctly
|
| 165 |
+
# Default to the Spaces URL pattern
|
| 166 |
+
import socket
|
| 167 |
+
hostname = socket.gethostname()
|
| 168 |
+
BASE_URL = f"https://{hostname}.hf.space"
|
| 169 |
+
print(f"Updated BASE_URL to {BASE_URL} for Hugging Face Spaces")
|
| 170 |
+
|
| 171 |
# Always use the full URL with the domain for audio files
|
| 172 |
# This ensures the frontend can access it regardless of where it's hosted
|
| 173 |
base_url = f"{BASE_URL}/audio/{filename}"
|