-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
112 lines (90 loc) · 3.46 KB
/
Copy pathpipeline.py
File metadata and controls
112 lines (90 loc) · 3.46 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
import subprocess
import time
import os
import sys
import requests
# === CONFIGURATION ===
# Node → Python pipeline steps
STEPS = [
{"cmd": "node fetchMultipleCalendarsByDate.js", "desc": "Fetch Calendar Events"},
{"cmd": "node processMeetings.js", "desc": "Process & Enrich Meetings (BigQuery)"},
{"cmd": "node transposeMeetingData.js", "desc": "Transpose Data Format"},
{"cmd": "python3 upload_to_sheets.py", "desc": "Upload final_categorized.csv to Google Sheets"}
]
OLLAMA_API_URL = "http://localhost:11500"
CLASSIFICATION_SCRIPT = "aiClassification.py"
def run_command(command, description):
"""Executes a shell command and halts pipeline on failure."""
print(f"\n🚀 [STEP] {description}...")
try:
subprocess.run(command, check=True, shell=True)
print(f"✅ {description} completed.")
except subprocess.CalledProcessError as e:
print(f"❌ {description} failed. (Exit Code: {e.returncode})")
sys.exit(1)
def is_ollama_ready(url):
"""Checks if Ollama is responsive at the given URL."""
try:
requests.get(url, timeout=2)
return True
except requests.RequestException:
return False
def manage_ollama():
"""Starts Ollama if not running, specifically on port 11500."""
print(f"\n🦙 [SETUP] Checking Ollama status on {OLLAMA_API_URL}...")
# 1. Check if already running
if is_ollama_ready(OLLAMA_API_URL):
print(" 🔹 Ollama is already running.")
return None # No process to kill later
# 2. Configure environment for custom port
ollama_env = os.environ.copy()
ollama_env["OLLAMA_HOST"] = "127.0.0.1:11500"
print(" 🔸 Starting Ollama server...")
try:
process = subprocess.Popen(
["ollama", "serve"],
env=ollama_env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
except FileNotFoundError:
print("❌ Error: 'ollama' command not found. Is it installed?")
sys.exit(1)
# 3. Wait for initialization
retries = 10
while retries > 0:
if is_ollama_ready(OLLAMA_API_URL):
print("✅ Ollama started successfully.")
return process
time.sleep(2)
retries -= 1
print(f" ⏳ Waiting for Ollama... ({10 - retries}/10)")
print("❌ Failed to start Ollama. Check installation.")
process.kill()
sys.exit(1)
def main():
print("==========================================")
print(" 🗓️ CALENDAR DATA PIPELINE START ")
print("==========================================")
# 1. Run Node.js Data Processing Steps
for step in STEPS[:-1]: # Run all steps except final_sheet_upload (done later)
run_command(step["cmd"], step["desc"])
# 2. Start Ollama
ollama_process = manage_ollama()
# 3. AI Classification
try:
run_command(f"python3 {CLASSIFICATION_SCRIPT}", "AI Classification with Llama3")
finally:
# 4. Cleanup Ollama if we started it
if ollama_process:
print("\n🛑 [CLEANUP] Stopping Ollama server...")
ollama_process.terminate()
ollama_process.wait()
# 5. Upload CSV → Google Sheets
final_step = STEPS[-1]
run_command(final_step["cmd"], final_step["desc"])
print("\n🎉 ========================================")
print(" PIPELINE COMPLETED SUCCESSFULLY ")
print("===========================================")
if __name__ == "__main__":
main()