Upload 2 files
Browse files- app.py +29 -2
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import uuid
|
|
| 7 |
from typing import Any, Callable, Dict, List, Optional, Union
|
| 8 |
|
| 9 |
from fastapi import Depends, FastAPI, Header, HTTPException
|
|
|
|
| 10 |
from fastapi.responses import StreamingResponse
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
from pydantic import BaseModel, Field
|
|
@@ -37,6 +38,7 @@ FLASH_ATTN = _env_bool("FLASH_ATTN", True)
|
|
| 37 |
USE_MMAP = _env_bool("USE_MMAP", True)
|
| 38 |
USE_MLOCK = _env_bool("USE_MLOCK", False)
|
| 39 |
NUMA = _env_bool("NUMA", False)
|
|
|
|
| 40 |
ENABLE_BENCH = _env_bool("ENABLE_BENCH", False)
|
| 41 |
ENABLE_TOOL_CALLS = _env_bool("ENABLE_TOOL_CALLS", True)
|
| 42 |
ENABLE_TOOL_HEURISTICS = _env_bool("ENABLE_TOOL_HEURISTICS", True)
|
|
@@ -50,6 +52,14 @@ logging.basicConfig(level=logging.INFO)
|
|
| 50 |
|
| 51 |
app = FastAPI(title="Qwen GGUF OpenAI-Compatible API")
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def _load_model():
|
| 55 |
import llama_cpp
|
|
@@ -77,7 +87,7 @@ def _load_model():
|
|
| 77 |
"numa": NUMA,
|
| 78 |
"n_gpu_layers": 0, # CPU-only
|
| 79 |
"flash_attn": FLASH_ATTN,
|
| 80 |
-
"verbose":
|
| 81 |
}
|
| 82 |
if n_threads_batch is not None:
|
| 83 |
kwargs["n_threads_batch"] = n_threads_batch
|
|
@@ -86,7 +96,24 @@ def _load_model():
|
|
| 86 |
if n_ubatch is not None:
|
| 87 |
kwargs["n_ubatch"] = n_ubatch
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
logger.info("Model loaded successfully!")
|
| 91 |
return llm
|
| 92 |
|
|
|
|
| 7 |
from typing import Any, Callable, Dict, List, Optional, Union
|
| 8 |
|
| 9 |
from fastapi import Depends, FastAPI, Header, HTTPException
|
| 10 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
from fastapi.responses import StreamingResponse
|
| 12 |
from huggingface_hub import hf_hub_download
|
| 13 |
from pydantic import BaseModel, Field
|
|
|
|
| 38 |
USE_MMAP = _env_bool("USE_MMAP", True)
|
| 39 |
USE_MLOCK = _env_bool("USE_MLOCK", False)
|
| 40 |
NUMA = _env_bool("NUMA", False)
|
| 41 |
+
LLAMA_VERBOSE = _env_bool("LLAMA_VERBOSE", True)
|
| 42 |
ENABLE_BENCH = _env_bool("ENABLE_BENCH", False)
|
| 43 |
ENABLE_TOOL_CALLS = _env_bool("ENABLE_TOOL_CALLS", True)
|
| 44 |
ENABLE_TOOL_HEURISTICS = _env_bool("ENABLE_TOOL_HEURISTICS", True)
|
|
|
|
| 52 |
|
| 53 |
app = FastAPI(title="Qwen GGUF OpenAI-Compatible API")
|
| 54 |
|
| 55 |
+
app.add_middleware(
|
| 56 |
+
CORSMiddleware,
|
| 57 |
+
allow_origins=["*"],
|
| 58 |
+
allow_credentials=True,
|
| 59 |
+
allow_methods=["*"],
|
| 60 |
+
allow_headers=["*"],
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
|
| 64 |
def _load_model():
|
| 65 |
import llama_cpp
|
|
|
|
| 87 |
"numa": NUMA,
|
| 88 |
"n_gpu_layers": 0, # CPU-only
|
| 89 |
"flash_attn": FLASH_ATTN,
|
| 90 |
+
"verbose": LLAMA_VERBOSE,
|
| 91 |
}
|
| 92 |
if n_threads_batch is not None:
|
| 93 |
kwargs["n_threads_batch"] = n_threads_batch
|
|
|
|
| 96 |
if n_ubatch is not None:
|
| 97 |
kwargs["n_ubatch"] = n_ubatch
|
| 98 |
|
| 99 |
+
logger.info(
|
| 100 |
+
"Loading model with n_ctx=%s n_threads=%s flash_attn=%s use_mmap=%s use_mlock=%s numa=%s n_batch=%s n_ubatch=%s n_threads_batch=%s verbose=%s",
|
| 101 |
+
kwargs["n_ctx"],
|
| 102 |
+
kwargs["n_threads"],
|
| 103 |
+
kwargs["flash_attn"],
|
| 104 |
+
kwargs["use_mmap"],
|
| 105 |
+
kwargs["use_mlock"],
|
| 106 |
+
kwargs["numa"],
|
| 107 |
+
kwargs.get("n_batch"),
|
| 108 |
+
kwargs.get("n_ubatch"),
|
| 109 |
+
kwargs.get("n_threads_batch"),
|
| 110 |
+
kwargs["verbose"],
|
| 111 |
+
)
|
| 112 |
+
try:
|
| 113 |
+
llm = Llama(**kwargs)
|
| 114 |
+
except Exception:
|
| 115 |
+
logger.exception("llama.cpp failed to load %s from repo %s", GGUF_FILE, GGUF_REPO)
|
| 116 |
+
raise
|
| 117 |
logger.info("Model loaded successfully!")
|
| 118 |
return llm
|
| 119 |
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
https://huggingface.co/d-ragon/
|
| 2 |
fastapi
|
| 3 |
uvicorn
|
| 4 |
pydantic
|
|
|
|
| 1 |
+
https://huggingface.co/d-ragon/lcpp4/resolve/main/llama_cpp_python-0.3.20-py3-none-linux_x86_64.whl?download=1
|
| 2 |
fastapi
|
| 3 |
uvicorn
|
| 4 |
pydantic
|