File size: 9,651 Bytes
135c42a
 
 
be43b96
135c42a
 
 
 
 
 
 
 
 
be43b96
 
135c42a
 
 
 
 
 
 
 
 
 
 
be43b96
135c42a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be43b96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import io
import json
import os
import pathlib
import time
from typing import Optional

import httpx
import pdfplumber
from docx import Document as DocxDocument
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field

load_dotenv()

CONCENTRATE_API_KEY = os.getenv("CONCENTRATE_API_KEY", "")
CONCENTRATE_BASE_URL = "https://api.concentrate.ai/v1"

app = FastAPI(title="LLM Arena Backend")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ── Model list cache ──────────────────────────────────────────────────────────

_models_cache: dict = {"data": None, "fetched_at": 0.0}
CACHE_TTL_SECONDS = 300


def _headers() -> dict[str, str]:
    return {
        "Authorization": f"Bearer {CONCENTRATE_API_KEY}",
        "Content-Type": "application/json",
    }


@app.get("/api/models")
async def list_models():
    """Return available models, cached for 5 minutes."""
    now = time.time()
    if _models_cache["data"] and now - _models_cache["fetched_at"] < CACHE_TTL_SECONDS:
        return _models_cache["data"]

    if not CONCENTRATE_API_KEY:
        raise HTTPException(status_code=500, detail="CONCENTRATE_API_KEY not set")

    async with httpx.AsyncClient(timeout=15) as client:
        resp = await client.get(f"{CONCENTRATE_BASE_URL}/models", headers=_headers())

    if resp.status_code != 200:
        raise HTTPException(status_code=resp.status_code, detail=resp.text)

    raw = resp.json()
    models_list = raw.get("data", raw) if isinstance(raw, dict) else raw

    simplified = []
    for m in models_list:
        slug = m.get("slug", "")
        display_name = m.get("name", slug)
        author = m.get("author", {}).get("slug", "unknown")
        providers = m.get("providers", {})

        if providers:
            for provider_slug in providers:
                model_id = f"{provider_slug}/{slug}"
                simplified.append({"id": model_id, "name": display_name, "provider": provider_slug})
        else:
            simplified.append({"id": slug, "name": display_name, "provider": author})

    simplified.sort(key=lambda x: (x["provider"], x["name"]))
    _models_cache["data"] = simplified
    _models_cache["fetched_at"] = now
    return simplified


# ── File upload ───────────────────────────────────────────────────────────────

MAX_FILE_SIZE = 10 * 1024 * 1024  # 10 MB
ALLOWED_EXTENSIONS = {".pdf", ".docx", ".doc", ".txt", ".md"}


def _extract_text_pdf(data: bytes) -> str:
    with pdfplumber.open(io.BytesIO(data)) as pdf:
        pages = []
        for page in pdf.pages:
            text = page.extract_text()
            if text:
                pages.append(text)
        return "\n\n".join(pages)


def _extract_text_docx(data: bytes) -> str:
    doc = DocxDocument(io.BytesIO(data))
    return "\n\n".join(p.text for p in doc.paragraphs if p.text.strip())


def _extract_text_plain(data: bytes) -> str:
    return data.decode("utf-8", errors="replace")


@app.post("/api/upload")
async def upload_file(file: UploadFile):
    """Extract text from an uploaded PDF, DOCX, or TXT file."""
    if not file.filename:
        raise HTTPException(status_code=400, detail="No filename provided")

    ext = os.path.splitext(file.filename)[1].lower()
    if ext not in ALLOWED_EXTENSIONS:
        raise HTTPException(
            status_code=400,
            detail=f"Unsupported file type '{ext}'. Allowed: {', '.join(sorted(ALLOWED_EXTENSIONS))}",
        )

    data = await file.read()
    if len(data) > MAX_FILE_SIZE:
        raise HTTPException(status_code=400, detail="File exceeds 10 MB limit")

    try:
        if ext == ".pdf":
            text = _extract_text_pdf(data)
        elif ext in (".docx", ".doc"):
            text = _extract_text_docx(data)
        else:
            text = _extract_text_plain(data)
    except Exception as e:
        raise HTTPException(status_code=422, detail=f"Failed to extract text: {e}")

    if not text.strip():
        raise HTTPException(status_code=422, detail="No readable text found in file")

    char_count = len(text)
    word_count = len(text.split())

    return {
        "filename": file.filename,
        "text": text,
        "char_count": char_count,
        "word_count": word_count,
    }


# ── Streaming proxy ───────────────────────────────────────────────────────────


class StreamRequest(BaseModel):
    model: str
    prompt: str
    document_text: Optional[str] = None
    temperature: Optional[float] = Field(None, ge=0, le=2)
    max_output_tokens: Optional[int] = Field(None, ge=1, le=16384)
    top_p: Optional[float] = Field(None, ge=0, le=1)


@app.post("/api/stream")
async def stream_response(req: StreamRequest):
    """SSE proxy: streams a Concentrate AI response back to the browser."""
    if not CONCENTRATE_API_KEY:
        raise HTTPException(status_code=500, detail="CONCENTRATE_API_KEY not set")

    if req.document_text:
        full_input = (
            f"The user has provided the following document for context:\n\n"
            f"---\n{req.document_text}\n---\n\n"
            f"User's question/instruction: {req.prompt}"
        )
    else:
        full_input = req.prompt

    body: dict = {
        "model": req.model,
        "input": full_input,
        "stream": True,
    }
    if req.temperature is not None:
        body["temperature"] = req.temperature
    if req.max_output_tokens is not None:
        body["max_output_tokens"] = req.max_output_tokens
    if req.top_p is not None:
        body["top_p"] = req.top_p

    async def event_generator():
        start_time = time.time()
        first_token_time: float | None = None

        async with httpx.AsyncClient(timeout=httpx.Timeout(connect=10, read=120, write=10, pool=10)) as client:
            async with client.stream(
                "POST",
                f"{CONCENTRATE_BASE_URL}/responses",
                headers=_headers(),
                json=body,
            ) as resp:
                if resp.status_code != 200:
                    error_body = await resp.aread()
                    yield f"event: error\ndata: {json.dumps({'error': error_body.decode()})}\n\n"
                    return

                async for raw_line in resp.aiter_lines():
                    if not raw_line.strip():
                        continue

                    if raw_line.startswith("event: "):
                        yield raw_line + "\n"
                        continue

                    if raw_line.startswith("data: "):
                        data_str = raw_line[6:]
                        try:
                            event = json.loads(data_str)
                        except json.JSONDecodeError:
                            yield raw_line + "\n\n"
                            continue

                        event_type = event.get("type", "")

                        if event_type == "response.output_text.delta" and first_token_time is None:
                            first_token_time = time.time()

                        yield raw_line + "\n\n"

                        if event_type in ("response.completed", "response.failed", "response.incomplete"):
                            end_time = time.time()
                            stats = {
                                "type": "arena.stats",
                                "total_time_ms": round((end_time - start_time) * 1000),
                                "time_to_first_token_ms": (
                                    round((first_token_time - start_time) * 1000)
                                    if first_token_time
                                    else None
                                ),
                            }
                            if event_type == "response.completed":
                                usage = event.get("response", {}).get("usage", {})
                                stats["usage"] = usage
                            yield f"event: arena.stats\ndata: {json.dumps(stats)}\n\n"
                    else:
                        yield raw_line + "\n\n"

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "X-Accel-Buffering": "no",
        },
    )


@app.get("/api/health")
async def health():
    return {"status": "ok"}


# ── Static file serving (production / HF Spaces) ─────────────────────────────

STATIC_DIR = pathlib.Path(__file__).resolve().parent / "static"

if STATIC_DIR.is_dir():
    app.mount("/assets", StaticFiles(directory=STATIC_DIR / "assets"), name="assets")

    @app.get("/{full_path:path}")
    async def serve_spa(full_path: str):
        """Serve the React SPA for any non-API route."""
        file_path = STATIC_DIR / full_path
        if file_path.is_file():
            return FileResponse(file_path)
        return FileResponse(STATIC_DIR / "index.html")