Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,43 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
import base64
|
| 5 |
import os
|
|
|
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
try:
|
| 30 |
-
await page.wait_for_selector(response_selector, timeout=60000)
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print(f"Timeout waiting for selector: {e}")
|
| 33 |
-
await browser.close()
|
| 34 |
-
raise
|
| 35 |
-
|
| 36 |
-
# Get response
|
| 37 |
-
print("Retrieving response...")
|
| 38 |
-
response = await page.evaluate(f'(selector) => {{ const element = document.querySelector(selector); return element ? element.innerText : "Brak odpowiedzi"; }}', response_selector)
|
| 39 |
-
print("Response retrieved.")
|
| 40 |
-
|
| 41 |
-
# Take a screenshot (optional, for debugging)
|
| 42 |
-
screenshot = await page.screenshot()
|
| 43 |
-
screenshot_base64 = base64.b64encode(screenshot).decode('utf-8')
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return response, screenshot_base64 # Return both text and image
|
| 47 |
-
|
| 48 |
-
except Exception as e:
|
| 49 |
-
print(f"Error within ask_copilot: {e}")
|
| 50 |
-
print(f"Stack trace: {e.__traceback__}")
|
| 51 |
-
raise
|
| 52 |
-
|
| 53 |
-
finally:
|
| 54 |
-
if 'browser' in locals() and browser:
|
| 55 |
-
print("Closing browser...")
|
| 56 |
-
await browser.close()
|
| 57 |
-
print("Browser closed.")
|
| 58 |
|
| 59 |
|
| 60 |
@app.route('/api/ask', methods=['POST'])
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
question =
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
try:
|
| 69 |
-
answer, screenshot_base64 = await ask_copilot(question) # Get text and image
|
| 70 |
-
print("Sending answer...")
|
| 71 |
-
# Return both answer and screenshot (for debugging)
|
| 72 |
-
return jsonify({'answer': answer, 'screenshot': screenshot_base64})
|
| 73 |
-
except Exception as e:
|
| 74 |
-
print(f"Error in /api/ask: {e}")
|
| 75 |
-
return jsonify({'error': 'Błąd podczas uzyskiwania odpowiedzi', 'details': str(e)}), 500
|
| 76 |
if __name__ == '__main__':
|
| 77 |
-
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
| 78 |
-
|
| 79 |
-
# For Hugging Face Spaces, we DO NOT need to run the app with app.run().
|
| 80 |
-
# Hugging Face Spaces handles this automatically. The following lines
|
| 81 |
-
# are only for local testing and should be commented out or removed
|
| 82 |
-
# when deploying to Hugging Face.
|
| 83 |
-
|
| 84 |
-
# if __name__ == '__main__':
|
| 85 |
-
# asyncio.run(app.run(debug=True, port=5000))
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import subprocess
|
| 3 |
+
import json
|
|
|
|
| 4 |
import os
|
| 5 |
+
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
def run_puppeteer_script(question):
|
| 9 |
+
"""Uruchamia skrypt Node.js (Puppeteer) jako oddzielny proces."""
|
| 10 |
try:
|
| 11 |
+
# Używamy npx, aby upewnić się, że Puppeteer jest dostępny
|
| 12 |
+
# i że mamy najnowszą wersję zgodną z naszym skryptem JS.
|
| 13 |
+
result = subprocess.run(
|
| 14 |
+
['node', 'ask_copilot.js', question], # Przekazanie pytania jako argumentu
|
| 15 |
+
capture_output=True,
|
| 16 |
+
text=True,
|
| 17 |
+
check=True,
|
| 18 |
+
env=dict(os.environ, PUPPETEER_EXECUTABLE_PATH='/usr/bin/google-chrome'), # Ustawienie zmiennej środowiskowej
|
| 19 |
+
timeout=60 # Dodanie timeoutu
|
| 20 |
+
)
|
| 21 |
+
# Skrypt JS powinien zwracać odpowiedź w formacie JSON
|
| 22 |
+
return json.loads(result.stdout)
|
| 23 |
+
except subprocess.CalledProcessError as e:
|
| 24 |
+
return {"error": "Błąd podczas uruchamiania Puppeteer", "details": str(e)}
|
| 25 |
+
except json.JSONDecodeError:
|
| 26 |
+
return {"error": "Błąd dekodowania JSON", "output": result.stdout}
|
| 27 |
+
except subprocess.TimeoutExpired:
|
| 28 |
+
return {"error": "Przekroczono czas oczekiwania na odpowiedź"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
@app.route('/api/ask', methods=['POST'])
|
| 32 |
+
def ask_copilot_route():
|
| 33 |
+
"""Endpoint API do zadawania pytań Copilot."""
|
| 34 |
+
data = request.get_json()
|
| 35 |
+
if not data or 'question' not in data:
|
| 36 |
+
return jsonify({"error": "Brak pytania"}), 400
|
| 37 |
|
| 38 |
+
question = data['question']
|
| 39 |
+
response = run_puppeteer_script(question)
|
| 40 |
+
return jsonify(response)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if __name__ == '__main__':
|
| 43 |
+
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|