|
from flask import Flask, request, jsonify |
|
import requests |
|
import os |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Pixtral-Large-Instruct-2411" |
|
HEADERS = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"} |
|
|
|
|
|
|
|
@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 |
|
|
|
|
|
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) |
|
|