GPTfree api commited on
Commit
abf1517
·
verified ·
1 Parent(s): ee8726e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -17
app.py CHANGED
@@ -1,21 +1,51 @@
 
1
  import requests
2
  import os
3
 
 
 
4
  # Hugging Face APIエンドポイントと認証トークン
5
- api_url = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
6
- headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
7
-
8
- # クエリのメッセージ
9
- payload = {
10
- "inputs": "Which of the depicted countries has the best food?",
11
- "parameters": {"max_new_tokens": 512}
12
- }
13
-
14
- # APIリクエスト
15
- response = requests.post(api_url, headers=headers, json=payload)
16
-
17
- # 結果表示
18
- if response.status_code == 200:
19
- print("Response:", response.json())
20
- else:
21
- print(f"Error {response.status_code}: {response.text}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
  import requests
3
  import os
4
 
5
+ app = Flask(__name__)
6
+
7
  # Hugging Face APIエンドポイントと認証トークン
8
+ HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
9
+ HEADERS = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
10
+
11
+
12
+ # /questionエンドポイント
13
+ @app.route('/question', methods=['GET'])
14
+ def question():
15
+ # パラメーターの取得
16
+ json_input = request.args.get("json")
17
+ text_input = request.args.get("text")
18
+
19
+ if not json_input and not text_input:
20
+ return jsonify({"error": "Please provide 'json' or 'text' parameter."}), 400
21
+
22
+ # 入力データの整形
23
+ if json_input:
24
+ try:
25
+ inputs = json_input
26
+ except Exception as e:
27
+ return jsonify({"error": f"Invalid JSON format: {str(e)}"}), 400
28
+ elif text_input:
29
+ inputs = text_input
30
+
31
+ # Hugging Face APIリクエストの送信
32
+ payload = {
33
+ "inputs": inputs,
34
+ "parameters": {"max_new_tokens": 512}
35
+ }
36
+
37
+ try:
38
+ response = requests.post(HF_API_URL, headers=HEADERS, json=payload)
39
+
40
+ if response.status_code == 200:
41
+ return jsonify({"response": response.json()})
42
+ else:
43
+ return jsonify({"error": f"Hugging Face API Error {response.status_code}: {response.text}"}), response.status_code
44
+
45
+ except Exception as e:
46
+ return jsonify({"error": f"Request failed: {str(e)}"}), 500
47
+
48
+
49
+ if __name__ == '__main__':
50
+ # デバッグモードでサーバーを起動
51
+ app.run(debug=True, host="0.0.0.0", port=7860)