Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,21 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# Load the model
|
| 17 |
-
llm = Llama(
|
| 18 |
-
model_path=MODEL_PATH,
|
| 19 |
-
n_ctx=2048,
|
| 20 |
-
n_threads=2,
|
| 21 |
-
n_batch=64,
|
| 22 |
-
use_mlock=True
|
| 23 |
)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
app = FastAPI(title="Mistral GGUF LLM API", version="1.0.0")
|
| 27 |
|
| 28 |
class InferenceRequest(BaseModel):
|
| 29 |
prompt: str
|
|
@@ -35,8 +27,8 @@ class InferenceResponse(BaseModel):
|
|
| 35 |
@app.post("/infer", response_model=InferenceResponse)
|
| 36 |
def infer(req: InferenceRequest):
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
return InferenceResponse(output=
|
| 40 |
except Exception as e:
|
| 41 |
return InferenceResponse(output=f"Error generating response: {str(e)}")
|
| 42 |
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from ctransformers import AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
# Model configuration for ctransformers (CPU-friendly)
|
| 6 |
+
MODEL_REPO_ID = "TheBloke/Mistral-7B-Instruct-v0.1-GGUF"
|
| 7 |
+
MODEL_FILE = "mistral-7b-instruct-v0.1.Q4_K_M.gguf"
|
| 8 |
+
|
| 9 |
+
# Load the model once at startup
|
| 10 |
+
llm = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
MODEL_REPO_ID,
|
| 12 |
+
model_file=MODEL_FILE,
|
| 13 |
+
model_type="mistral",
|
| 14 |
+
gpu_layers=0,
|
| 15 |
+
context_length=2048,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
+
app = FastAPI(title="Mistral GGUF LLM API (ctransformers)", version="1.0.0")
|
|
|
|
| 19 |
|
| 20 |
class InferenceRequest(BaseModel):
|
| 21 |
prompt: str
|
|
|
|
| 27 |
@app.post("/infer", response_model=InferenceResponse)
|
| 28 |
def infer(req: InferenceRequest):
|
| 29 |
try:
|
| 30 |
+
generated_text = llm(req.prompt, max_new_tokens=req.max_tokens)
|
| 31 |
+
return InferenceResponse(output=str(generated_text).strip())
|
| 32 |
except Exception as e:
|
| 33 |
return InferenceResponse(output=f"Error generating response: {str(e)}")
|
| 34 |
|