Upload external_api_engine_hf.py
Browse files- external_api_engine_hf.py +31 -0
external_api_engine_hf.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Hugging Faceのアクセストークンをここに記入
|
5 |
+
API_TOKEN = "YOUR_HUGGINGFACE_TOKEN"
|
6 |
+
API_URL = "https://api-inference.huggingface.co/models/rinna/japanese-gpt-neox-3.6b"
|
7 |
+
|
8 |
+
headers = {
|
9 |
+
"Authorization": f"Bearer {API_TOKEN}"
|
10 |
+
}
|
11 |
+
|
12 |
+
def call_external_api(input_text):
|
13 |
+
try:
|
14 |
+
payload = {
|
15 |
+
"inputs": input_text,
|
16 |
+
"parameters": {
|
17 |
+
"max_new_tokens": 100,
|
18 |
+
"temperature": 0.8,
|
19 |
+
"do_sample": True
|
20 |
+
}
|
21 |
+
}
|
22 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
|
23 |
+
response.raise_for_status()
|
24 |
+
generated = response.json()
|
25 |
+
|
26 |
+
if isinstance(generated, list) and "generated_text" in generated[0]:
|
27 |
+
return generated[0]["generated_text"]
|
28 |
+
else:
|
29 |
+
return "[API応答形式が不明です]"
|
30 |
+
except Exception as e:
|
31 |
+
return f"[Hugging Face APIエラー] {str(e)}"
|