Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213143 unmodified lines18192021222324252627282930313 unmodified lines3536373839404142431 unmodified line4546474849505110 unmodified lines6263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185import osimport jsonimport timeimport requestsimport uvicornfrom fastapi import FastAPI, Depends, HTTPException, Requestfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.security import HTTPBearer, HTTPAuthorizationCredentialsfrom fastapi.responses import StreamingResponseapp = FastAPI(title="o87Dev Cloud LLM API")security = HTTPBearer(auto_error=False)app.add_middleware(3 unmodified linesallow_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", "")# Free CPU tier: keep context small or requests will timeout after 5 minMAX_CTX = 4096MAX_OUT = 1024TIMEOUT = 240 # 4 min hard limit — under HF's 5 min killdef verify_token(creds: HTTPAuthorizationCredentials = Depends(security)):if not API_TOKEN:3 unmodified linesreturn creds.credentials@app.get("/")async def root():return {"status": "ok", "model": MODEL, "max_ctx": MAX_CTX}@app.get("/health")1 unmodified linetry: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, "max_ctx": MAX_CTX}except Exception as e:return {"status": "starting", "error": str(e)}10 unmodified lines@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)payload = {"model": model,"messages": body.get("messages", []),"stream": stream,"options": {"num_ctx": MAX_CTX,"num_predict": min(body.get("max_tokens", MAX_OUT), MAX_OUT),"temperature": body.get("temperature", 0.7),}}if stream:def generate():try:with requests.post(f"{OLLAMA_BASE}/v1/chat/completions",json=payload, stream=True, timeout=TIMEOUT) as r:for chunk in r.iter_content(chunk_size=None):if chunk:yield chunkexcept Exception as e:yield f"data: {{\"error\": \"{e}\"}}\n\n".encode()return StreamingResponse(generate(), media_type="text/event-stream")try:r = requests.post(f"{OLLAMA_BASE}/v1/chat/completions", json=payload, timeout=TIMEOUT)return r.json()except requests.Timeout:raise HTTPException(504, "Inference timeout — try a shorter prompt")@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)payload = {"model": model,"messages": body.get("messages", []),"stream": stream,"options": {"num_ctx": MAX_CTX,"num_predict": min(body.get("max_tokens", MAX_OUT), MAX_OUT),"temperature": body.get("temperature", 0.7),}}if stream: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".encode()yield f"event: content_block_start\ndata: {json.dumps({'type':'content_block_start','index':0,'content_block':{'type':'text','text':''}})}\n\n".encode()yield b"event: ping\ndata: {\"type\":\"ping\"}\n\n"out_tokens = 0try:with requests.post(f"{OLLAMA_BASE}/v1/chat/completions",json=payload, stream=True, timeout=TIMEOUT) as r:buf = ""for chunk in r.iter_content(chunk_size=None):if not chunk:continuebuf += chunk.decode("utf-8", errors="ignore")lines = buf.split("\n")buf = lines.pop()for line in lines:line = line.strip()if not line or not line.startswith("data: "):continuejs = line[6:]if js == "[DONE]":breaktry:d = json.loads(js)if d.get("usage"):out_tokens = d["usage"].get("completion_tokens", 0)text = (d.get("choices") or [{}])[0].get("delta", {}).get("content", "")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".encode()if (d.get("choices") or [{}])[0].get("finish_reason"):breakexcept Exception:passexcept 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".encode()yield b"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':out_tokens}})}\n\n".encode()yield b"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"return StreamingResponse(generate_anthropic(), media_type="text/event-stream")try:r = requests.post(f"{OLLAMA_BASE}/v1/chat/completions", json=payload, timeout=TIMEOUT)data = r.json()content = (data.get("choices") or [{}])[0].get("message", {}).get("content", "")return {"id": data.get("id", f"msg_{int(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 requests.Timeout:raise HTTPException(504, "Inference timeout — try a shorter prompt")if __name__ == "__main__":123456789101112131415161718192021222324252627282930313233343536373839404142434445463 unmodified lines5051525354555657585960616263643 unmodified lines68697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131 unmodified line11511611711811912012112212312412512612710 unmodified lines138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316import osimport jsonimport timeimport asyncioimport requestsimport uvicornfrom fastapi import FastAPI, Depends, HTTPException, Requestfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.security import HTTPBearer, HTTPAuthorizationCredentialsfrom fastapi.responses import StreamingResponsefrom contextlib import asynccontextmanagerimport subprocessimport shutil# Check if ollama is availableOLLAMA_AVAILABLE = shutil.which("ollama") is not None@asynccontextmanagerasync def lifespan(app: FastAPI):"""Startup and shutdown events"""if OLLAMA_AVAILABLE:print("Starting Ollama service...")subprocess.Popen(["ollama", "serve"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)await asyncio.sleep(3) # Wait for Ollama to start# Set keep-alive to prevent model unloadingos.environ["OLLAMA_KEEP_ALIVE"] = "24h"# Pull model if neededtry:r = requests.get(f"{OLLAMA_BASE}/api/tags", timeout=5)models = [m["name"] for m in r.json().get("models", [])]if MODEL not in models:print(f"Pulling model {MODEL}...")subprocess.run(["ollama", "pull", MODEL], check=False)except Exception as e:print(f"Warning: Could not check/pull model: {e}")yieldprint("Shutting down...")app = FastAPI(title="o87Dev Cloud LLM API", lifespan=lifespan)security = HTTPBearer(auto_error=False)app.add_middleware(3 unmodified linesallow_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", "")MAX_CTX = int(os.environ.get("MAX_CTX", "4096"))MAX_OUT = int(os.environ.get("MAX_OUT", "1024"))TIMEOUT = int(os.environ.get("TIMEOUT", "240")) # 4 min limit# Semaphore to limit concurrent requests (prevents OOM)semaphore = asyncio.Semaphore(1) # Only 1 request at a time for CPU Spacesdef verify_token(creds: HTTPAuthorizationCredentials = Depends(security)):if not API_TOKEN:3 unmodified linesreturn creds.credentialsasync def wait_for_ollama(max_retries=10, delay=1):"""Wait for Ollama to be ready, with retries"""for i in range(max_retries):try:r = requests.get(f"{OLLAMA_BASE}/api/tags", timeout=2)if r.status_code == 200:return Trueexcept:passawait asyncio.sleep(delay)return Falseasync def ensure_model_loaded(model_name: str = None):"""Pre-load model with a dummy request to force it into memory"""model = model_name or MODELtry:# Check if model is already loadedr = requests.get(f"{OLLAMA_BASE}/api/ps", timeout=2)loaded = [m.get("model") for m in r.json().get("models", [])]if model not in loaded:print(f"Pre-loading model {model}...")requests.post(f"{OLLAMA_BASE}/api/generate",json={"model": model, "prompt": "test", "stream": False},timeout=30)print(f"Model {model} loaded")except Exception as e:print(f"Warning: Could not pre-load model: {e}")@app.get("/")async def root():return {"status": "ok","model": MODEL,"max_ctx": MAX_CTX,"ollama_available": OLLAMA_AVAILABLE}@app.get("/health")1 unmodified linetry:r = requests.get(f"{OLLAMA_BASE}/api/tags", timeout=5)models = [m["name"] for m in r.json().get("models", [])]return {"status": "ok" if MODEL in models else "model_missing","model": MODEL,"model_available": MODEL in models,"available_models": models,"max_ctx": MAX_CTX}except Exception as e:return {"status": "starting", "error": str(e)}10 unmodified lines@app.post("/v1/chat/completions")async def chat_completions(request: Request, token: str = Depends(verify_token)):"""OpenAI-compatible endpoint with retries and better error handling"""# Wait for Ollama to be readyif not await wait_for_ollama():raise HTTPException(503, "Ollama service not ready")async with semaphore:body = await request.json()model = body.get("model", MODEL)stream = body.get("stream", False)# Ensure model is loaded before proceedingawait ensure_model_loaded(model)payload = {"model": model,"messages": body.get("messages", []),"stream": stream,"options": {"num_ctx": MAX_CTX,"num_predict": min(body.get("max_tokens", MAX_OUT), MAX_OUT),"temperature": body.get("temperature", 0.7),}}if stream:def generate():try:with requests.post(f"{OLLAMA_BASE}/v1/chat/completions",json=payload,stream=True,timeout=TIMEOUT) as r:if r.status_code != 200:error_msg = f"Ollama error: {r.status_code}"yield f"data: {json.dumps({'error': error_msg})}\n\n".encode()yield b"data: [DONE]\n\n"returnfor chunk in r.iter_content(chunk_size=None):if chunk:yield chunkexcept requests.Timeout:yield f"data: {json.dumps({'error': 'Request timeout - try a shorter prompt'})}\n\n".encode()yield b"data: [DONE]\n\n"except Exception as e:yield f"data: {json.dumps({'error': str(e)})}\n\n".encode()yield b"data: [DONE]\n\n"return StreamingResponse(generate(), media_type="text/event-stream")# Non-streaming request with retry logicmax_retries = 2for attempt in range(max_retries):try:r = requests.post(f"{OLLAMA_BASE}/v1/chat/completions",json=payload,timeout=TIMEOUT)if r.status_code == 200:return r.json()elif r.status_code == 404:# Model not found - try to pull itif attempt < max_retries - 1:print(f"Model {model} not found, attempting pull...")subprocess.run(["ollama", "pull", model], check=False)await asyncio.sleep(5)continueraise HTTPException(r.status_code, f"Ollama error: {r.text}")except requests.Timeout:if attempt == max_retries - 1:raise HTTPException(504, "Inference timeout — try a shorter prompt")await asyncio.sleep(2)except Exception as e:if attempt == max_retries - 1:raise HTTPException(500, str(e))await asyncio.sleep(2)@app.post("/v1/messages")async def messages(request: Request, token: str = Depends(verify_token)):"""Anthropic-compatible messages endpoint"""if not await wait_for_ollama():raise HTTPException(503, "Ollama service not ready")async with semaphore:body = await request.json()model = body.get("model", MODEL)stream = body.get("stream", False)await ensure_model_loaded(model)payload = {"model": model,"messages": body.get("messages", []),"stream": stream,"options": {"num_ctx": MAX_CTX,"num_predict": min(body.get("max_tokens", MAX_OUT), MAX_OUT),"temperature": body.get("temperature", 0.7),}}if stream: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".encode()yield f"event: content_block_start\ndata: {json.dumps({'type':'content_block_start','index':0,'content_block':{'type':'text','text':''}})}\n\n".encode()yield b"event: ping\ndata: {\"type\":\"ping\"}\n\n"out_tokens = 0try:with requests.post(f"{OLLAMA_BASE}/v1/chat/completions",json=payload, stream=True, timeout=TIMEOUT) as r:if r.status_code != 200:yield f"event: content_block_delta\ndata: {json.dumps({'type':'content_block_delta','index':0,'delta':{'type':'text_delta','text':f'Error: Ollama returned {r.status_code}'}})}\n\n".encode()else:buf = ""for chunk in r.iter_content(chunk_size=None):if not chunk:continuebuf += chunk.decode("utf-8", errors="ignore")lines = buf.split("\n")buf = lines.pop()for line in lines:line = line.strip()if not line or not line.startswith("data: "):continuejs = line[6:]if js == "[DONE]":breaktry:d = json.loads(js)if d.get("usage"):out_tokens = d["usage"].get("completion_tokens", 0)text = (d.get("choices") or [{}])[0].get("delta", {}).get("content", "")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".encode()except:passexcept 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".encode()yield b"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':out_tokens}})}\n\n".encode()yield b"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"return StreamingResponse(generate_anthropic(), media_type="text/event-stream")# Non-streamingtry:r = requests.post(f"{OLLAMA_BASE}/v1/chat/completions", json=payload, timeout=TIMEOUT)data = r.json()content = (data.get("choices") or [{}])[0].get("message", {}).get("content", "")return {"id": data.get("id", f"msg_{int(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 requests.Timeout:raise HTTPException(504, "Inference timeout — try a shorter prompt")if __name__ == "__main__":
app.py+248-117