-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_web_ui.py
More file actions
158 lines (131 loc) · 5.32 KB
/
Copy pathstart_web_ui.py
File metadata and controls
158 lines (131 loc) · 5.32 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
#!/usr/bin/env python3
"""
Startup script for the Dynamic AI Framework Web UI
"""
import sys
import subprocess
import webbrowser
import time
from pathlib import Path
from dynamic_ai_framework.config import get_server_config
from dynamic_ai_framework.core.logging_config import get_logger, setup_logging
def check_dependencies():
"""Check if required dependencies are installed."""
logger = get_logger("startup")
required_packages = [
'flask',
'flask-cors',
'langchain',
'langchain-community',
'langchain-ollama'
]
missing_packages = []
for package in required_packages:
try:
__import__(package.replace('-', '_'))
except ImportError:
missing_packages.append(package)
if missing_packages:
print("Missing required packages:")
for package in missing_packages:
print(f" - {package}")
print("\nInstall missing packages with:")
print(f"pip install {' '.join(missing_packages)}")
return False
return True
def check_ollama():
"""Check if Ollama is running."""
logger = get_logger("startup")
try:
import subprocess
from dynamic_ai_framework.config import get_external_services_config
timeout = get_external_services_config().ollama_timeout_seconds
result = subprocess.run(['ollama', 'list'], capture_output=True, text=True, timeout=timeout)
if result.returncode == 0:
lines = result.stdout.strip().split('\n')[1:] # Skip header
models = []
for line in lines:
if line.strip():
model_name = line.split()[0] # First column is model name
models.append(model_name)
if models:
logger.info(f"✓ Ollama is running with {len(models)} models available:")
for model in models[:5]: # Show first 5 models
logger.info(f" - {model}")
if len(models) > 5:
logger.info(f" ... and {len(models) - 5} more")
else:
logger.info("✓ Ollama is running but no models are installed")
logger.info(" You can pull models with: ollama pull <model-name>")
return True
else:
logger.warning("✗ Ollama is not responding")
logger.info(" Start Ollama with: ollama serve")
return False
except FileNotFoundError:
logger.error("✗ Ollama command not found")
logger.info(" Please install Ollama from https://ollama.ai")
return False
except Exception as e:
logger.error(f"✗ Error checking Ollama: {e}")
logger.info(" Make sure Ollama is installed and running")
return False
def start_web_ui():
"""Start the web UI."""
print("Starting Dynamic AI Framework Web UI...")
# Import and run the Flask app
try:
from web_app import app
server_config = get_server_config()
print("✓ Web UI loaded successfully")
print(f"\nStarting server at http://{server_config.host}:{server_config.port}")
print("Press Ctrl+C to stop the server")
# Open browser after a short delay
def open_browser():
server_config = get_server_config()
if server_config.auto_open_browser:
time.sleep(server_config.browser_delay_seconds)
webbrowser.open(f'http://localhost:{server_config.port}')
import threading
threading.Thread(target=open_browser).start()
# Start Flask app with configurable settings
server_config = get_server_config()
app.run(debug=server_config.debug, host=server_config.host, port=server_config.port)
except Exception as e:
logger.error(f"✗ Failed to start web UI: {e}")
return False
def main():
"""Main startup function."""
# Set up logging first
setup_logging(level="INFO", include_timestamp=False)
logger = get_logger("startup")
logger.info("=" * 50)
logger.info("Dynamic AI Framework - Web UI Startup")
logger.info("=" * 50)
# Check current directory
if not Path("dynamic_ai_framework").exists():
logger.error("✗ Error: dynamic_ai_framework directory not found")
logger.error(" Please run this script from the project root directory")
sys.exit(1)
logger.info("✓ Project directory found")
# Check dependencies
logger.info("\nChecking dependencies...")
if not check_dependencies():
sys.exit(1)
logger.info("✓ All dependencies available")
# Check Ollama
logger.info("\nChecking Ollama...")
ollama_ok = check_ollama()
if not ollama_ok:
response = input("\nOllama is not ready. Continue anyway? (y/N): ")
if response.lower() != 'y':
logger.info("Please set up Ollama and try again.")
logger.info("You can install Ollama from: https://ollama.ai")
sys.exit(1)
logger.warning("Continuing without Ollama verification...")
logger.info("Note: You'll need to set up Ollama and models before using the web UI")
# Start web UI
logger.info("\n" + "=" * 50)
start_web_ui()
if __name__ == "__main__":
main()