Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,37 @@
|
|
1 |
-
import os
|
2 |
from fastapi import FastAPI
|
3 |
from pydantic import BaseModel
|
4 |
-
from transformers import
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
# β Model setup
|
9 |
MODEL_ID = "bigcode/starcoder2-3b"
|
10 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID,trust_remote_code=True)
|
11 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_ID,trust_remote_code=True)
|
12 |
|
13 |
-
#
|
14 |
-
|
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 |
-
#
|
26 |
prompt = PromptTemplate(
|
27 |
input_variables=["description"],
|
28 |
template=(
|
@@ -33,7 +42,7 @@ prompt = PromptTemplate(
|
|
33 |
)
|
34 |
chain = LLMChain(llm=llm, prompt=prompt)
|
35 |
|
36 |
-
# FastAPI app
|
37 |
app = FastAPI()
|
38 |
|
39 |
class Req(BaseModel):
|
@@ -44,7 +53,6 @@ class Res(BaseModel):
|
|
44 |
|
45 |
@app.post("/generate-emmet", response_model=Res)
|
46 |
async def generate_emmet(req: Req):
|
47 |
-
raw = chain.
|
48 |
-
# take just the first line after the prompt
|
49 |
emmet = raw.strip().splitlines()[0]
|
50 |
return {"emmet": emmet}
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
3 |
+
from transformers import (
|
4 |
+
pipeline,
|
5 |
+
AutoTokenizer,
|
6 |
+
AutoModelForCausalLM,
|
7 |
+
GenerationConfig,
|
8 |
+
)
|
9 |
+
from langchain_huggingface import HuggingFacePipeline
|
10 |
+
from langchain_core.prompts import PromptTemplate
|
11 |
+
from langchain.chains import LLMChain
|
12 |
|
13 |
+
# β Model setup β
|
14 |
MODEL_ID = "bigcode/starcoder2-3b"
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
16 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True)
|
17 |
|
18 |
+
# β Generation config & pipeline (new API) β
|
19 |
+
gen_config = GenerationConfig(
|
|
|
|
|
|
|
20 |
max_new_tokens=64,
|
21 |
temperature=0.2,
|
22 |
top_p=0.95,
|
23 |
do_sample=False,
|
24 |
)
|
25 |
+
pipe = pipeline(
|
26 |
+
"text-generation",
|
27 |
+
model=model,
|
28 |
+
tokenizer=tokenizer,
|
29 |
+
device_map="auto",
|
30 |
+
generation_config=gen_config,
|
31 |
+
)
|
32 |
llm = HuggingFacePipeline(pipeline=pipe)
|
33 |
|
34 |
+
# β Prompt & chain β
|
35 |
prompt = PromptTemplate(
|
36 |
input_variables=["description"],
|
37 |
template=(
|
|
|
42 |
)
|
43 |
chain = LLMChain(llm=llm, prompt=prompt)
|
44 |
|
45 |
+
# β FastAPI app β
|
46 |
app = FastAPI()
|
47 |
|
48 |
class Req(BaseModel):
|
|
|
53 |
|
54 |
@app.post("/generate-emmet", response_model=Res)
|
55 |
async def generate_emmet(req: Req):
|
56 |
+
raw = chain.invoke(req.description) # use .invoke() instead of deprecated .run()
|
|
|
57 |
emmet = raw.strip().splitlines()[0]
|
58 |
return {"emmet": emmet}
|