victor-johnson commited on
Commit
34b4f29
·
verified ·
1 Parent(s): fc7bcbf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI
3
+ from pydantic import BaseModel
4
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
5
+ from langchain.llms import HuggingFacePipeline
6
+ from langchain import PromptTemplate, LLMChain
7
+
8
+ # — Model setup (small enough to CPU-serve in a Space) —
9
+ MODEL_ID = "bigcode/starcoder2-1b"
10
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
11
+ model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
12
+
13
+ # wrap in a HF pipeline and LangChain LLM
14
+ pipe = pipeline(
15
+ "text-generation",
16
+ model=model,
17
+ tokenizer=tokenizer,
18
+ max_new_tokens=64,
19
+ temperature=0.2,
20
+ top_p=0.95,
21
+ do_sample=False,
22
+ )
23
+ llm = HuggingFacePipeline(pipeline=pipe)
24
+
25
+ # define a simple prompt → chain
26
+ prompt = PromptTemplate(
27
+ input_variables=["description"],
28
+ template=(
29
+ "### Convert English description to an Emmet abbreviation\n"
30
+ "Description: {description}\n"
31
+ "Emmet:"
32
+ ),
33
+ )
34
+ chain = LLMChain(llm=llm, prompt=prompt)
35
+
36
+ # FastAPI app
37
+ app = FastAPI()
38
+
39
+ class Req(BaseModel):
40
+ description: str
41
+
42
+ class Res(BaseModel):
43
+ emmet: str
44
+
45
+ @app.post("/generate-emmet", response_model=Res)
46
+ async def generate_emmet(req: Req):
47
+ raw = chain.run(req.description)
48
+ # take just the first line after the prompt
49
+ emmet = raw.strip().splitlines()[0]
50
+ return {"emmet": emmet}