-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·196 lines (164 loc) · 5.99 KB
/
Copy pathmain.py
File metadata and controls
executable file
·196 lines (164 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
"""
Main entry point for the Gradio Bailian Text-to-Video Application.
This script provides a command-line interface to launch the web application
with various configuration options.
"""
import argparse
import logging
import sys
import os
from pathlib import Path
import socket
# Add src directory to Python path
src_path = Path(__file__).parent / "src"
sys.path.insert(0, str(src_path))
try:
from src.gradio_app import create_app, EnhancedGradioVideoApp
from src.config import Config
from src.video_service_factory import MultiModalVideoApp
except ImportError as e:
print(f"❌ Import error: {e}")
print("Please ensure all dependencies are installed by running:")
print("pip install -r requirements.txt")
sys.exit(1)
def setup_logging(debug: bool = False):
"""Setup logging configuration."""
level = logging.DEBUG if debug else logging.INFO
logging.basicConfig(
level=level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
def find_available_port(start_port: int, end_port: int = 65535) -> int:
"""
Find the first available port in a given range.
Args:
start_port: The port number to start searching from.
end_port: The upper bound of the port range to search.
Returns:
The first available port number, or raises an IOError if no port is available.
"""
for port in range(start_port, end_port + 1):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
if s.connect_ex(('127.0.0.1', port)) != 0:
return port
raise IOError(f"No available ports in range {start_port}-{end_port}")
def check_environment():
"""Check if the environment is properly configured."""
try:
# Validate configuration
Config.validate_config()
# Initialize multi-modal app
app = MultiModalVideoApp(api_key=Config.DASHSCOPE_API_KEY)
status = app.get_service_status()
print("✅ Environment Check Passed")
print(f" API Configured: {status['api_configured']}")
print(f" Supported Modes: {', '.join(status['supported_modes'])}")
print(f" Current Mode: {status['current_mode'] or 'None'}")
print(" Available Models:")
for mode, info in status['modes_info'].items():
print(f" {mode}: {', '.join(info['available_models'])}")
print()
return True
except Exception as e:
print(f"❌ Environment check failed: {e}")
print("\nPlease ensure:")
print("1. DASHSCOPE_API_KEY is set in your .env file")
print("2. All dependencies are installed (pip install -r requirements.txt)")
print("3. You have internet connectivity")
return False
def main():
"""Main function to run the application."""
parser = argparse.ArgumentParser(
description="Gradio Bailian Text-to-Video Generator",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py # Launch with default settings
python main.py --port 8080 # Launch on port 8080
python main.py --share # Create public link
python main.py --debug # Enable debug mode
python main.py --check-env # Check environment only
"""
)
parser.add_argument(
"--host",
default="127.0.0.1",
help="Host to bind the server to (default: 127.0.0.1)"
)
parser.add_argument(
"--port",
type=int,
default=7860,
help="Port to run the server on (default: 7860)"
)
parser.add_argument(
"--share",
action="store_true",
help="Create a public link for the app"
)
parser.add_argument(
"--debug",
action="store_true",
help="Enable debug mode with verbose logging"
)
parser.add_argument(
"--check-env",
action="store_true",
help="Check environment configuration and exit"
)
args = parser.parse_args()
# Setup logging
setup_logging(args.debug)
logger = logging.getLogger(__name__)
print("🎬 Enhanced Multi-Modal Video Generator")
print("=" * 50)
# Check environment
if not check_environment():
sys.exit(1)
# If only checking environment, exit here
if args.check_env:
print("Environment check completed successfully!")
sys.exit(0)
try:
# Find an available port
try:
available_port = find_available_port(args.port)
if available_port != args.port:
print(f"⚠️ Port {args.port} is in use. Using port {available_port} instead.")
except IOError as e:
logger.error(f"Port search failed: {e}")
print(f"❌ {e}")
sys.exit(1)
# Create and launch the application
logger.info("Initializing Gradio application...")
app = create_app()
print(f"🚀 Starting server on {args.host}:{available_port}")
if args.share:
print("🌐 Creating public link...")
if args.debug:
print("🔍 Debug mode enabled")
print("\n" + "=" * 50)
print("Application is starting...")
print("Press Ctrl+C to stop the server")
print("=" * 50)
# Launch the application
app.launch(
server_name=args.host,
server_port=available_port,
share=args.share,
debug=args.debug
)
except KeyboardInterrupt:
print("\n\n👋 Application stopped by user")
sys.exit(0)
except Exception as e:
logger.error(f"Failed to start application: {e}")
print(f"\n❌ Failed to start application: {e}")
if args.debug:
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()