adowu commited on
Commit
7a8ef73
·
verified ·
1 Parent(s): ad7e105

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify, Response
2
+ import asyncio
3
+ from playwright.async_api import async_playwright
4
+ import base64
5
+
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
+
77
+
78
+ # For Hugging Face Spaces, we DO NOT need to run the app with app.run().
79
+ # Hugging Face Spaces handles this automatically. The following lines
80
+ # are only for local testing and should be commented out or removed
81
+ # when deploying to Hugging Face.
82
+
83
+ # if __name__ == '__main__':
84
+ # asyncio.run(app.run(debug=True, port=5000))