File size: 1,830 Bytes
16e01a9
 
5038f49
ce4e5c9
 
16e01a9
 
82c5a00
188bb6e
82c5a00
 
188bb6e
5038f49
 
c82ed9e
 
 
5038f49
 
188bb6e
 
 
ce4e5c9
 
 
 
 
188bb6e
add7e8f
 
 
 
 
 
 
 
188bb6e
16e01a9
 
 
 
 
 
 
188bb6e
9b1c7a4
35ca4ed
188bb6e
ce4e5c9
16e01a9
 
188bb6e
16e01a9
188bb6e
16e01a9
 
188bb6e
 
 
 
 
 
 
 
 
 
16e01a9
add7e8f
16e01a9
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
import json
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
from pydantic import BaseModel
from typing import List

# Initialize FastAPI
app = FastAPI()

# Enable CORS for frontend communication
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load the model
# We use n_ctx=512 to keep memory footprint low
print("Loading model... please wait.")
MODEL_PATH = hf_hub_download(
    repo_id="Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF", 
    filename="qwen2.5-coder-1.5b-instruct-q4_k_m.gguf"
)

# Force CPU mode (n_gpu_layers=0) for maximum stability on Free Tier
llm = Llama(
    model_path=MODEL_PATH, 
    n_ctx=512, 
    n_gpu_layers=0, 
    n_threads=1
)
print("Model loaded successfully.")

# Pydantic models for request validation
class ChatMessage(BaseModel):
    role: str
    content: str

class ChatRequest(BaseModel):
    messages: List[ChatMessage]

# Endpoints
@app.get("/")
def read_root():
    return {"message": "Server is up and waiting for chat!"}

@app.post("/v1/chat/completions")
async def chat_completion(request: ChatRequest):
    print("Received chat request...")
    try:
        # Convert request to history format
        history = [{"role": msg.role, "content": msg.content} for msg in request.messages]
        
        # Run inference with stream=False to ensure stable connection
        response = llm.create_chat_completion(
            messages=history,
            temperature=0.2,
            stream=False 
        )
        
        print("Inference finished.")
        return response
        
    except Exception as e:
        print(f"ERROR: {e}")
        raise HTTPException(status_code=500, detail=str(e))