File size: 1,660 Bytes
26f9959 0b467b5 26f9959 7a8ef73 26f9959 7a8ef73 26f9959 7a8ef73 26f9959 7a8ef73 26f9959 7a8ef73 277aa19 26f9959 |
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 |
from flask import Flask, request, jsonify
import subprocess
import json
import os
app = Flask(__name__)
def run_puppeteer_script(question):
"""Uruchamia skrypt Node.js (Puppeteer) jako oddzielny proces."""
try:
# Używamy npx, aby upewnić się, że Puppeteer jest dostępny
# i że mamy najnowszą wersję zgodną z naszym skryptem JS.
result = subprocess.run(
['node', 'ask_copilot.js', question], # Przekazanie pytania jako argumentu
capture_output=True,
text=True,
check=True,
env=dict(os.environ, PUPPETEER_EXECUTABLE_PATH='/usr/bin/google-chrome'), # Ustawienie zmiennej środowiskowej
timeout=60 # Dodanie timeoutu
)
# Skrypt JS powinien zwracać odpowiedź w formacie JSON
return json.loads(result.stdout)
except subprocess.CalledProcessError as e:
return {"error": "Błąd podczas uruchamiania Puppeteer", "details": str(e)}
except json.JSONDecodeError:
return {"error": "Błąd dekodowania JSON", "output": result.stdout}
except subprocess.TimeoutExpired:
return {"error": "Przekroczono czas oczekiwania na odpowiedź"}
@app.route('/api/ask', methods=['POST'])
def ask_copilot_route():
"""Endpoint API do zadawania pytań Copilot."""
data = request.get_json()
if not data or 'question' not in data:
return jsonify({"error": "Brak pytania"}), 400
question = data['question']
response = run_puppeteer_script(question)
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860))) |