adowu commited on
Commit
26f9959
·
verified ·
1 Parent(s): 0b467b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -75
app.py CHANGED
@@ -1,85 +1,43 @@
1
- from flask import Flask, request, jsonify, Response
2
- import asyncio
3
- from playwright.async_api import async_playwright
4
- import base64
5
  import os
 
6
  app = Flask(__name__)
7
 
8
- async def ask_copilot(question):
9
- print("Launching browser...")
10
  try:
11
- async with async_playwright() as p:
12
- browser = await p.chromium.launch(headless=True)
13
- print("Browser launched successfully.")
14
- page = await browser.new_page()
15
- await page.set_viewport_size({"width": 750, "height": 915})
16
- print("Navigating to Copilot...")
17
- await page.goto('https://copilot.microsoft.com/', wait_until='networkidle')
18
-
19
- # Input question and submit
20
- print("Entering question...")
21
- await page.wait_for_selector('#userInput')
22
- await page.click('#userInput')
23
- await page.type('#userInput', question)
24
- await page.keyboard.press('Enter')
25
-
26
- # Wait for response - **IMPORTANT: REPLACE WITH CORRECT SELECTOR**
27
- print("Waiting for response element...")
28
- response_selector = '[data-testid="conversation-turn"]' # <--- CHANGE THIS!
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
- async def ask_route():
62
- if not request.json or 'question' not in request.json:
63
- return jsonify({'error': 'Brak pytania'}), 400
 
 
64
 
65
- question = request.json['question']
66
- print(f"Received question: {question}")
 
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)))