Spaces:
Runtime error
Runtime error
Altamira Builder commited on
Commit ·
eea79ff
1
Parent(s): 67dc353
minimal FastAPI test (no Gradio)
Browse files- app.py +6 -104
- requirements.txt +0 -4
app.py
CHANGED
|
@@ -1,110 +1,12 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
|
| 5 |
-
import gradio as gr
|
| 6 |
-
from cryptography.fernet import Fernet
|
| 7 |
from fastapi import FastAPI
|
| 8 |
from fastapi.responses import JSONResponse
|
| 9 |
-
from huggingface_hub import HfApi
|
| 10 |
-
|
| 11 |
-
API = HfApi()
|
| 12 |
-
STATE_DATASET = "altamira-state-store"
|
| 13 |
-
DEFAULT_SANDBOX = "altamira-default-sandbox"
|
| 14 |
-
STATE_DIR = Path("/tmp/altamira-state")
|
| 15 |
-
STATE_DIR.mkdir(parents=True, exist_ok=True)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def _get_cipher():
|
| 19 |
-
key = os.environ.get("ALTAMIRA_ENCRYPTION_KEY")
|
| 20 |
-
if key is None:
|
| 21 |
-
key = Fernet.generate_key()
|
| 22 |
-
os.environ["ALTAMIRA_ENCRYPTION_KEY"] = key.decode()
|
| 23 |
-
return Fernet(key.encode() if isinstance(key, str) else key)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def atomic_write(data: dict, name: str = "config"):
|
| 27 |
-
tmp = STATE_DIR / f"{name}.tmp"
|
| 28 |
-
final = STATE_DIR / f"{name}.enc"
|
| 29 |
-
cipher = _get_cipher()
|
| 30 |
-
payload = json.dumps(data).encode()
|
| 31 |
-
encrypted = cipher.encrypt(payload)
|
| 32 |
-
tmp.write_bytes(encrypted)
|
| 33 |
-
tmp.rename(final)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
def atomic_read(name: str = "config") -> dict | None:
|
| 37 |
-
final = STATE_DIR / f"{name}.enc"
|
| 38 |
-
if not final.exists():
|
| 39 |
-
return None
|
| 40 |
-
cipher = _get_cipher()
|
| 41 |
-
encrypted = final.read_bytes()
|
| 42 |
-
try:
|
| 43 |
-
payload = cipher.decrypt(encrypted)
|
| 44 |
-
return json.loads(payload.decode())
|
| 45 |
-
except Exception:
|
| 46 |
-
return None
|
| 47 |
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
try:
|
| 53 |
-
state_data = {
|
| 54 |
-
"hf_token": hf_token,
|
| 55 |
-
"github_token": gh_token,
|
| 56 |
-
"initialized": True,
|
| 57 |
-
"default_sandbox": DEFAULT_SANDBOX
|
| 58 |
-
}
|
| 59 |
-
atomic_write(state_data)
|
| 60 |
-
return f"Initialized. Sandbox: {DEFAULT_SANDBOX}"
|
| 61 |
-
except Exception as e:
|
| 62 |
-
return f"Error: {str(e)}"
|
| 63 |
|
| 64 |
-
|
| 65 |
-
def get_state():
|
| 66 |
-
data = atomic_read()
|
| 67 |
-
if data is None:
|
| 68 |
-
return "No state found"
|
| 69 |
-
safe = {k: v for k, v in data.items() if k not in ("hf_token", "github_token")}
|
| 70 |
-
safe["_has_hf_token"] = "hf_token" in data
|
| 71 |
-
safe["_has_github_token"] = "github_token" in data
|
| 72 |
-
return json.dumps(safe, indent=2)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
def handle_gh_token(t: str, u: str):
|
| 76 |
-
if not t:
|
| 77 |
-
return "No token provided"
|
| 78 |
-
data = atomic_read() or {}
|
| 79 |
-
data["github_token"] = t
|
| 80 |
-
data["github_user"] = u
|
| 81 |
-
atomic_write(data)
|
| 82 |
-
return f"Saved for {u}"
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
with gr.Blocks(title="Altamira Orchestrator") as demo:
|
| 86 |
-
gr.Markdown("# Altamira Orchestration Engine v1.2.0")
|
| 87 |
-
with gr.Tab("Gatekeeper"):
|
| 88 |
-
hf_in = gr.Textbox(label="HF Write Token", type="password")
|
| 89 |
-
gh_in = gr.Textbox(label="GitHub PAT Token", type="password")
|
| 90 |
-
init_btn = gr.Button("Run Initialization")
|
| 91 |
-
init_out = gr.Textbox(label="Result")
|
| 92 |
-
init_btn.click(fn=initialize_full, inputs=[hf_in, gh_in], outputs=init_out)
|
| 93 |
-
with gr.Tab("State"):
|
| 94 |
-
refresh_btn = gr.Button("Read State")
|
| 95 |
-
state_display = gr.Textbox(label="Current State")
|
| 96 |
-
refresh_btn.click(fn=get_state, outputs=state_display)
|
| 97 |
-
with gr.Tab("Auth"):
|
| 98 |
-
gh_token_field = gr.Textbox(label="GitHub Token", type="password")
|
| 99 |
-
gh_user_field = gr.Textbox(label="GitHub User")
|
| 100 |
-
gh_status = gr.Textbox(label="Status")
|
| 101 |
-
save_gh = gr.Button("Save GitHub Token")
|
| 102 |
-
save_gh.click(fn=handle_gh_token, inputs=[gh_token_field, gh_user_field], outputs=gh_status)
|
| 103 |
-
|
| 104 |
-
fastapi_app = FastAPI()
|
| 105 |
-
|
| 106 |
-
@fastapi_app.get("/health")
|
| 107 |
async def health():
|
| 108 |
-
return JSONResponse({"status": "
|
| 109 |
-
|
| 110 |
-
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import JSONResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
app = FastAPI()
|
| 5 |
|
| 6 |
+
@app.get("/")
|
| 7 |
+
async def root():
|
| 8 |
+
return JSONResponse({"status": "alive", "message": "Hello from Altamira"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
@app.get("/health")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
async def health():
|
| 12 |
+
return JSONResponse({"status": "healthy"})
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,2 @@
|
|
| 1 |
-
gradio==4.44.1
|
| 2 |
-
huggingface_hub==0.25.2
|
| 3 |
fastapi>=0.104.0
|
| 4 |
uvicorn[standard]>=0.24.0
|
| 5 |
-
cryptography>=41.0.0
|
| 6 |
-
httpx>=0.25.0
|
|
|
|
|
|
|
|
|
|
| 1 |
fastapi>=0.104.0
|
| 2 |
uvicorn[standard]>=0.24.0
|
|
|
|
|
|