-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
132 lines (104 loc) Β· 4.17 KB
/
Copy pathrun.py
File metadata and controls
132 lines (104 loc) Β· 4.17 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
#!/usr/bin/env python3
"""
GenAI Testing Tutorial - RAG Chatbot Application
Entry point for running the Flask application
"""
import os
import sys
import subprocess
# Add the app directory to the Python path
sys.path.insert(0, os.path.dirname(__file__))
def format_pip_install_cmd() -> str:
"""Return a pip install command that targets the active interpreter."""
return f'"{sys.executable}" -m pip install -r requirements.txt'
def get_project_venv_python() -> str | None:
"""Return project venv Python path when present, otherwise None."""
base_dir = os.path.dirname(__file__)
parent_dir = os.path.dirname(base_dir)
if os.name == "nt":
candidates = [
os.path.join(base_dir, "training-env", "Scripts", "python.exe"),
os.path.join(parent_dir, "training-env", "Scripts", "python.exe"),
]
else:
candidates = [
os.path.join(base_dir, "training-env", "bin", "python"),
os.path.join(parent_dir, "training-env", "bin", "python"),
]
for candidate in candidates:
if os.path.exists(candidate):
return candidate
return None
def maybe_relaunch_with_project_venv():
"""Relaunch this script with project venv Python when available."""
# Avoid relaunch loops.
if os.getenv("TESTING_AI_SKIP_VENV_REDIRECT") == "1":
return
venv_python = get_project_venv_python()
if not venv_python:
return
current_python = os.path.abspath(sys.executable)
if os.path.abspath(venv_python) == current_python:
return
print("π Switching to project virtual environment interpreter...")
env = os.environ.copy()
env["TESTING_AI_SKIP_VENV_REDIRECT"] = "1"
cmd = [venv_python, os.path.abspath(__file__), *sys.argv[1:]]
rc = subprocess.call(cmd, env=env)
sys.exit(rc)
if __name__ == '__main__':
maybe_relaunch_with_project_venv()
try:
from dotenv import load_dotenv
except ModuleNotFoundError as e:
missing = getattr(e, "name", "unknown module")
print("β Missing required Python dependency:")
print(f" - {missing}")
print("\nInstall project dependencies with your current interpreter:")
print(f" {format_pip_install_cmd()}")
venv_python = get_project_venv_python()
if venv_python:
print("\nOr run using the project virtual environment interpreter:")
print(f" \"{venv_python}\" -m pip install -r requirements.txt")
print(f" \"{venv_python}\" run.py")
print("\nThen run this command again.")
sys.exit(1)
# Load environment variables
load_dotenv()
# Import and run the Flask app
try:
from app.main import app
except ModuleNotFoundError as e:
missing = getattr(e, "name", "unknown module")
print("β Missing required Python dependency:")
print(f" - {missing}")
print("\nInstall project dependencies with your current interpreter:")
print(f" {format_pip_install_cmd()}")
venv_python = get_project_venv_python()
if venv_python:
print("\nOr run using the project virtual environment interpreter:")
print(f" \"{venv_python}\" -m pip install -r requirements.txt")
print(f" \"{venv_python}\" run.py")
print("\nThen run this command again.")
sys.exit(1)
if __name__ == '__main__':
# Validate required environment variables
required_vars = ['COHERE_API_KEY']
missing_vars = [var for var in required_vars if not os.getenv(var)]
if missing_vars:
print("β Missing required environment variables:")
for var in missing_vars:
print(f" - {var}")
print("\nPlease add your LLM API Key to the .env file.")
sys.exit(1)
print("π Starting GenAI Testing Tutorial Application...")
print(f"π Documents directory: {os.path.join(os.path.dirname(__file__), 'data', 'documents')}")
print(f"π§ Environment: {os.getenv('FLASK_ENV', 'development')}")
print(f"π Server will start on: http://localhost:{os.getenv('FLASK_PORT', 5000)}")
print("\n" + "="*50)
# Start the Flask application
app.run(
host='0.0.0.0',
port=int(os.getenv('FLASK_PORT', 5000)),
debug=os.getenv('FLASK_DEBUG', 'True').lower() == 'true'
)