點擊後展開
import requests, json
from time import sleep
from tqdm.auto import tqdm, trange
API_URL = "https://api-inference.huggingface.co/models/jslin09/gpt2-chinese-cluecorpussmall-finetuned-fraud"
API_TOKEN = 'XXXXXXXXXXXXXXX' # 調用模型的 API token
headers = {"Authorization": f"Bearer {API_TOKEN}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return json.loads(response.content.decode("utf-8"))
prompt = "梅友乾明知其無資力支付酒店消費,亦無付款意願,竟意圖為自己不法之所有,"
query_dict = {
"inputs": prompt,
}
text_len = 300
t = trange(text_len, desc= '生成例稿', leave=True)
for i in t:
response = query(query_dict)
try:
response_text = response[0]['generated_text']
query_dict["inputs"] = response_text
t.set_description(f"{i}: {response[0]['generated_text']}")
t.refresh()
except KeyError:
sleep(30) # 如果伺服器太忙無回應,等30秒後再試。
pass
print(response[0]['generated_text'])
或是,你要使用 transformers 套件來實作你的程式,將本模型下載至你本地端的電腦中執行,可以參考下列程式碼: