File size: 2,513 Bytes
cf79dfe
31f2a28
8b9e927
 
 
 
4b5d661
cf79dfe
 
 
 
 
 
 
 
8b9e927
 
 
 
 
 
 
 
 
 
31f2a28
8b9e927
 
 
 
31f2a28
d75ec6b
 
 
 
 
 
 
 
 
 
8b9e927
 
 
 
4b5d661
 
 
 
 
 
8b9e927
 
 
4b5d661
d75ec6b
4bbd0fb
 
 
 
 
 
 
cf79dfe
4bbd0fb
 
 
 
 
 
8b9e927
 
 
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
import spaces  # Necesar pentru runtime-ul ZeroGPU
import gradio as gr
import json
from evaluator import evaluate_trajectories_batch
from report_generator import generate_ai_report
from database import verify_api_key_db, save_evaluation_to_db
import threading
# --- DUMMY FUNCTION PENTRU VALIDAREA ZERO-GPU LA PORNIRE ---
# Aceasta functie exista DOAR pentru a trece de verificarea HF la startup.
# Nu este apelata de API si NU consuma cota utilizatorilor.
@spaces.GPU
def _dummy_startup_check():
    pass

# --- ENDPOINT-UL PRINCIPAL (RULEAZA 100% PE CPU, FARA COTA GPU) ---
def evaluate_trajectory_api(api_key: str, payload_json: str) -> str:
    auth_ctx = verify_api_key_db(api_key)
    if not auth_ctx:
        return json.dumps({"error": "Access Denied. Invalid API Key.", "status_code": 403})
    
    if auth_ctx.get("quota_exceeded"):
        return json.dumps({
            "error": f"Monthly Quota Exceeded ({auth_ctx['usage']}/{auth_ctx['limit']}). Upgrade to Pro.",
            "status_code": 429
        })

    try:
        raw_payload = json.loads(payload_json)
    except Exception as e:
        return json.dumps({"error": f"Malformed JSON: {e}", "status_code": 400})

    # AFLAM PLANUL UTILIZATORULUI DIN SUPABASE (ex: "free" sau "pro")
    user_plan = auth_ctx.get("plan", "free")

    # Trimitem planul catre functia de evaluare
    report = evaluate_trajectories_batch(raw_payload, "standard", False, plan=user_plan)

    # Daca a depasit limita de pasi pe free tier, returnam eroarea
    if "error" in report:
        return json.dumps(report)

    ai_markdown = generate_ai_report(report)
    report["narrative_report"] = ai_markdown

    if auth_ctx.get("project_id"):
        t = threading.Thread(
            target=save_evaluation_to_db, 
            args=(auth_ctx["project_id"], report),
            daemon=True
        )
        t.start()

    return json.dumps(report)



with gr.Blocks(title="Limina AI Engine") as demo:
    gr.Markdown("# Limina AI — Cognitive Trajectory Engine API")
    api_key_input = gr.Textbox(label="API Key", type="password")
    payload_input = gr.Textbox(label="Trajectory JSON", lines=6)
    output_json = gr.Textbox(label="Diagnostic Report JSON", lines=10)
    submit_btn = gr.Button("Evaluate")
    
    # Apelam doar functia de CPU
    submit_btn.click(
        fn=evaluate_trajectory_api,
        inputs=[api_key_input, payload_input],
        outputs=output_json,
        api_name="evaluate"
    )

if __name__ == "__main__":
    demo.launch()