Spaces:
Paused
Paused
Update utils/config.py
Browse files- utils/config.py +100 -100
utils/config.py
CHANGED
|
@@ -1,101 +1,101 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Configuration Utilities
|
| 3 |
-
|
| 4 |
-
This module provides utilities for loading and managing configuration settings.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import os
|
| 8 |
-
import argparse
|
| 9 |
-
import logging
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
-
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
-
|
| 14 |
-
def load_config():
|
| 15 |
-
"""Load configuration from environment variables and command line arguments.
|
| 16 |
-
|
| 17 |
-
Returns:
|
| 18 |
-
argparse.Namespace: The configuration settings
|
| 19 |
-
"""
|
| 20 |
-
# Load environment variables
|
| 21 |
-
load_dotenv()
|
| 22 |
-
|
| 23 |
-
# Get default values from environment variables
|
| 24 |
-
default_host = os.getenv("HOST", "localhost")
|
| 25 |
-
default_port = int(os.getenv("PORT", "
|
| 26 |
-
default_verify_ssl = os.getenv("VERIFY_SSL", "true").lower() != "false"
|
| 27 |
-
default_max_queue_size = int(os.getenv("MAX_QUEUE_SIZE", "100"))
|
| 28 |
-
default_rate_limit_requests = int(os.getenv("RATE_LIMIT_REQUESTS", "30"))
|
| 29 |
-
default_rate_limit_window = int(os.getenv("RATE_LIMIT_WINDOW", "60"))
|
| 30 |
-
|
| 31 |
-
parser = argparse.ArgumentParser(description="Run the TTS API server")
|
| 32 |
-
parser.add_argument("--host", type=str, default=default_host, help="Host to bind to")
|
| 33 |
-
parser.add_argument("--port", type=int, default=default_port, help="Port to bind to")
|
| 34 |
-
parser.add_argument("--no-verify-ssl", action="store_true", help="Disable SSL certificate verification (insecure, use only for testing)")
|
| 35 |
-
parser.add_argument("--max-queue-size", type=int, default=default_max_queue_size, help="Maximum number of tasks in queue")
|
| 36 |
-
parser.add_argument("--test-connection", action="store_true", help="Test connection to OpenAI.fm and exit")
|
| 37 |
-
|
| 38 |
-
args = parser.parse_args()
|
| 39 |
-
|
| 40 |
-
# Apply global SSL settings if needed
|
| 41 |
-
if args.no_verify_ssl or not default_verify_ssl:
|
| 42 |
-
import ssl
|
| 43 |
-
# Disable SSL verification globally in Python
|
| 44 |
-
ssl._create_default_https_context = ssl._create_unverified_context
|
| 45 |
-
logger.warning("SSL certificate verification disabled GLOBALLY. This is insecure!")
|
| 46 |
-
|
| 47 |
-
return {
|
| 48 |
-
'host': args.host,
|
| 49 |
-
'port': args.port,
|
| 50 |
-
'verify_ssl': not args.no_verify_ssl,
|
| 51 |
-
'max_queue_size': args.max_queue_size,
|
| 52 |
-
'rate_limit_requests': default_rate_limit_requests,
|
| 53 |
-
'rate_limit_window': default_rate_limit_window,
|
| 54 |
-
'test_connection': args.test_connection
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
async def test_connection(session):
|
| 58 |
-
"""Test connection to OpenAI.fm.
|
| 59 |
-
|
| 60 |
-
Args:
|
| 61 |
-
session: aiohttp.ClientSession to use for requests
|
| 62 |
-
"""
|
| 63 |
-
logger.info("Testing connection to OpenAI.fm...")
|
| 64 |
-
|
| 65 |
-
try:
|
| 66 |
-
logger.info("Sending GET request to OpenAI.fm homepage")
|
| 67 |
-
async with session.get("https://www.openai.fm") as response:
|
| 68 |
-
logger.info(f"Homepage status: {response.status}")
|
| 69 |
-
if response.status == 200:
|
| 70 |
-
logger.info("Successfully connected to OpenAI.fm homepage")
|
| 71 |
-
else:
|
| 72 |
-
logger.error(f"Failed to connect to OpenAI.fm homepage: {response.status}")
|
| 73 |
-
|
| 74 |
-
logger.info("Testing API endpoint with minimal request")
|
| 75 |
-
test_data = {"input": "Test", "voice": "alloy"}
|
| 76 |
-
import urllib.parse
|
| 77 |
-
url_encoded_data = urllib.parse.urlencode(test_data)
|
| 78 |
-
|
| 79 |
-
async with session.post(
|
| 80 |
-
"https://www.openai.fm/api/generate",
|
| 81 |
-
data=url_encoded_data,
|
| 82 |
-
headers={
|
| 83 |
-
"Accept": "*/*",
|
| 84 |
-
"Accept-Language": "en-US,en;q=0.9",
|
| 85 |
-
"Origin": "https://www.openai.fm",
|
| 86 |
-
"Referer": "https://www.openai.fm/",
|
| 87 |
-
"Content-Type": "application/x-www-form-urlencoded"
|
| 88 |
-
}
|
| 89 |
-
) as response:
|
| 90 |
-
logger.info(f"API endpoint status: {response.status}")
|
| 91 |
-
if response.status == 200:
|
| 92 |
-
data = await response.read()
|
| 93 |
-
logger.info(f"Successfully received {len(data)} bytes from API")
|
| 94 |
-
else:
|
| 95 |
-
text = await response.text()
|
| 96 |
-
logger.error(f"API request failed: {response.status}, {text}")
|
| 97 |
-
|
| 98 |
-
except Exception as e:
|
| 99 |
-
logger.error(f"Connection test failed with error: {str(e)}")
|
| 100 |
-
import traceback
|
| 101 |
logger.error(traceback.format_exc())
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Configuration Utilities
|
| 3 |
+
|
| 4 |
+
This module provides utilities for loading and managing configuration settings.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import argparse
|
| 9 |
+
import logging
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
def load_config():
|
| 15 |
+
"""Load configuration from environment variables and command line arguments.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
argparse.Namespace: The configuration settings
|
| 19 |
+
"""
|
| 20 |
+
# Load environment variables
|
| 21 |
+
load_dotenv()
|
| 22 |
+
|
| 23 |
+
# Get default values from environment variables
|
| 24 |
+
default_host = os.getenv("HOST", "localhost")
|
| 25 |
+
default_port = int(os.getenv("PORT", "7860"))
|
| 26 |
+
default_verify_ssl = os.getenv("VERIFY_SSL", "true").lower() != "false"
|
| 27 |
+
default_max_queue_size = int(os.getenv("MAX_QUEUE_SIZE", "100"))
|
| 28 |
+
default_rate_limit_requests = int(os.getenv("RATE_LIMIT_REQUESTS", "30"))
|
| 29 |
+
default_rate_limit_window = int(os.getenv("RATE_LIMIT_WINDOW", "60"))
|
| 30 |
+
|
| 31 |
+
parser = argparse.ArgumentParser(description="Run the TTS API server")
|
| 32 |
+
parser.add_argument("--host", type=str, default=default_host, help="Host to bind to")
|
| 33 |
+
parser.add_argument("--port", type=int, default=default_port, help="Port to bind to")
|
| 34 |
+
parser.add_argument("--no-verify-ssl", action="store_true", help="Disable SSL certificate verification (insecure, use only for testing)")
|
| 35 |
+
parser.add_argument("--max-queue-size", type=int, default=default_max_queue_size, help="Maximum number of tasks in queue")
|
| 36 |
+
parser.add_argument("--test-connection", action="store_true", help="Test connection to OpenAI.fm and exit")
|
| 37 |
+
|
| 38 |
+
args = parser.parse_args()
|
| 39 |
+
|
| 40 |
+
# Apply global SSL settings if needed
|
| 41 |
+
if args.no_verify_ssl or not default_verify_ssl:
|
| 42 |
+
import ssl
|
| 43 |
+
# Disable SSL verification globally in Python
|
| 44 |
+
ssl._create_default_https_context = ssl._create_unverified_context
|
| 45 |
+
logger.warning("SSL certificate verification disabled GLOBALLY. This is insecure!")
|
| 46 |
+
|
| 47 |
+
return {
|
| 48 |
+
'host': args.host,
|
| 49 |
+
'port': args.port,
|
| 50 |
+
'verify_ssl': not args.no_verify_ssl,
|
| 51 |
+
'max_queue_size': args.max_queue_size,
|
| 52 |
+
'rate_limit_requests': default_rate_limit_requests,
|
| 53 |
+
'rate_limit_window': default_rate_limit_window,
|
| 54 |
+
'test_connection': args.test_connection
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
async def test_connection(session):
|
| 58 |
+
"""Test connection to OpenAI.fm.
|
| 59 |
+
|
| 60 |
+
Args:
|
| 61 |
+
session: aiohttp.ClientSession to use for requests
|
| 62 |
+
"""
|
| 63 |
+
logger.info("Testing connection to OpenAI.fm...")
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
logger.info("Sending GET request to OpenAI.fm homepage")
|
| 67 |
+
async with session.get("https://www.openai.fm") as response:
|
| 68 |
+
logger.info(f"Homepage status: {response.status}")
|
| 69 |
+
if response.status == 200:
|
| 70 |
+
logger.info("Successfully connected to OpenAI.fm homepage")
|
| 71 |
+
else:
|
| 72 |
+
logger.error(f"Failed to connect to OpenAI.fm homepage: {response.status}")
|
| 73 |
+
|
| 74 |
+
logger.info("Testing API endpoint with minimal request")
|
| 75 |
+
test_data = {"input": "Test", "voice": "alloy"}
|
| 76 |
+
import urllib.parse
|
| 77 |
+
url_encoded_data = urllib.parse.urlencode(test_data)
|
| 78 |
+
|
| 79 |
+
async with session.post(
|
| 80 |
+
"https://www.openai.fm/api/generate",
|
| 81 |
+
data=url_encoded_data,
|
| 82 |
+
headers={
|
| 83 |
+
"Accept": "*/*",
|
| 84 |
+
"Accept-Language": "en-US,en;q=0.9",
|
| 85 |
+
"Origin": "https://www.openai.fm",
|
| 86 |
+
"Referer": "https://www.openai.fm/",
|
| 87 |
+
"Content-Type": "application/x-www-form-urlencoded"
|
| 88 |
+
}
|
| 89 |
+
) as response:
|
| 90 |
+
logger.info(f"API endpoint status: {response.status}")
|
| 91 |
+
if response.status == 200:
|
| 92 |
+
data = await response.read()
|
| 93 |
+
logger.info(f"Successfully received {len(data)} bytes from API")
|
| 94 |
+
else:
|
| 95 |
+
text = await response.text()
|
| 96 |
+
logger.error(f"API request failed: {response.status}, {text}")
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
logger.error(f"Connection test failed with error: {str(e)}")
|
| 100 |
+
import traceback
|
| 101 |
logger.error(traceback.format_exc())
|