Spaces:
Runtime error
Runtime error
Use custom app
Browse files- Dockerfile +2 -2
- app.py +73 -0
- requirements.txt +3 -1
Dockerfile
CHANGED
|
@@ -17,6 +17,6 @@ RUN wget -q -O model.gguf "https://huggingface.co/dicta-il/dictalm2.0-instruct-G
|
|
| 17 |
|
| 18 |
COPY --chown=user . .
|
| 19 |
|
| 20 |
-
# Run
|
| 21 |
# Port 7860 is required for HF Spaces
|
| 22 |
-
CMD ["
|
|
|
|
| 17 |
|
| 18 |
COPY --chown=user . .
|
| 19 |
|
| 20 |
+
# Run FastAPI server
|
| 21 |
# Port 7860 is required for HF Spaces
|
| 22 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
+
import uvicorn
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
print("Loading Llama model...")
|
| 8 |
+
llm = Llama(
|
| 9 |
+
model_path="model.gguf",
|
| 10 |
+
n_ctx=4096,
|
| 11 |
+
n_gpu_layers=0,
|
| 12 |
+
)
|
| 13 |
+
print("Model loaded successfully!")
|
| 14 |
+
|
| 15 |
+
@app.post("/v1/chat/completions")
|
| 16 |
+
async def chat_completions(request: Request):
|
| 17 |
+
try:
|
| 18 |
+
data = await request.json()
|
| 19 |
+
except:
|
| 20 |
+
raise HTTPException(status_code=400, detail="Invalid JSON body")
|
| 21 |
+
|
| 22 |
+
messages = data.get("messages", [])
|
| 23 |
+
max_tokens = data.get("max_tokens", 1024)
|
| 24 |
+
temperature = data.get("temperature", 0.7)
|
| 25 |
+
|
| 26 |
+
# Construct Dicta-LM Prompt
|
| 27 |
+
prompt = "<s>"
|
| 28 |
+
for m in messages:
|
| 29 |
+
if m["role"] == "user":
|
| 30 |
+
prompt += f"[INST] {m['content']} [/INST]\n"
|
| 31 |
+
elif m["role"] == "assistant":
|
| 32 |
+
prompt += f"{m['content']}</s> "
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
response = llm(
|
| 36 |
+
prompt=prompt,
|
| 37 |
+
max_tokens=max_tokens,
|
| 38 |
+
temperature=temperature,
|
| 39 |
+
stop=["</s>", "[INST]", "[/INST]"],
|
| 40 |
+
echo=False
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
return {
|
| 44 |
+
"id": response["id"],
|
| 45 |
+
"object": "chat.completion",
|
| 46 |
+
"created": response["created"],
|
| 47 |
+
"model": "dictalm2.0-instruct",
|
| 48 |
+
"choices": [
|
| 49 |
+
{
|
| 50 |
+
"index": 0,
|
| 51 |
+
"message": {
|
| 52 |
+
"role": "assistant",
|
| 53 |
+
"content": response["choices"][0]["text"].strip()
|
| 54 |
+
},
|
| 55 |
+
"finish_reason": response["choices"][0]["finish_reason"]
|
| 56 |
+
}
|
| 57 |
+
],
|
| 58 |
+
"usage": response["usage"]
|
| 59 |
+
}
|
| 60 |
+
except Exception as e:
|
| 61 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 62 |
+
|
| 63 |
+
@app.get("/v1/models")
|
| 64 |
+
def get_models():
|
| 65 |
+
return {
|
| 66 |
+
"data": [
|
| 67 |
+
{"id": "dictalm2.0-instruct", "object": "model", "owned_by": "dicta"}
|
| 68 |
+
]
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
@app.get("/")
|
| 72 |
+
def health_check():
|
| 73 |
+
return {"status": "running"}
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
-
llama-cpp-python
|
| 2 |
huggingface_hub
|
| 3 |
jinja2
|
|
|
|
|
|
|
|
|
| 1 |
+
llama-cpp-python
|
| 2 |
huggingface_hub
|
| 3 |
jinja2
|
| 4 |
+
fastapi
|
| 5 |
+
uvicorn
|