Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +12 -16
- README.md +97 -135
- app.py +115 -100
- requirements.txt +6 -6
Dockerfile
CHANGED
|
@@ -1,16 +1,12 @@
|
|
| 1 |
-
FROM python:3.
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
EXPOSE 7860
|
| 15 |
-
|
| 16 |
-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt ./
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
|
@@ -1,135 +1,97 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
-
|
| 19 |
-
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
-
|
| 29 |
-
-
|
| 30 |
-
-
|
| 31 |
-
-
|
| 32 |
-
|
| 33 |
-
##
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
-
|
| 48 |
-
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
``
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
## Run locally (with Docker)
|
| 100 |
-
|
| 101 |
-
Build image:
|
| 102 |
-
|
| 103 |
-
```bash
|
| 104 |
-
docker build -t hf-llm-runner .
|
| 105 |
-
```
|
| 106 |
-
|
| 107 |
-
Run container:
|
| 108 |
-
|
| 109 |
-
```bash
|
| 110 |
-
docker run --rm -p 7860:7860 hf-llm-runner
|
| 111 |
-
```
|
| 112 |
-
|
| 113 |
-
## Create and deploy on Hugging Face Spaces (Docker)
|
| 114 |
-
|
| 115 |
-
1. Go to Hugging Face -> Spaces -> **Create new Space**.
|
| 116 |
-
2. Set:
|
| 117 |
-
- Owner: your account/org
|
| 118 |
-
- Space name: your choice
|
| 119 |
-
- License: your choice
|
| 120 |
-
- SDK: **Docker**
|
| 121 |
-
- Visibility: Public (or Private if your plan supports it)
|
| 122 |
-
3. Create the Space.
|
| 123 |
-
4. Upload/push these files into the Space repo root:
|
| 124 |
-
- `app.py`
|
| 125 |
-
- `requirements.txt`
|
| 126 |
-
- `Dockerfile`
|
| 127 |
-
- `README.md`
|
| 128 |
-
5. Wait for build to finish. First startup may be slow because model weights download.
|
| 129 |
-
6. Test:
|
| 130 |
-
- `GET https://<your-space-name>.hf.space/health`
|
| 131 |
-
- `POST https://<your-space-name>.hf.space/generate`
|
| 132 |
-
|
| 133 |
-
Use your Space URL from backend:
|
| 134 |
-
|
| 135 |
-
`https://<your-space-name>.hf.space/generate`
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Streaming LLM API
|
| 3 |
+
colorFrom: blue
|
| 4 |
+
colorTo: green
|
| 5 |
+
sdk: docker
|
| 6 |
+
app_port: 7860
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
# Hugging Face Space Streaming LLM Inference API
|
| 10 |
+
|
| 11 |
+
A lightweight Hugging Face Space API server for real-time token streaming with **Qwen2.5-0.5B-Instruct**.
|
| 12 |
+
|
| 13 |
+
## Features
|
| 14 |
+
|
| 15 |
+
- FastAPI server with SSE streaming endpoint
|
| 16 |
+
- One-time model/tokenizer loading during startup
|
| 17 |
+
- Configurable generation parameters (`max_tokens`, `temperature`, `top_p`)
|
| 18 |
+
- Efficient inference with `torch.no_grad()` and `device_map="auto"`
|
| 19 |
+
- Request validation and clear error responses
|
| 20 |
+
|
| 21 |
+
## Model
|
| 22 |
+
|
| 23 |
+
- **Primary model:** `Qwen/Qwen2.5-0.5B-Instruct`
|
| 24 |
+
- Automatically downloaded from Hugging Face at startup
|
| 25 |
+
|
| 26 |
+
## File Structure
|
| 27 |
+
|
| 28 |
+
- `app.py`
|
| 29 |
+
- `requirements.txt`
|
| 30 |
+
- `README.md`
|
| 31 |
+
- `Dockerfile`
|
| 32 |
+
|
| 33 |
+
## Requirements
|
| 34 |
+
|
| 35 |
+
```txt
|
| 36 |
+
transformers
|
| 37 |
+
accelerate
|
| 38 |
+
torch
|
| 39 |
+
fastapi
|
| 40 |
+
uvicorn
|
| 41 |
+
pydantic
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
## Run Locally
|
| 45 |
+
|
| 46 |
+
```bash
|
| 47 |
+
pip install -r requirements.txt
|
| 48 |
+
uvicorn app:app --host 0.0.0.0 --port 7860
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
## API
|
| 52 |
+
|
| 53 |
+
### `POST /generate_stream`
|
| 54 |
+
|
| 55 |
+
Request JSON:
|
| 56 |
+
|
| 57 |
+
```json
|
| 58 |
+
{
|
| 59 |
+
"prompt": "user prompt text",
|
| 60 |
+
"max_tokens": 512,
|
| 61 |
+
"temperature": 0.7,
|
| 62 |
+
"top_p": 0.9
|
| 63 |
+
}
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
- `prompt` is required and must not be empty.
|
| 67 |
+
- `max_tokens`, `temperature`, and `top_p` are optional.
|
| 68 |
+
|
| 69 |
+
Response:
|
| 70 |
+
|
| 71 |
+
- Content type: `text/event-stream`
|
| 72 |
+
- Streams generated text chunks incrementally as SSE events.
|
| 73 |
+
|
| 74 |
+
## Example cURL
|
| 75 |
+
|
| 76 |
+
```bash
|
| 77 |
+
curl -N -X POST "https://your-space-name.hf.space/generate_stream" \
|
| 78 |
+
-H "Content-Type: application/json" \
|
| 79 |
+
-d '{"prompt":"Explain artificial intelligence"}'
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
## Backend Integration Flow
|
| 83 |
+
|
| 84 |
+
1. Backend sends prompt to Hugging Face Space.
|
| 85 |
+
2. Space generates and streams tokens.
|
| 86 |
+
3. Backend relays streamed tokens to client in real time.
|
| 87 |
+
|
| 88 |
+
## Hugging Face Space Setup
|
| 89 |
+
|
| 90 |
+
- Space SDK: **Docker**
|
| 91 |
+
- Ensure app starts with `uvicorn app:app --host 0.0.0.0 --port 7860`
|
| 92 |
+
- Expose port `7860`
|
| 93 |
+
|
| 94 |
+
## Notes
|
| 95 |
+
|
| 96 |
+
- The first startup may take longer due to model download.
|
| 97 |
+
- Keep model loading in startup lifecycle so it is initialized once.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,100 +1,115 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
if
|
| 66 |
-
raise HTTPException(status_code=
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import threading
|
| 3 |
+
from contextlib import asynccontextmanager
|
| 4 |
+
from typing import AsyncGenerator
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
from fastapi import FastAPI, HTTPException
|
| 8 |
+
from fastapi.responses import StreamingResponse
|
| 9 |
+
from pydantic import BaseModel, Field, field_validator
|
| 10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 11 |
+
|
| 12 |
+
MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 13 |
+
|
| 14 |
+
# Loaded once at startup.
|
| 15 |
+
tokenizer = None
|
| 16 |
+
model = None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class GenerateRequest(BaseModel):
|
| 20 |
+
prompt: str = Field(..., min_length=1, description="Input prompt text")
|
| 21 |
+
max_tokens: int = Field(default=512, ge=1, le=2048)
|
| 22 |
+
temperature: float = Field(default=0.7, ge=0.0, le=2.0)
|
| 23 |
+
top_p: float = Field(default=0.9, gt=0.0, le=1.0)
|
| 24 |
+
|
| 25 |
+
@field_validator("prompt")
|
| 26 |
+
@classmethod
|
| 27 |
+
def prompt_must_not_be_blank(cls, value: str) -> str:
|
| 28 |
+
if not value.strip():
|
| 29 |
+
raise ValueError("Prompt cannot be empty or whitespace")
|
| 30 |
+
return value
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@asynccontextmanager
|
| 34 |
+
async def lifespan(_: FastAPI):
|
| 35 |
+
global tokenizer, model
|
| 36 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 37 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 38 |
+
MODEL_ID,
|
| 39 |
+
torch_dtype="auto",
|
| 40 |
+
device_map="auto",
|
| 41 |
+
trust_remote_code=True,
|
| 42 |
+
)
|
| 43 |
+
model.eval()
|
| 44 |
+
yield
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
app = FastAPI(
|
| 48 |
+
title="Hugging Face Space Streaming LLM Inference API",
|
| 49 |
+
description="Streaming token generation API using Qwen2.5-0.5B-Instruct",
|
| 50 |
+
version="1.0.0",
|
| 51 |
+
lifespan=lifespan,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@app.get("/")
|
| 56 |
+
async def health() -> dict:
|
| 57 |
+
return {
|
| 58 |
+
"status": "ok",
|
| 59 |
+
"model": MODEL_ID,
|
| 60 |
+
"endpoints": ["POST /generate_stream"],
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
async def stream_generate(req: GenerateRequest) -> AsyncGenerator[str, None]:
|
| 65 |
+
if model is None or tokenizer is None:
|
| 66 |
+
raise HTTPException(status_code=503, detail="Model is still loading")
|
| 67 |
+
|
| 68 |
+
inputs = tokenizer(req.prompt, return_tensors="pt")
|
| 69 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 70 |
+
|
| 71 |
+
streamer = TextIteratorStreamer(
|
| 72 |
+
tokenizer,
|
| 73 |
+
skip_prompt=True,
|
| 74 |
+
skip_special_tokens=True,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
generation_kwargs = {
|
| 78 |
+
**inputs,
|
| 79 |
+
"streamer": streamer,
|
| 80 |
+
"max_new_tokens": req.max_tokens,
|
| 81 |
+
"do_sample": req.temperature > 0,
|
| 82 |
+
"temperature": req.temperature if req.temperature > 0 else None,
|
| 83 |
+
"top_p": req.top_p,
|
| 84 |
+
"pad_token_id": tokenizer.eos_token_id,
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
def run_generation() -> None:
|
| 88 |
+
with torch.no_grad():
|
| 89 |
+
model.generate(**generation_kwargs)
|
| 90 |
+
|
| 91 |
+
thread = threading.Thread(target=run_generation, daemon=True)
|
| 92 |
+
thread.start()
|
| 93 |
+
|
| 94 |
+
for text in streamer:
|
| 95 |
+
# SSE format: each event line starts with "data:"
|
| 96 |
+
yield f"data: {text}\n\n"
|
| 97 |
+
await asyncio.sleep(0)
|
| 98 |
+
|
| 99 |
+
yield "data: [DONE]\n\n"
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
@app.post("/generate_stream")
|
| 103 |
+
async def generate_stream(req: GenerateRequest):
|
| 104 |
+
try:
|
| 105 |
+
return StreamingResponse(stream_generate(req), media_type="text/event-stream")
|
| 106 |
+
except HTTPException:
|
| 107 |
+
raise
|
| 108 |
+
except Exception as exc: # pragma: no cover
|
| 109 |
+
raise HTTPException(status_code=500, detail=f"Generation error: {str(exc)}") from exc
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
import uvicorn
|
| 114 |
+
|
| 115 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
transformers
|
| 2 |
-
accelerate
|
| 3 |
-
torch
|
| 4 |
-
fastapi
|
| 5 |
-
uvicorn
|
| 6 |
-
pydantic
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
accelerate
|
| 3 |
+
torch
|
| 4 |
+
fastapi
|
| 5 |
+
uvicorn
|
| 6 |
+
pydantic
|