Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# app.py in GATEWAY Space
|
| 2 |
|
| 3 |
import os
|
| 4 |
-
from flask import Flask, request, jsonify
|
| 5 |
from flask_cors import CORS
|
| 6 |
import requests
|
| 7 |
|
|
@@ -26,6 +26,10 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
| 26 |
APP_USER = os.environ.get("APP_USER", "demo")
|
| 27 |
APP_PASSWORD = os.environ.get("APP_PASSWORD", "demo-password")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
@app.post("/login")
|
| 30 |
def login():
|
| 31 |
data = request.get_json(force=True) or {}
|
|
@@ -38,11 +42,13 @@ def login():
|
|
| 38 |
if username != APP_USER or password != APP_PASSWORD:
|
| 39 |
return jsonify({"ok": False, "error": "Invalid credentials"}), 401
|
| 40 |
|
| 41 |
-
# Create a JWT that expires in e.g. 8 hours (default can be tuned)
|
| 42 |
access_token = create_access_token(identity=username)
|
| 43 |
return jsonify({"ok": True, "token": access_token}), 200
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
@app.route("/chat", methods=["POST"])
|
| 47 |
@jwt_required()
|
| 48 |
def proxy_chat():
|
|
@@ -74,6 +80,47 @@ def proxy_chat():
|
|
| 74 |
return jsonify(data), resp.status_code
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
@app.route("/prolog/query", methods=["POST"])
|
| 78 |
@jwt_required()
|
| 79 |
def proxy_prolog_query():
|
|
@@ -99,20 +146,23 @@ def proxy_prolog_query():
|
|
| 99 |
data = resp.json()
|
| 100 |
except ValueError:
|
| 101 |
return jsonify(
|
| 102 |
-
{
|
| 103 |
-
"ok": False,
|
| 104 |
-
"error": f"Backend returned non-JSON: {resp.text[:200]}",
|
| 105 |
-
}
|
| 106 |
), 502
|
| 107 |
|
| 108 |
return jsonify(data), resp.status_code
|
| 109 |
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
@app.get("/")
|
| 112 |
def health():
|
| 113 |
return "Gateway OK", 200
|
| 114 |
|
| 115 |
|
|
|
|
|
|
|
|
|
|
| 116 |
if __name__ == "__main__":
|
| 117 |
port = int(os.environ.get("PORT", 7860))
|
| 118 |
app.run(host="0.0.0.0", port=port)
|
|
|
|
| 1 |
# app.py in GATEWAY Space
|
| 2 |
|
| 3 |
import os
|
| 4 |
+
from flask import Flask, request, jsonify, Response, stream_with_context
|
| 5 |
from flask_cors import CORS
|
| 6 |
import requests
|
| 7 |
|
|
|
|
| 26 |
APP_USER = os.environ.get("APP_USER", "demo")
|
| 27 |
APP_PASSWORD = os.environ.get("APP_PASSWORD", "demo-password")
|
| 28 |
|
| 29 |
+
|
| 30 |
+
# ============================================================
|
| 31 |
+
# LOGIN ENDPOINT
|
| 32 |
+
# ============================================================
|
| 33 |
@app.post("/login")
|
| 34 |
def login():
|
| 35 |
data = request.get_json(force=True) or {}
|
|
|
|
| 42 |
if username != APP_USER or password != APP_PASSWORD:
|
| 43 |
return jsonify({"ok": False, "error": "Invalid credentials"}), 401
|
| 44 |
|
|
|
|
| 45 |
access_token = create_access_token(identity=username)
|
| 46 |
return jsonify({"ok": True, "token": access_token}), 200
|
| 47 |
|
| 48 |
|
| 49 |
+
# ============================================================
|
| 50 |
+
# NON-STREAMING CHAT (JSON → JSON)
|
| 51 |
+
# ============================================================
|
| 52 |
@app.route("/chat", methods=["POST"])
|
| 53 |
@jwt_required()
|
| 54 |
def proxy_chat():
|
|
|
|
| 80 |
return jsonify(data), resp.status_code
|
| 81 |
|
| 82 |
|
| 83 |
+
# ============================================================
|
| 84 |
+
# STREAMING CHAT (TEXT STREAM → TEXT STREAM)
|
| 85 |
+
# ============================================================
|
| 86 |
+
@app.route("/chat/stream", methods=["POST"])
|
| 87 |
+
@jwt_required()
|
| 88 |
+
def proxy_chat_stream():
|
| 89 |
+
if HF_TOKEN is None:
|
| 90 |
+
return Response("HF_TOKEN not configured", status=500)
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
payload = request.get_json(force=True) or {}
|
| 94 |
+
except Exception:
|
| 95 |
+
return Response("Invalid JSON", status=400)
|
| 96 |
+
|
| 97 |
+
def generate():
|
| 98 |
+
# Forward streaming request to backend
|
| 99 |
+
with requests.post(
|
| 100 |
+
f"{BACKEND_BASE}/chat/stream",
|
| 101 |
+
json=payload,
|
| 102 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 103 |
+
stream=True,
|
| 104 |
+
timeout=300,
|
| 105 |
+
) as r:
|
| 106 |
+
|
| 107 |
+
# Backend error? Surface it.
|
| 108 |
+
if r.status_code != 200:
|
| 109 |
+
yield f"[Backend error {r.status_code}]"
|
| 110 |
+
return
|
| 111 |
+
|
| 112 |
+
# Yield chunks from backend to frontend
|
| 113 |
+
for chunk in r.iter_lines():
|
| 114 |
+
if chunk:
|
| 115 |
+
yield chunk.decode("utf-8")
|
| 116 |
+
|
| 117 |
+
return Response(stream_with_context(generate()),
|
| 118 |
+
mimetype="text/plain")
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# ============================================================
|
| 122 |
+
# RAW PROLOG QUERY PROXY
|
| 123 |
+
# ============================================================
|
| 124 |
@app.route("/prolog/query", methods=["POST"])
|
| 125 |
@jwt_required()
|
| 126 |
def proxy_prolog_query():
|
|
|
|
| 146 |
data = resp.json()
|
| 147 |
except ValueError:
|
| 148 |
return jsonify(
|
| 149 |
+
{"ok": False, "error": f"Backend returned non-JSON: {resp.text[:200]}"}
|
|
|
|
|
|
|
|
|
|
| 150 |
), 502
|
| 151 |
|
| 152 |
return jsonify(data), resp.status_code
|
| 153 |
|
| 154 |
|
| 155 |
+
# ============================================================
|
| 156 |
+
# HEALTH CHECK
|
| 157 |
+
# ============================================================
|
| 158 |
@app.get("/")
|
| 159 |
def health():
|
| 160 |
return "Gateway OK", 200
|
| 161 |
|
| 162 |
|
| 163 |
+
# ============================================================
|
| 164 |
+
# MAIN
|
| 165 |
+
# ============================================================
|
| 166 |
if __name__ == "__main__":
|
| 167 |
port = int(os.environ.get("PORT", 7860))
|
| 168 |
app.run(host="0.0.0.0", port=port)
|