File size: 3,670 Bytes
e3456a9
0da1272
e3456a9
0da1272
e3456a9
 
c152fec
0da1272
 
e3456a9
c152fec
0da1272
e3456a9
 
0831fe7
e3456a9
c152fec
e3456a9
 
0da1272
a08db6a
0da1272
 
 
 
 
b8e4ebb
3f52ffc
0da1272
 
 
3f52ffc
fa36132
0da1272
 
 
fa36132
0da1272
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa36132
0da1272
e3456a9
 
 
 
 
bde160e
0da1272
 
 
 
 
e3456a9
0da1272
0831fe7
 
 
 
 
 
e3456a9
c152fec
df604d9
0da1272
df604d9
 
0da1272
e3456a9
0da1272
df604d9
0831fe7
0da1272
0831fe7
0da1272
e3456a9
0da1272
e3456a9
 
0da1272
 
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
"""
Flare – LLM startup notifier
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Projeler açılırken LLM provider'a startup çağrısı yapar.
"""

from __future__ import annotations
import threading, requests, os
from typing import Dict, Any
from utils import log
from config_provider import ConfigProvider, ProjectConfig, VersionConfig
from llm_factory import LLMFactory

def _select_live_version(p: ProjectConfig) -> VersionConfig | None:
    """Yayınlanmış en güncel versiyonu getir."""
    published = [v for v in p.versions if v.published]
    return max(published, key=lambda v: v.id) if published else None

def notify_startup():
    """Notify LLM provider about project startups"""
    cfg = ConfigProvider.get()
    
    # Check if current provider needs startup notification
    llm_config = cfg.global_config.llm_provider
    if not llm_config:
        log("⚠️ No LLM provider configured")
        return
    
    provider_def = cfg.global_config.get_provider_config("llm", llm_config.name)
    if not provider_def:
        log(f"⚠️ Unknown LLM provider: {llm_config.name}")
        return
    
    # Currently only Spark variants need startup
    if llm_config.name not in ("spark", "spark_cloud", "spark_onpremise"):
        log(f"ℹ️ {llm_config.name} doesn't require startup notification")
        return
    
    # Check if provider requires repo info
    if not provider_def.requires_repo_info:
        log(f"ℹ️ {llm_config.name} doesn't require repo info for startup")
        return
    
    # Get endpoint and API key
    endpoint = llm_config.endpoint
    if not endpoint:
        log("⚠️ No LLM endpoint configured for startup")
        return
    
    api_key = cfg.global_config.get_plain_api_key("llm")
    if not api_key:
        # Try environment
        env_var = "SPARK_TOKEN"
        if os.environ.get("SPACE_ID"):
            api_key = os.environ.get(env_var)
        else:
            from dotenv import load_dotenv
            load_dotenv()
            api_key = os.getenv(env_var)
        
        if not api_key:
            log("⚠️ No API key available for startup")
            return
    
    spark_base = str(endpoint).rstrip("/")

    for p in cfg.projects:
        if not p.enabled:
            continue
        v = _select_live_version(p)
        if not v:
            continue
        
        # Check if version has LLM config (required for Spark)
        if not v.llm:
            log(f"⚠️ No LLM config in version {v.id} for project '{p.name}'")
            continue

        body: Dict[str, Any] = {
            # Required fields for Spark startup
            "project_name": p.name,
            "project_version": v.id,
            "repo_id": v.llm.repo_id,
            "generation_config": v.llm.generation_config,
            "use_fine_tune": v.llm.use_fine_tune,
            "fine_tune_zip": v.llm.fine_tune_zip,
        }

        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }

        try:
            log(f"🚀 Notifying {llm_config.name} /startup for project '{p.name}' …")
            r = requests.post(f"{spark_base}/startup", json=body, headers=headers, timeout=10)
            if r.status_code >= 400:
                log(f"❌ LLM provider responded {r.status_code}: {r.text}")
                r.raise_for_status()
            log(f"✅ LLM provider acknowledged startup ({r.status_code})")
        except Exception as e:
            log(f"⚠️ LLM startup failed: {e}")

def run_in_thread():
    """Run startup notification in background thread"""
    threading.Thread(target=notify_startup, daemon=True).start()