Spaces:
Paused
Paused
Update endpoints.py
Browse files- endpoints.py +57 -32
endpoints.py
CHANGED
|
@@ -17,60 +17,85 @@ app.add_middleware(
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
allow_credentials=True,
|
| 19 |
)
|
| 20 |
-
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-v0.1"
|
| 21 |
-
headers = {"Authorization": f"Bearer {key}"}
|
| 22 |
-
|
| 23 |
-
def query(payload):
|
| 24 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 25 |
-
return response.json()
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
# tokenizer = AutoTokenizer.from_pretrained("WizardLM/WizardCoder-1B-V1.0")
|
| 34 |
# base_model = AutoModelForCausalLM.from_pretrained("WizardLM/WizardCoder-1B-V1.0")
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
do_sample=True,
|
| 43 |
-
top_p=0.95,
|
| 44 |
-
repetition_penalty=1.2,
|
| 45 |
-
)
|
| 46 |
# hf_llm = HuggingFacePipeline(pipeline=pipe)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
@app.get("/")
|
| 50 |
def root():
|
| 51 |
return {"message": "R&D LLM API"}
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
-
async def askLLM(prompt):
|
| 60 |
-
output = pipe(prompt,do_sample=False)
|
| 61 |
-
return output
|
| 62 |
|
| 63 |
@app.post("/ask_llm")
|
| 64 |
-
async def ask_llm_endpoint(prompt: str):
|
| 65 |
-
|
| 66 |
-
result = pipe(prompt,do_sample=False)
|
| 67 |
return {"result": result}
|
| 68 |
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
from langchain.llms import OpenAI
|
| 76 |
|
|
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
allow_credentials=True,
|
| 19 |
)
|
| 20 |
+
# API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-v0.1"
|
| 21 |
+
# headers = {"Authorization": f"Bearer {key}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# def query(payload):
|
| 24 |
+
# response = requests.post(API_URL, headers=headers, json=payload)
|
| 25 |
+
# return response.json()
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
+
def LLM(llm_name, length):
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(llm_name)
|
| 32 |
+
model = AutoModelForCausalLM.from_pretrained(llm_name)
|
| 33 |
+
pipe = pipeline("text-generation",
|
| 34 |
+
model=model,
|
| 35 |
+
tokenizer=tokenizer,
|
| 36 |
+
max_length=length,
|
| 37 |
+
do_sample=True,
|
| 38 |
+
top_p=0.95,
|
| 39 |
+
repetition_penalty=1.2,
|
| 40 |
+
)
|
| 41 |
+
return pipe
|
| 42 |
|
| 43 |
|
| 44 |
# tokenizer = AutoTokenizer.from_pretrained("WizardLM/WizardCoder-1B-V1.0")
|
| 45 |
# base_model = AutoModelForCausalLM.from_pretrained("WizardLM/WizardCoder-1B-V1.0")
|
| 46 |
+
# Mistral 7B
|
| 47 |
+
mistral_llm = LLM("mistralai/Mistral-7B-v0.1",30000)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# WizardCoder 13B
|
| 51 |
+
wizard_llm = LLM("WizardLM/WizardCoder-Python-13B-V1.0",8000)
|
| 52 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# hf_llm = HuggingFacePipeline(pipeline=pipe)
|
| 54 |
|
| 55 |
+
def ask_model(model, prompt):
|
| 56 |
+
if(model == 'mistral'):
|
| 57 |
+
return mistral_llm(prompt)
|
| 58 |
+
if(model == 'wizard'):
|
| 59 |
+
return wizard_llm(prompt)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
|
| 63 |
@app.get("/")
|
| 64 |
def root():
|
| 65 |
return {"message": "R&D LLM API"}
|
| 66 |
+
|
| 67 |
+
# @app.get("/get")
|
| 68 |
+
# def get():
|
| 69 |
+
# result = pipe("name 5 programming languages",do_sample=False)
|
| 70 |
+
# print(result)
|
| 71 |
+
# return {"message": result}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
@app.post("/ask_llm")
|
| 81 |
+
async def ask_llm_endpoint(model:str, prompt: str):
|
| 82 |
+
result = ask_model(model,prompt)
|
|
|
|
| 83 |
return {"result": result}
|
| 84 |
|
| 85 |
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
// APIs
|
| 94 |
+
|
| 95 |
+
# @app.post("/ask_HFAPI")
|
| 96 |
+
# def ask_HFAPI_endpoint(prompt: str):
|
| 97 |
+
# result = query(prompt)
|
| 98 |
+
# return {"result": result}
|
| 99 |
|
| 100 |
from langchain.llms import OpenAI
|
| 101 |
|