File size: 1,433 Bytes
34b4f29
 
4c456b6
 
 
 
 
 
 
 
34b4f29
4c456b6
ee67cd5
4c456b6
 
34b4f29
253aa25
4c456b6
 
 
 
 
253aa25
 
 
 
4c456b6
34b4f29
 
4c456b6
34b4f29
 
 
 
 
 
 
 
 
 
4c456b6
34b4f29
 
 
 
 
 
 
 
 
 
4c456b6
34b4f29
1643b9a
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
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import (
    pipeline,
    AutoTokenizer,
    AutoModelForCausalLM,
)
from langchain_huggingface import HuggingFacePipeline
from langchain_core.prompts import PromptTemplate
from langchain.chains import LLMChain

# β€” Model setup β€”
MODEL_ID = "bigcode/starcoder2-3b"
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True)

# β€” Pipeline setup (pass generation parameters directly) β€”
pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    device_map="auto",
    max_new_tokens=64,
    temperature=0.2,
    top_p=0.95,
    do_sample=False,
)
llm = HuggingFacePipeline(pipeline=pipe)

# β€” Prompt & chain β€”
prompt = PromptTemplate(
    input_variables=["description"],
    template=(
        "### Convert English description to an Emmet abbreviation\n"
        "Description: {description}\n"
        "Emmet:"
    ),
)
chain = LLMChain(llm=llm, prompt=prompt)

# β€” FastAPI app β€”
app = FastAPI()

class Req(BaseModel):
    description: str

class Res(BaseModel):
    emmet: str

@app.post("/generate-emmet", response_model=Res)
async def generate_emmet(req: Req):
    raw = chain.invoke(req.description)  # use .invoke() instead of deprecated .run()
    emmet = raw.strip().splitlines()[0]
    return {"emmet": emmet}