Spaces:
Running on Zero
Running on Zero
| 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. | |
| 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() |