Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ from fastapi.responses import HTMLResponse, JSONResponse
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import Dict
|
| 5 |
import time
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI(title="Tessai LLM Bridge", version="0.1.0")
|
| 8 |
|
|
@@ -62,152 +64,23 @@ def count_active_sessions(window_seconds: int = 300) -> int:
|
|
| 62 |
|
| 63 |
|
| 64 |
# -----------------------------
|
| 65 |
-
# LLM integration
|
| 66 |
-
# -----------------------------
|
| 67 |
-
|
| 68 |
-
def call_llm(message: str, context: Dict[str, str] | None = None) -> tuple[str, int]:
|
| 69 |
-
"""
|
| 70 |
-
Replace this with your real LLM call.
|
| 71 |
-
|
| 72 |
-
For now, it just echoes the message and pretends each character is a token.
|
| 73 |
-
This keeps the server functional while you wire in the real backend.
|
| 74 |
-
"""
|
| 75 |
-
reply = f"Echo: {message}"
|
| 76 |
-
tokens_used = len(message)
|
| 77 |
-
return reply, tokens_used
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
# -----------------------------
|
| 81 |
-
# API endpoints
|
| 82 |
# -----------------------------
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
reply, tokens_used = call_llm(payload.message, payload.context)
|
| 88 |
-
record_request(payload.session_id, tokens_used)
|
| 89 |
-
|
| 90 |
-
return ChatResponse(
|
| 91 |
-
session_id=payload.session_id,
|
| 92 |
-
reply=reply,
|
| 93 |
-
tokens_used=tokens_used,
|
| 94 |
-
)
|
| 95 |
|
| 96 |
|
| 97 |
-
|
| 98 |
-
async def health():
|
| 99 |
-
return JSONResponse(
|
| 100 |
-
{
|
| 101 |
-
"status": "ok",
|
| 102 |
-
"total_requests": metrics["total_requests"],
|
| 103 |
-
"total_tokens": metrics["total_tokens"],
|
| 104 |
-
"active_sessions_5m": count_active_sessions(300),
|
| 105 |
-
}
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
@app.get("/admin", response_class=HTMLResponse)
|
| 110 |
-
async def admin_dashboard():
|
| 111 |
-
active_5m = count_active_sessions(300)
|
| 112 |
-
rows = []
|
| 113 |
-
|
| 114 |
-
for sid, s in metrics["sessions"].items():
|
| 115 |
-
last_seen = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(s["last_seen"]))
|
| 116 |
-
rows.append(
|
| 117 |
-
f"<tr>"
|
| 118 |
-
f"<td>{sid}</td>"
|
| 119 |
-
f"<td>{s['requests']}</td>"
|
| 120 |
-
f"<td>{s['tokens']}</td>"
|
| 121 |
-
f"<td>{last_seen}</td>"
|
| 122 |
-
f"</tr>"
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
rows_html = "\n".join(rows) if rows else "<tr><td colspan='4'>No sessions yet</td></tr>"
|
| 126 |
-
|
| 127 |
-
html = f"""
|
| 128 |
-
<!doctype html>
|
| 129 |
-
<html>
|
| 130 |
-
<head>
|
| 131 |
-
<title>Tessai LLM Admin</title>
|
| 132 |
-
<meta charset="utf-8" />
|
| 133 |
-
<style>
|
| 134 |
-
body {{
|
| 135 |
-
font-family: system-ui, sans-serif;
|
| 136 |
-
margin: 20px;
|
| 137 |
-
}}
|
| 138 |
-
h1, h2 {{
|
| 139 |
-
margin-bottom: 0.2rem;
|
| 140 |
-
}}
|
| 141 |
-
.metrics {{
|
| 142 |
-
display: flex;
|
| 143 |
-
gap: 1.5rem;
|
| 144 |
-
margin-bottom: 1.5rem;
|
| 145 |
-
}}
|
| 146 |
-
.metric-card {{
|
| 147 |
-
padding: 1rem 1.5rem;
|
| 148 |
-
border-radius: 8px;
|
| 149 |
-
border: 1px solid #ddd;
|
| 150 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
|
| 151 |
-
}}
|
| 152 |
-
table {{
|
| 153 |
-
border-collapse: collapse;
|
| 154 |
-
width: 100%;
|
| 155 |
-
}}
|
| 156 |
-
th, td {{
|
| 157 |
-
border: 1px solid #ddd;
|
| 158 |
-
padding: 8px;
|
| 159 |
-
font-size: 0.9rem;
|
| 160 |
-
}}
|
| 161 |
-
th {{
|
| 162 |
-
background: #f4f4f4;
|
| 163 |
-
text-align: left;
|
| 164 |
-
}}
|
| 165 |
-
</style>
|
| 166 |
-
</head>
|
| 167 |
-
<body>
|
| 168 |
-
<h1>Tessai LLM Bridge</h1>
|
| 169 |
-
<p>Simple meter board for current traffic.</p>
|
| 170 |
-
|
| 171 |
-
<div class="metrics">
|
| 172 |
-
<div class="metric-card">
|
| 173 |
-
<h2>Total requests</h2>
|
| 174 |
-
<p>{metrics["total_requests"]}</p>
|
| 175 |
-
</div>
|
| 176 |
-
<div class="metric-card">
|
| 177 |
-
<h2>Total tokens (approx)</h2>
|
| 178 |
-
<p>{metrics["total_tokens"]}</p>
|
| 179 |
-
</div>
|
| 180 |
-
<div class="metric-card">
|
| 181 |
-
<h2>Active sessions (last 5 min)</h2>
|
| 182 |
-
<p>{active_5m}</p>
|
| 183 |
-
</div>
|
| 184 |
-
</div>
|
| 185 |
-
|
| 186 |
-
<h2>Sessions</h2>
|
| 187 |
-
<table>
|
| 188 |
-
<thead>
|
| 189 |
-
<tr>
|
| 190 |
-
<th>Session ID</th>
|
| 191 |
-
<th>Requests</th>
|
| 192 |
-
<th>Tokens</th>
|
| 193 |
-
<th>Last seen</th>
|
| 194 |
-
</tr>
|
| 195 |
-
</thead>
|
| 196 |
-
<tbody>
|
| 197 |
-
{rows_html}
|
| 198 |
-
</tbody>
|
| 199 |
-
</table>
|
| 200 |
-
</body>
|
| 201 |
-
</html>
|
| 202 |
"""
|
| 203 |
-
|
| 204 |
-
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
if __name__ == "__main__":
|
| 211 |
-
import uvicorn
|
| 212 |
|
| 213 |
-
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import Dict
|
| 5 |
import time
|
| 6 |
+
import os
|
| 7 |
+
import httpx
|
| 8 |
|
| 9 |
app = FastAPI(title="Tessai LLM Bridge", version="0.1.0")
|
| 10 |
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
# -----------------------------
|
| 67 |
+
# LLM integration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# -----------------------------
|
| 69 |
+
# Configure via environment variables in Hugging Face:
|
| 70 |
+
# TESSAI_LLM_URL = full URL of the model endpoint
|
| 71 |
+
# TESSAI_LLM_KEY = auth token (if needed)
|
| 72 |
|
| 73 |
|
| 74 |
+
LLM_URL = os.getenv("TESSAI_LLM_URL", "").strip()
|
| 75 |
+
LLM_KEY = os.getenv("TESSAI_LLM_KEY", "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
+
async def call_llm(message: str, context: Dict[str, str] | None = None) -> tuple[str, int]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
"""
|
| 80 |
+
Replace this with whatever target you want.
|
|
|
|
| 81 |
|
| 82 |
+
This implementation is ready for a typical Hugging Face Inference API
|
| 83 |
+
text-generation endpoint. If LLM_URL is not set, it falls back to a local echo.
|
| 84 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
# Fallback so the b
|