Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +14 -0
- app.py +58 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
|
| 6 |
+
|
| 7 |
+
COPY requirements.txt .
|
| 8 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
+
|
| 10 |
+
COPY app.py .
|
| 11 |
+
|
| 12 |
+
EXPOSE 7860
|
| 13 |
+
|
| 14 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Header, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# MODEL
|
| 10 |
+
# -----------------------------
|
| 11 |
+
MODEL_NAME = "lfm-research/LFM-2.5-1.2B-Instruct"
|
| 12 |
+
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 14 |
+
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
MODEL_NAME,
|
| 17 |
+
device_map="auto",
|
| 18 |
+
torch_dtype=torch.float16
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# -----------------------------
|
| 22 |
+
# YOUR CUSTOM API KEY
|
| 23 |
+
# -----------------------------
|
| 24 |
+
API_KEY = "Model12134-1344"
|
| 25 |
+
|
| 26 |
+
class Prompt(BaseModel):
|
| 27 |
+
message: str
|
| 28 |
+
|
| 29 |
+
@app.post("/chat")
|
| 30 |
+
def chat(
|
| 31 |
+
prompt: Prompt,
|
| 32 |
+
x_api_key: str = Header(None)
|
| 33 |
+
):
|
| 34 |
+
# ---- API KEY VALIDATION ----
|
| 35 |
+
if not x_api_key:
|
| 36 |
+
raise HTTPException(status_code=401, detail="API key missing")
|
| 37 |
+
|
| 38 |
+
if x_api_key != API_KEY:
|
| 39 |
+
raise HTTPException(status_code=403, detail="Invalid API key")
|
| 40 |
+
|
| 41 |
+
inputs = tokenizer(
|
| 42 |
+
prompt.message,
|
| 43 |
+
return_tensors="pt"
|
| 44 |
+
).to(model.device)
|
| 45 |
+
|
| 46 |
+
outputs = model.generate(
|
| 47 |
+
**inputs,
|
| 48 |
+
max_new_tokens=256,
|
| 49 |
+
temperature=0.3,
|
| 50 |
+
do_sample=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
response = tokenizer.decode(
|
| 54 |
+
outputs[0][inputs["input_ids"].shape[-1]:],
|
| 55 |
+
skip_special_tokens=True
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
return {"response": response}
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
accelerate
|
| 6 |
+
sentencepiece
|