NitinBot001 commited on
Commit
119600e
·
verified ·
1 Parent(s): eac05f8

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +108 -118
main.py CHANGED
@@ -1,118 +1,108 @@
1
- """
2
- OpenAI TTS API Server
3
-
4
- This module provides a server that's compatible with OpenAI's TTS API format.
5
- This is the main entry point for the application.
6
- """
7
-
8
- import asyncio
9
- import aiohttp
10
- import logging
11
- import ssl
12
- import time
13
- import sys
14
- from typing import Optional
15
- from aiohttp import TCPConnector, ClientTimeout
16
-
17
- from utils.config import load_config, test_connection
18
- from server.api import TTSServer
19
-
20
- # Configure logging
21
- logging.basicConfig(
22
- level=logging.INFO,
23
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
24
- )
25
- logger = logging.getLogger(__name__)
26
-
27
- async def create_test_session(verify_ssl: bool = True) -> Optional[aiohttp.ClientSession]:
28
- """Create a session for testing with optimized settings."""
29
- try:
30
- if not verify_ssl:
31
- connector = TCPConnector(
32
- ssl=False,
33
- limit=5,
34
- ttl_dns_cache=300,
35
- use_dns_cache=True,
36
- enable_cleanup_closed=True
37
- )
38
- else:
39
- connector = TCPConnector(
40
- limit=5,
41
- ttl_dns_cache=300,
42
- use_dns_cache=True,
43
- enable_cleanup_closed=True
44
- )
45
-
46
- timeout = ClientTimeout(
47
- total=30,
48
- connect=10,
49
- sock_read=20
50
- )
51
-
52
- return aiohttp.ClientSession(
53
- connector=connector,
54
- timeout=timeout
55
- )
56
- except Exception as e:
57
- logger.error(f"Failed to create test session: {str(e)}")
58
- return None
59
-
60
- async def main():
61
- """Main function to start the server."""
62
- try:
63
- config = load_config()
64
-
65
- # Test connection mode
66
- if config.get('test_connection', False):
67
- session = await create_test_session(config['verify_ssl'])
68
- if not session:
69
- logger.error("Failed to create test session")
70
- sys.exit(1)
71
-
72
- try:
73
- await test_connection(session)
74
- except Exception as e:
75
- logger.error(f"Connection test failed: {str(e)}")
76
- sys.exit(1)
77
- finally:
78
- await session.close()
79
-
80
- logger.info("Connection test completed successfully")
81
- return
82
-
83
- # Start the server
84
- server = TTSServer(
85
- host=config['host'],
86
- port=config['port'],
87
- verify_ssl=config['verify_ssl'],
88
- max_queue_size=config['max_queue_size']
89
- )
90
-
91
- await server.start()
92
-
93
- try:
94
- # Keep the server running
95
- while True:
96
- await asyncio.sleep(1)
97
- except KeyboardInterrupt:
98
- logger.info("Received shutdown signal")
99
- await server.stop()
100
- logger.info("TTS server stopped gracefully")
101
- except Exception as e:
102
- logger.error(f"Server error: {str(e)}")
103
- await server.stop()
104
- sys.exit(1)
105
-
106
- except Exception as e:
107
- logger.error(f"Fatal error: {str(e)}")
108
- sys.exit(1)
109
-
110
- if __name__ == "__main__":
111
- try:
112
- asyncio.run(main())
113
- except KeyboardInterrupt:
114
- logger.info("Process interrupted by user")
115
- sys.exit(0)
116
- except Exception as e:
117
- logger.error(f"Process terminated due to error: {str(e)}")
118
- sys.exit(1)
 
1
+ """
2
+ OpenAI TTS API Server
3
+
4
+ This module provides a server that's compatible with OpenAI's TTS API format.
5
+ This is the main entry point for the application.
6
+ """
7
+
8
+ import asyncio
9
+ import aiohttp
10
+ import logging
11
+ import ssl
12
+ import time
13
+ import sys
14
+ from typing import Optional
15
+ from aiohttp import TCPConnector, ClientTimeout
16
+
17
+ from utils.config import load_config, test_connection
18
+ from server.api import TTSServer
19
+
20
+ # Configure logging
21
+ logging.basicConfig(
22
+ level=logging.INFO,
23
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
24
+ )
25
+ logger = logging.getLogger(__name__)
26
+
27
+ async def create_test_session(verify_ssl: bool = True) -> Optional[aiohttp.ClientSession]:
28
+ """Create a session for testing with optimized settings."""
29
+ try:
30
+ if not verify_ssl:
31
+ connector = TCPConnector(
32
+ ssl=False,
33
+ limit=5,
34
+ ttl_dns_cache=300,
35
+ use_dns_cache=True,
36
+ enable_cleanup_closed=True
37
+ )
38
+ else:
39
+ connector = TCPConnector(
40
+ limit=5,
41
+ ttl_dns_cache=300,
42
+ use_dns_cache=True,
43
+ enable_cleanup_closed=True
44
+ )
45
+
46
+ timeout = ClientTimeout(
47
+ total=30,
48
+ connect=10,
49
+ sock_read=20
50
+ )
51
+
52
+ return aiohttp.ClientSession(
53
+ connector=connector,
54
+ timeout=timeout
55
+ )
56
+ except Exception as e:
57
+ logger.error(f"Failed to create test session: {str(e)}")
58
+ return None
59
+
60
+ async def main():
61
+ """Main function to start the server."""
62
+ try:
63
+ config = load_config()
64
+
65
+ # Test connection mode
66
+ if config.get('test_connection', False):
67
+ session = await create_test_session(config['verify_ssl'])
68
+ if not session:
69
+ logger.error("Failed to create test session")
70
+ sys.exit(1)
71
+
72
+ try:
73
+ await test_connection(session)
74
+ except Exception as e:
75
+ logger.error(f"Connection test failed: {str(e)}")
76
+ sys.exit(1)
77
+ finally:
78
+ await session.close()
79
+
80
+ logger.info("Connection test completed successfully")
81
+ return
82
+
83
+ # Start the server
84
+ server = TTSServer(
85
+ host=config['host'],
86
+ port=config['port'],
87
+ verify_ssl=config['verify_ssl'],
88
+ max_queue_size=config['max_queue_size']
89
+ )
90
+
91
+ await server.start()
92
+
93
+ try:
94
+ # Keep the server running
95
+ while True:
96
+ await asyncio.sleep(1)
97
+ except KeyboardInterrupt:
98
+ logger.info("Received shutdown signal")
99
+ await server.stop()
100
+ logger.info("TTS server stopped gracefully")
101
+ except Exception as e:
102
+ logger.error(f"Server error: {str(e)}")
103
+ await server.stop()
104
+ sys.exit(1)
105
+
106
+ except Exception as e:
107
+ logger.error(f"Fatal error: {str(e)}")
108
+ sys.exit(1)