File size: 1,370 Bytes
93952a4
 
 
2fc13f6
93952a4
 
2fc13f6
 
93952a4
 
 
 
2fc13f6
 
93952a4
 
 
 
 
 
 
2fc13f6
 
93952a4
 
 
 
 
2fc13f6
93952a4
2fc13f6
93952a4
 
2fc13f6
93952a4
 
 
 
 
 
2fc13f6
 
93952a4
 
 
 
2fc13f6
93952a4
2fc13f6
93952a4
 
 
 
2fc13f6
 
 
93952a4
 
 
 
1
2
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
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline
import torch

app = FastAPI(title="Micro Tier LLM API")

MODELS = {
    "qwen-0.5b": "Qwen/Qwen2.5-0.5B-Instruct",
    "llama-1b": "meta-llama/Llama-3.2-1B-Instruct",
    "qwen-1b": "Qwen/Qwen2.5-1B-Instruct",
    "gemma-1b": "google/gemma-3-1b-it"
}

loaded_models = {}

class GenerateRequest(BaseModel):
    model: str
    prompt: str
    max_new_tokens: int = 256
    temperature: float = 0.7


def get_pipeline(model_name):
    if model_name not in loaded_models:
        loaded_models[model_name] = pipeline(
            "text-generation",
            model=MODELS[model_name],
            torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
            device_map="auto"
        )
    return loaded_models[model_name]


@app.get("/")
def root():
    return {
        "status": "running",
        "models": list(MODELS.keys())
    }


@app.post("/generate")
def generate(req: GenerateRequest):
    if req.model not in MODELS:
        return {"error": "Unknown model"}

    pipe = get_pipeline(req.model)

    output = pipe(
        req.prompt,
        max_new_tokens=req.max_new_tokens,
        temperature=req.temperature,
        do_sample=True,
    )

    return {
        "model": req.model,
        "response": output[0]["generated_text"]
    }