[a72e01]: / src / utils.py

Download this file

25 lines (20 with data), 750 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import requests
import os
from typing import Optional
HUGGING_FACE_TOKEN = os.environ["HUGGING_FACE_TOKEN"]
MODEL_API_URL = (
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
)
headers = {"Authorization": f"Bearer {HUGGING_FACE_TOKEN}"}
def query(payload: Optional[dict] = None, inputs: Optional[str] = None):
if payload is None:
payload = {
"inputs": inputs,
"parameters": {
"max_new_tokens": 240,
"return_full_text": False,
},
"options": {"use_cache": True, "wait_for_model": True},
}
response = requests.post(MODEL_API_URL, headers=headers, json=payload)
return response.json()[0]["generated_text"]