File size: 8,896 Bytes
78822f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess
import logging
import json
import requests
import uvicorn
from fastapi import FastAPI, Depends, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.responses import StreamingResponse
from huggingface_hub import HfApi

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(title="o87Dev Cloud LLM API")
security = HTTPBearer()

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

OLLAMA_BASE = "http://localhost:11434"
MODEL = os.environ.get("DEFAULT_MODEL", "qwen2.5-coder:7b-instruct-q4_K_M")
API_TOKEN = os.environ.get("API_TOKEN")  # Set as Space secret

# ── Auth ──────────────────────────────────────────────────────────────────────

def verify_token(creds: HTTPAuthorizationCredentials = Depends(security)):
    token = creds.credentials
    # If API_TOKEN secret is set, validate against it directly (faster)
    if API_TOKEN:
        if token != API_TOKEN:
            raise HTTPException(401, "Invalid token")
        return token
    # Fallback: validate as HF token
    try:
        HfApi().whoami(token=token)
    except Exception:
        raise HTTPException(401, "Invalid Hugging Face token")
    return token

# ── Health ────────────────────────────────────────────────────────────────────

@app.get("/health")
async def health():
    try:
        r = requests.get(f"{OLLAMA_BASE}/api/tags", timeout=5)
        models = [m["name"] for m in r.json().get("models", [])]
        return {"status": "ok", "model": MODEL, "available_models": models}
    except Exception as e:
        return {"status": "starting", "error": str(e)}

# ── OpenAI-compatible /v1/chat/completions ────────────────────────────────────

@app.post("/v1/chat/completions")
async def chat_completions(request: Request, token: str = Depends(verify_token)):
    body = await request.json()
    model = body.get("model", MODEL)
    stream = body.get("stream", False)

    ollama_payload = {
        "model": model,
        "messages": body.get("messages", []),
        "stream": stream,
        "options": {
            "num_ctx": body.get("max_tokens", 32768),
            "temperature": body.get("temperature", 0.7),
        }
    }

    if stream:
        def generate():
            try:
                with requests.post(
                    f"{OLLAMA_BASE}/v1/chat/completions",
                    json=ollama_payload,
                    stream=True,
                    timeout=300
                ) as r:
                    for chunk in r.iter_content(chunk_size=None):
                        if chunk:
                            yield chunk
            except Exception as e:
                yield f"data: {{\"error\": \"{str(e)}\"}}\n\n"
        return StreamingResponse(generate(), media_type="text/event-stream")
    else:
        try:
            r = requests.post(
                f"{OLLAMA_BASE}/v1/chat/completions",
                json=ollama_payload,
                timeout=300
            )
            return r.json()
        except Exception as e:
            raise HTTPException(500, str(e))

# ── Anthropic-compatible /v1/messages ─────────────────────────────────────────

@app.post("/v1/messages")
async def messages(request: Request, token: str = Depends(verify_token)):
    body = await request.json()
    model = body.get("model", MODEL)
    stream = body.get("stream", False)

    ollama_payload = {
        "model": model,
        "messages": body.get("messages", []),
        "stream": stream,
        "options": {
            "num_ctx": body.get("max_tokens", 32768),
            "temperature": body.get("temperature", 0.7),
        }
    }

    if stream:
        import time

        def generate_anthropic():
            msg_id = f"msg_{int(time.time())}"
            yield f"event: message_start\ndata: {json.dumps({'type':'message_start','message':{'id':msg_id,'type':'message','role':'assistant','content':[],'model':model,'stop_reason':None,'usage':{'input_tokens':0,'output_tokens':0}}})}\n\n"
            yield f"event: content_block_start\ndata: {json.dumps({'type':'content_block_start','index':0,'content_block':{'type':'text','text':''}})}\n\n"
            yield f"event: ping\ndata: {{\"type\":\"ping\"}}\n\n"

            output_tokens = 0
            try:
                with requests.post(
                    f"{OLLAMA_BASE}/v1/chat/completions",
                    json=ollama_payload,
                    stream=True,
                    timeout=300
                ) as r:
                    buffer = ""
                    for chunk in r.iter_content(chunk_size=None):
                        if not chunk:
                            continue
                        buffer += chunk.decode("utf-8", errors="ignore")
                        lines = buffer.split("\n")
                        buffer = lines.pop()
                        for line in lines:
                            line = line.strip()
                            if not line or not line.startswith("data: "):
                                continue
                            js = line[6:]
                            if js == "[DONE]":
                                break
                            try:
                                data = json.loads(js)
                                if data.get("usage"):
                                    output_tokens = data["usage"].get("completion_tokens", 0)
                                delta = data.get("choices", [{}])[0].get("delta", {})
                                text = delta.get("content") or delta.get("reasoning") or ""
                                if text:
                                    yield f"event: content_block_delta\ndata: {json.dumps({'type':'content_block_delta','index':0,'delta':{'type':'text_delta','text':text}})}\n\n"
                                if data.get("choices", [{}])[0].get("finish_reason"):
                                    break
                            except Exception:
                                pass
            except Exception as e:
                yield f"event: content_block_delta\ndata: {json.dumps({'type':'content_block_delta','index':0,'delta':{'type':'text_delta','text':f'Error: {e}'}})}\n\n"

            yield f"event: content_block_stop\ndata: {{\"type\":\"content_block_stop\",\"index\":0}}\n\n"
            yield f"event: message_delta\ndata: {json.dumps({'type':'message_delta','delta':{'stop_reason':'end_turn','stop_sequence':None},'usage':{'output_tokens':output_tokens}})}\n\n"
            yield f"event: message_stop\ndata: {{\"type\":\"message_stop\"}}\n\n"

        return StreamingResponse(generate_anthropic(), media_type="text/event-stream")
    else:
        try:
            r = requests.post(
                f"{OLLAMA_BASE}/v1/chat/completions",
                json=ollama_payload,
                timeout=300
            )
            data = r.json()
            content = data.get("choices", [{}])[0].get("message", {}).get("content", "")
            return {
                "id": data.get("id", f"msg_{int(__import__('time').time())}"),
                "type": "message",
                "role": "assistant",
                "content": [{"type": "text", "text": content}],
                "model": model,
                "stop_reason": "end_turn",
                "usage": {
                    "input_tokens": data.get("usage", {}).get("prompt_tokens", 0),
                    "output_tokens": data.get("usage", {}).get("completion_tokens", 0)
                }
            }
        except Exception as e:
            raise HTTPException(500, str(e))

# ── Models list ───────────────────────────────────────────────────────────────

@app.get("/v1/models")
async def list_models(token: str = Depends(verify_token)):
    try:
        r = requests.get(f"{OLLAMA_BASE}/api/tags", timeout=5)
        models = [{"id": m["name"], "object": "model"} for m in r.json().get("models", [])]
        return {"object": "list", "data": models}
    except Exception:
        return {"object": "list", "data": [{"id": MODEL, "object": "model"}]}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)