File size: 1,600 Bytes
abf1517
e717066
df4d00f
5488267
abf1517
 
e717066
c506cc4
abf1517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask, request, jsonify
import requests
import os

app = Flask(__name__)

# Hugging Face APIエンドポイントと認証トークン
HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Pixtral-Large-Instruct-2411"
HEADERS = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}


# /questionエンドポイント
@app.route('/question', methods=['GET'])
def question():
    # パラメーターの取得
    json_input = request.args.get("json")
    text_input = request.args.get("text")

    if not json_input and not text_input:
        return jsonify({"error": "Please provide 'json' or 'text' parameter."}), 400

    # 入力データの整形
    if json_input:
        try:
            inputs = json_input
        except Exception as e:
            return jsonify({"error": f"Invalid JSON format: {str(e)}"}), 400
    elif text_input:
        inputs = text_input

    # Hugging Face APIリクエストの送信
    payload = {
        "inputs": inputs,
        "parameters": {"max_new_tokens": 512}
    }

    try:
        response = requests.post(HF_API_URL, headers=HEADERS, json=payload)

        if response.status_code == 200:
            return jsonify({"response": response.json()})
        else:
            return jsonify({"error": f"Hugging Face API Error {response.status_code}: {response.text}"}), response.status_code

    except Exception as e:
        return jsonify({"error": f"Request failed: {str(e)}"}), 500


if __name__ == '__main__':
    # デバッグモードでサーバーを起動
    app.run(debug=True, host="0.0.0.0", port=7860)