Priyanshu Kumar
commited on
Commit
·
43cc365
1
Parent(s):
37a2a92
Switch to Docker SDK for FastAPI app
Browse files- DOCKERFILE +2 -4
- app.py +44 -0
DOCKERFILE
CHANGED
@@ -7,11 +7,9 @@ RUN useradd -m -u 1000 user
|
|
7 |
USER user
|
8 |
ENV PATH="/home/user/.local/bin:$PATH"
|
9 |
|
10 |
-
#app directory is ./app where the FastAPI app is located
|
11 |
-
WORKDIR /app
|
12 |
-
|
13 |
COPY --chown=user ./requirements.txt requirements.txt
|
14 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
15 |
|
16 |
-
COPY
|
|
|
17 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
7 |
USER user
|
8 |
ENV PATH="/home/user/.local/bin:$PATH"
|
9 |
|
|
|
|
|
|
|
10 |
COPY --chown=user ./requirements.txt requirements.txt
|
11 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
12 |
|
13 |
+
COPY . .
|
14 |
+
|
15 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from llama_cpp import Llama
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
import os
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# === Model Config ===
|
9 |
+
REPO_ID = "TheBloke/Mistral-7B-Instruct-v0.1-GGUF"
|
10 |
+
FILENAME = "mistral-7b-instruct-v0.1.Q4_K_M.gguf"
|
11 |
+
MODEL_DIR = "models"
|
12 |
+
MODEL_PATH = os.path.join(MODEL_DIR, FILENAME)
|
13 |
+
|
14 |
+
# === Download only if not already present ===
|
15 |
+
if not os.path.exists(MODEL_PATH):
|
16 |
+
print(f"Downloading model {FILENAME} from Hugging Face...")
|
17 |
+
model_path = hf_hub_download(
|
18 |
+
repo_id=REPO_ID,
|
19 |
+
filename=FILENAME,
|
20 |
+
cache_dir=MODEL_DIR,
|
21 |
+
local_dir=MODEL_DIR,
|
22 |
+
local_dir_use_symlinks=False
|
23 |
+
)
|
24 |
+
else:
|
25 |
+
print(f"Model already exists at: {MODEL_PATH}")
|
26 |
+
model_path = MODEL_PATH
|
27 |
+
|
28 |
+
# === Load LLM ===
|
29 |
+
llm = Llama(
|
30 |
+
model_path=model_path,
|
31 |
+
n_ctx=1024,
|
32 |
+
n_threads=4 # Adjust for your CPU
|
33 |
+
)
|
34 |
+
|
35 |
+
@app.get("/")
|
36 |
+
def root():
|
37 |
+
return {"message": "Mistral API is live!"}
|
38 |
+
|
39 |
+
@app.post("/generate")
|
40 |
+
async def generate(request: Request):
|
41 |
+
data = await request.json()
|
42 |
+
prompt = data.get("prompt", "")
|
43 |
+
response = llm(prompt, max_tokens=128, temperature=0.7)
|
44 |
+
return {"response": response["choices"][0]["text"]}
|