Spaces:
Sleeping
Sleeping
daniellefranca96
commited on
Commit
•
c51a031
1
Parent(s):
e53fe12
Update main.py
Browse files
main.py
CHANGED
@@ -4,34 +4,64 @@ import requests
|
|
4 |
from ctransformers import AutoModelForCausalLM
|
5 |
|
6 |
llms = {
|
7 |
-
"
|
8 |
-
"
|
9 |
-
"
|
10 |
-
"
|
11 |
-
"
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
}
|
15 |
|
16 |
-
for k in llms.keys():
|
17 |
-
AutoModelForCausalLM.from_pretrained(llms[k]['name'], model_file=llms[k]['file'])
|
18 |
-
|
19 |
#Pydantic object
|
20 |
class validation(BaseModel):
|
21 |
prompt: str
|
22 |
llm: str
|
|
|
23 |
#Fast API
|
24 |
app = FastAPI()
|
25 |
|
26 |
@app.post("/llm_on_cpu")
|
27 |
async def stream(item: validation):
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
user="""
|
32 |
{prompt}"""
|
33 |
|
34 |
-
|
35 |
|
36 |
prompt = f"{prefix}{user.replace('{prompt}', item.prompt)}{suffix}"
|
37 |
-
return llm(prompt)
|
|
|
4 |
from ctransformers import AutoModelForCausalLM
|
5 |
|
6 |
llms = {
|
7 |
+
"TinyLLama 1b 4_K_M 2048": {
|
8 |
+
"nctx": 2048,
|
9 |
+
"file": "tinyllama-1.1b-chat-v0.3.Q4_K_M.gguf",
|
10 |
+
"prefix": "### Human:",
|
11 |
+
"suffix": "### Assistant:"
|
12 |
+
},
|
13 |
+
"TinyLLama 1b OpenOrca 4_K_M 2048": {
|
14 |
+
"nctx": 2048,
|
15 |
+
"file": "tinyllama-1.1b-1t-openorca.Q4_K_M.gguf",
|
16 |
+
"prefix": "<|im_start|>system You are a helpfull assistant<|im_end|><|im_start|>user",
|
17 |
+
"suffix": "<|im_end|><|im_start|>assistant"
|
18 |
+
},
|
19 |
+
"OpenLLama 3b 4_K_M 196k": {
|
20 |
+
"nctx": 80000,
|
21 |
+
"file": "open-llama-3b-v2-wizard-evol-instuct-v2-196k.Q4_K_M.gguf",
|
22 |
+
"prefix": "### HUMAN:",
|
23 |
+
"suffix": "### RESPONSE:"
|
24 |
+
},
|
25 |
+
"Phi-2 2.7b 4_K_M 2048": {
|
26 |
+
"nctx": 2048,
|
27 |
+
"file": "phi-2.Q4_K_M.gguf",
|
28 |
+
"prefix": "Instruct:",
|
29 |
+
"suffix": "Output:"
|
30 |
+
},
|
31 |
+
"Mixtral MOE 7bx2 4_K_M 32K": {
|
32 |
+
"nctx": 32000,
|
33 |
+
"file": "mixtral_7bx2_moe.Q4_K_M.gguf",
|
34 |
+
"prefix": "",
|
35 |
+
"suffix": ""
|
36 |
+
},
|
37 |
+
"Stable Zephyr 3b 4_K_M 4096": {
|
38 |
+
"nctx": 4096,
|
39 |
+
"file": "stablelm-zephyr-3b.Q4_K_M.gguf",
|
40 |
+
"prefix": "<|user|>",
|
41 |
+
"suffix": "<|endoftext|><|assistant|>"
|
42 |
+
}
|
43 |
}
|
44 |
|
|
|
|
|
|
|
45 |
#Pydantic object
|
46 |
class validation(BaseModel):
|
47 |
prompt: str
|
48 |
llm: str
|
49 |
+
|
50 |
#Fast API
|
51 |
app = FastAPI()
|
52 |
|
53 |
@app.post("/llm_on_cpu")
|
54 |
async def stream(item: validation):
|
55 |
|
56 |
+
model = llms[item.llm]
|
57 |
+
prefix=model['prefix']
|
58 |
+
suffix=model['suffix']
|
59 |
+
nctx = model['nctx'] if 'nctx' in item.keys() else 1024
|
60 |
+
max_tokens = model['max_tokens'] if 'max_tokens' in item.keys() else 512
|
61 |
user="""
|
62 |
{prompt}"""
|
63 |
|
64 |
+
model = Llama(model_path="./"+model['file'], n_ctx=model['nctx'], verbose=False, n_threads=8)
|
65 |
|
66 |
prompt = f"{prefix}{user.replace('{prompt}', item.prompt)}{suffix}"
|
67 |
+
return llm(prompt, max_tokens=max_tokens)
|