Spaces:
Sleeping
Sleeping
Dmitry Beresnev commited on
Commit ·
dde400a
1
Parent(s): 8837f11
fix dockerfile and app module
Browse filesFiles changed (2)
- Dockerfile+22-14
- app.py+167-47
8 unmodified lines910111212131415151617181920211920212223242516 unmodified lines4243444546474849505152535455565758596061626364656667556869707172596061626364656673748 unmodified lines libcurl4-openssl-dev \ && rm -rf /var/lib/apt/lists/*
# Clone and build llama.cpp# Clone and build llama.cpp with optimizations for speedWORKDIR /build# Cache bust to force fresh buildARG CACHEBUST=1ARG CACHEBUST=2RUN git clone https://github.com/ggerganov/llama.cpp.git && \ cd llama.cpp && \ cmake -B build -DCMAKE_BUILD_TYPE=Release \ -DGGML_NATIVE=OFF \ -DGGML_AVX2=OFF \ -DGGML_OPENMP=OFF && \ -DGGML_NATIVE=ON \ -DGGML_AVX2=ON \ -DGGML_FMA=ON \ -DGGML_F16C=ON && \ cmake --build build --config Release --target llama-server -j$(nproc) && \ echo "=== Binary dependencies ===" && \ ldd build/bin/llama-server || true16 unmodified lines# Update library cacheRUN ldconfig
# Install Python and FastAPI dependenciesRUN apt-get update && apt-get install -y --no-install-recommends \ python3 \ python3-pip \ && rm -rf /var/lib/apt/lists/*
# Install Python packagesRUN pip3 install --no-cache-dir fastapi uvicorn requests pydantic --break-system-packages
# Create non-root userRUN useradd -m -u 1000 user && \ mkdir -p /home/user/.cache/llama.cpp && \ chown -R user:user /home/user
# Copy application codeCOPY --chown=user:user app.py /home/user/app.py
USER userWORKDIR /home/user
# Set environment variablesENV HOME=/home/user \ LLAMA_CACHE=/home/user/.cache/llama.cpp \ PATH=/home/user/.local/bin:$PATH PATH=/home/user/.local/bin:$PATH \ PYTHONUNBUFFERED=1
EXPOSE 7860
# Start llama-server with HuggingFace model# Using DeepSeek LLM 7B Chat - general purpose modelCMD ["llama-server", \ "-hf", "TheBloke/deepseek-llm-7B-chat-GGUF:deepseek-llm-7b-chat.Q4_K_M.gguf", \ "--host", "0.0.0.0", \ "--port", "7860", \ "-c", "2048", \ "--metrics"]# Start FastAPI app (which manages llama-server internally)CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]123123455678678910111213141516171819202122232425262728293012131415161718192021222333343536373839404142434424252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118484950515253119120121122123124575859125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179from fastapi import FastAPIfrom llama_cpp import Llamafrom huggingface_hub import hf_hub_downloadfrom fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModelimport subprocessimport signalimport os
# GGUF model configurationREPO_ID = "TheBloke/deepseek-coder-6.7B-instruct-GGUF"FILENAME = "deepseek-coder-6.7b-instruct.Q4_K_M.gguf"import requestsimport timefrom typing import Optional
app = FastAPI()
# Download and cache the GGUF modelprint(f"Downloading {FILENAME} from {REPO_ID}...")model_path = hf_hub_download( repo_id=REPO_ID, filename=FILENAME, cache_dir=os.getenv("HF_HOME", "./models"))print(f"Model downloaded to: {model_path}")
# Load the model with llama-cpp-pythonprint("Loading model into memory...")llm = Llama( model_path=model_path, n_ctx=2048, # Context window n_threads=4, # CPU threads n_gpu_layers=0, # Use CPU only (set >0 if GPU available) verbose=False)print("Model loaded successfully!")# Predefined list of available modelsAVAILABLE_MODELS = { # === Financial & Summarization Models (Recommended) === "qwen-2.5-7b": "bartowski/Qwen2.5-7B-Instruct-GGUF:Qwen2.5-7B-Instruct-Q4_K_M.gguf", # Best for financial + multilingual "kimi-k2-9b": "bartowski/k2-chat-GGUF:k2-chat-Q4_K_M.gguf", # Kimi K2 - long context, good reasoning "yi-1.5-9b": "bartowski/Yi-1.5-9B-Chat-GGUF:Yi-1.5-9B-Chat-Q4_K_M.gguf", # Excellent for finance "llama-3.1-8b": "bartowski/Meta-Llama-3.1-8B-Instruct-GGUF:Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf", # Great reasoning "mistral-7b": "TheBloke/Mistral-7B-Instruct-v0.3-GGUF:mistral-7b-instruct-v0.3.Q4_K_M.gguf", # Reliable summarization
# === Coding Models === "deepseek-coder": "TheBloke/deepseek-coder-6.7B-instruct-GGUF:deepseek-coder-6.7b-instruct.Q4_K_M.gguf",
@app.post("/v1/chat/completions")def chat(req: dict): messages = req.get("messages", []) max_tokens = req.get("max_tokens", 256) temperature = req.get("temperature", 0.7)
# Use llama-cpp-python's built-in chat completion response = llm.create_chat_completion( messages=messages, max_tokens=max_tokens, temperature=temperature, stop=["</s>", "User:", "###"] # === General Purpose === "deepseek-chat": "TheBloke/deepseek-llm-7B-chat-GGUF:deepseek-llm-7b-chat.Q4_K_M.gguf", "llama-3.2-3b": "bartowski/Llama-3.2-3B-Instruct-GGUF:Llama-3.2-3B-Instruct-Q4_K_M.gguf", # Fast & lightweight}
# Global statecurrent_model = "deepseek-chat" # Default modelllama_process: Optional[subprocess.Popen] = NoneLLAMA_SERVER_PORT = 8080LLAMA_SERVER_URL = f"http://localhost:{LLAMA_SERVER_PORT}"
class ModelSwitchRequest(BaseModel): model_name: str
class ChatCompletionRequest(BaseModel): messages: list[dict] max_tokens: int = 256 temperature: float = 0.7
def start_llama_server(model_id: str) -> subprocess.Popen: """Start llama-server with specified model (optimized for speed).""" cmd = [ "llama-server", "-hf", model_id, "--host", "0.0.0.0", "--port", str(LLAMA_SERVER_PORT), "-c", "2048", # Context size "-t", "4", # CPU threads (adjust based on cores) "-ngl", "0", # GPU layers (0 for CPU-only) "--cont-batching", # Enable continuous batching for speed "-b", "512", # Batch size ]
print(f"Starting llama-server with model: {model_id}") process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid if os.name != 'nt' else None )
# Wait for server to be ready max_retries = 60 for i in range(max_retries): try: response = requests.get(f"{LLAMA_SERVER_URL}/health", timeout=1) if response.status_code == 200: print(f"llama-server ready after {i+1} seconds") return process except: time.sleep(1)
raise RuntimeError("llama-server failed to start")
def stop_llama_server(): """Stop the running llama-server.""" global llama_process if llama_process: print("Stopping llama-server...") try: if os.name != 'nt': os.killpg(os.getpgid(llama_process.pid), signal.SIGTERM) else: llama_process.terminate() llama_process.wait(timeout=10) except: if os.name != 'nt': os.killpg(os.getpgid(llama_process.pid), signal.SIGKILL) else: llama_process.kill() llama_process = None time.sleep(2) # Give it time to fully shut down
@app.on_event("startup")async def startup_event(): """Start with default model.""" global llama_process model_id = AVAILABLE_MODELS[current_model] llama_process = start_llama_server(model_id)
@app.on_event("shutdown")async def shutdown_event(): """Clean shutdown.""" stop_llama_server()
@app.get("/")async def root(): return { "choices": [{ "message": { "role": "assistant", "content": response["choices"][0]["message"]["content"] } }] "status": "DeepSeek API with dynamic model switching", "current_model": current_model, "available_models": list(AVAILABLE_MODELS.keys()) }
@app.get("/")def root(): return {"status": "DeepSeek API is online (GGUF)"}@app.get("/models")async def list_models(): """List all available models.""" return { "current_model": current_model, "available_models": list(AVAILABLE_MODELS.keys()) }
@app.post("/switch-model")async def switch_model(request: ModelSwitchRequest): """Switch to a different model.""" global current_model, llama_process
if request.model_name not in AVAILABLE_MODELS: raise HTTPException( status_code=400, detail=f"Model '{request.model_name}' not found. Available: {list(AVAILABLE_MODELS.keys())}" )
if request.model_name == current_model: return {"message": f"Already using model: {current_model}"}
# Stop current server stop_llama_server()
# Start with new model model_id = AVAILABLE_MODELS[request.model_name] llama_process = start_llama_server(model_id) current_model = request.model_name
return { "message": f"Switched to model: {current_model}", "model": current_model }
@app.post("/v1/chat/completions")async def chat_completions(request: ChatCompletionRequest): """OpenAI-compatible chat completions endpoint.""" try: # Forward to llama-server response = requests.post( f"{LLAMA_SERVER_URL}/v1/chat/completions", json={ "messages": request.messages, "max_tokens": request.max_tokens, "temperature": request.temperature, }, timeout=300 ) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: raise HTTPException(status_code=500, detail=f"llama-server error: {str(e)}")Dockerfile+22-14
app.py+167-47