yuduki / external_api_engine_hf.py
rusinopp's picture
Upload external_api_engine_hf.py
a2f7636 verified
raw
history blame
953 Bytes
import requests
# Hugging Faceのアクセストークンをここに記入
API_TOKEN = "YOUR_HUGGINGFACE_TOKEN"
API_URL = "https://api-inference.huggingface.co/models/rinna/japanese-gpt-neox-3.6b"
headers = {
"Authorization": f"Bearer {API_TOKEN}"
}
def call_external_api(input_text):
try:
payload = {
"inputs": input_text,
"parameters": {
"max_new_tokens": 100,
"temperature": 0.8,
"do_sample": True
}
}
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
response.raise_for_status()
generated = response.json()
if isinstance(generated, list) and "generated_text" in generated[0]:
return generated[0]["generated_text"]
else:
return "[API応答形式が不明です]"
except Exception as e:
return f"[Hugging Face APIエラー] {str(e)}"