Download main.py from Phupingbut1989/Workflow: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/Phupingbut1989/Workflow/resolve/main/main.py
- Command line
-
hf download hf://Phupingbut1989/Workflow/main.py
-
curl -L -o main.py https://huggingface.co/Phupingbut1989/Workflow/resolve/main/main.py
2.93 kB
| --- | |
| license: mit | |
| --- | |
| import os | |
| import sqlite3 | |
| import uuid | |
| from datetime import datetime, timezone | |
| from fastapi import FastAPI, Header, HTTPException | |
| from pydantic import BaseModel, Field | |
| app = FastAPI(title="Brilla AI Workflow") | |
| DB = os.getenv("BRILLA_DB", "workflow.db") | |
| API_KEY = os.getenv("BRILLA_API_KEY") | |
| def auth(authorization=None): | |
| if not API_KEY: | |
| raise HTTPException(503, "API key not configured") | |
| if authorization != f"Bearer {API_KEY}": | |
| raise HTTPException(401, "Unauthorized") | |
| def db(): | |
| conn = sqlite3.connect(DB) | |
| conn.row_factory = sqlite3.Row | |
| conn.execute(""" | |
| CREATE TABLE IF NOT EXISTS workflows ( | |
| id TEXT PRIMARY KEY, | |
| name TEXT, | |
| goal TEXT, | |
| status TEXT, | |
| output TEXT, | |
| created_at TEXT | |
| ) | |
| """) | |
| return conn | |
| class Workflow(BaseModel): | |
| name: str = Field(min_length=1, max_length=100) | |
| goal: str = Field(min_length=1, max_length=500) | |
| def home(): | |
| return {"system": "Brilla AI Workflow", "status": "ready"} | |
| def health(): | |
| return {"status": "ok"} | |
| def create(data: Workflow, authorization: str = Header(None)): | |
| auth(authorization) | |
| wid = str(uuid.uuid4()) | |
| conn = db() | |
| conn.execute( | |
| "INSERT INTO workflows VALUES (?, ?, ?, ?, ?, ?)", | |
| (wid, data.name, data.goal, "pending", None, | |
| datetime.now(timezone.utc).isoformat()) | |
| ) | |
| conn.commit() | |
| conn.close() | |
| return { | |
| "workflow_id": wid, | |
| "status": "pending" | |
| } | |
| def execute(wid: str, authorization: str = Header(None)): | |
| auth(authorization) | |
| conn = db() | |
| row = conn.execute( | |
| "SELECT * FROM workflows WHERE id=?", (wid,) | |
| ).fetchone() | |
| if not row: | |
| conn.close() | |
| raise HTTPException(404, "Workflow not found") | |
| # ตัวอย่าง Executor | |
| result = { | |
| "goal": row["goal"], | |
| "message": "Workflow executed" | |
| } | |
| conn.execute( | |
| "UPDATE workflows SET status=?, output=? WHERE id=?", | |
| ("completed", str(result), wid) | |
| ) | |
| conn.commit() | |
| conn.close() | |
| return { | |
| "workflow_id": wid, | |
| "status": "completed", | |
| "output": result | |
| } | |
| def status(wid: str, authorization: str = Header(None)): | |
| auth(authorization) | |
| conn = db() | |
| row = conn.execute( | |
| "SELECT * FROM workflows WHERE id=?", (wid,) | |
| ).fetchone() | |
| conn.close() | |
| if not row: | |
| raise HTTPException(404, "Not found") | |
| return dict(row) | |
| def history(authorization: str = Header(None)): | |
| auth(authorization) | |
| conn = db() | |
| rows = conn.execute( | |
| "SELECT * FROM workflows ORDER BY created_at DESC" | |
| ).fetchall() | |
| conn.close() | |
| return {"items": [dict(r) for r in rows]} |