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)