Spaces:
Sleeping
Sleeping
Add Playwright service and integrate with tools.py for web search functionality
Browse files- Dockerfile +6 -0
- playwright_service.js +38 -0
- run.sh +5 -1
- tools.py +7 -28
Dockerfile
CHANGED
@@ -39,6 +39,12 @@ USER user
|
|
39 |
COPY --chown=user requirements.txt requirements.txt
|
40 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Copy nginx configuration
|
43 |
COPY --chown=user nginx.conf /etc/nginx/sites-available/default
|
44 |
|
|
|
39 |
COPY --chown=user requirements.txt requirements.txt
|
40 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
41 |
|
42 |
+
# Install Node.js and Playwright
|
43 |
+
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
|
44 |
+
apt-get install -y nodejs && \
|
45 |
+
npm install -g npm && \
|
46 |
+
npm install express playwright
|
47 |
+
|
48 |
# Copy nginx configuration
|
49 |
COPY --chown=user nginx.conf /etc/nginx/sites-available/default
|
50 |
|
playwright_service.js
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import express from 'express';
|
2 |
+
import { chromium } from 'playwright';
|
3 |
+
import fs from 'fs';
|
4 |
+
|
5 |
+
const app = express();
|
6 |
+
app.use(express.json());
|
7 |
+
|
8 |
+
app.post('/search', async (req, res) => {
|
9 |
+
const { query } = req.body;
|
10 |
+
if (!query) return res.status(400).json({ error: 'Missing query' });
|
11 |
+
let browser;
|
12 |
+
try {
|
13 |
+
browser = await chromium.launch({ headless: true });
|
14 |
+
const context = await browser.newContext({
|
15 |
+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
16 |
+
viewport: { width: 1280, height: 800 },
|
17 |
+
locale: 'en-US',
|
18 |
+
});
|
19 |
+
const page = await context.newPage();
|
20 |
+
// Use Bing's HTML search endpoint
|
21 |
+
const searchUrl = 'https://www.bing.com/search?q=' + encodeURIComponent(query);
|
22 |
+
await page.goto(searchUrl, { waitUntil: 'domcontentloaded' });
|
23 |
+
await page.waitForTimeout(1200 + Math.floor(Math.random() * 800)); // Random delay
|
24 |
+
// Get visible text from the results page
|
25 |
+
const visibleText = await page.evaluate(() => document.body.innerText);
|
26 |
+
res.json({ result: visibleText });
|
27 |
+
} catch (e) {
|
28 |
+
console.error('Playwright error:', e);
|
29 |
+
res.status(500).json({ error: e.message });
|
30 |
+
} finally {
|
31 |
+
if (browser) await browser.close();
|
32 |
+
}
|
33 |
+
});
|
34 |
+
|
35 |
+
const PORT = process.env.PLAYWRIGHT_PORT || 5000;
|
36 |
+
app.listen(PORT, () => {
|
37 |
+
console.log(`Playwright service running on port ${PORT}`);
|
38 |
+
});
|
run.sh
CHANGED
@@ -3,10 +3,14 @@
|
|
3 |
# start nginx
|
4 |
service nginx start
|
5 |
|
|
|
|
|
|
|
6 |
# start the processes
|
7 |
python tools.py > /dev/stdout 2>&1 & echo $! > tools.pid
|
8 |
python app.py > /dev/stdout 2>&1 # blocking
|
9 |
|
10 |
# when unblocked, kill other processes and clean up
|
11 |
pkill -F tools.pid
|
12 |
-
|
|
|
|
3 |
# start nginx
|
4 |
service nginx start
|
5 |
|
6 |
+
# start the Playwright service
|
7 |
+
node playwright_service.js > /dev/stdout 2>&1 & echo $! > playwright.pid
|
8 |
+
|
9 |
# start the processes
|
10 |
python tools.py > /dev/stdout 2>&1 & echo $! > tools.pid
|
11 |
python app.py > /dev/stdout 2>&1 # blocking
|
12 |
|
13 |
# when unblocked, kill other processes and clean up
|
14 |
pkill -F tools.pid
|
15 |
+
pkill -F playwright.pid
|
16 |
+
rm tools.pid playwright.pid
|
tools.py
CHANGED
@@ -51,38 +51,17 @@ def calculate() -> Response:
|
|
51 |
@tools.route("/websearch", methods=["POST"])
|
52 |
def google_search() -> Response:
|
53 |
try:
|
54 |
-
data = json.loads(request.data)
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
time.sleep(1)
|
62 |
-
|
63 |
-
search_results = driver.find_elements(By.CSS_SELECTOR, "div.kno-rdesc span span")
|
64 |
-
if len(search_results) > 0: # check if google quick answer box exists
|
65 |
-
return jsonify(result=search_results[0].text)
|
66 |
-
else: # otherwise, find list of search results
|
67 |
-
search_results = driver.find_elements(By.CSS_SELECTOR, "div.g")
|
68 |
-
for result in search_results:
|
69 |
-
try: # first sentence of last span element in result
|
70 |
-
first_result = result.find_elements(By.CSS_SELECTOR, "div.VwiC3b span")
|
71 |
-
return jsonify(result=first_result[-1].text.split("...")[0])
|
72 |
-
except IndexError: # if no span element in result, go to next result
|
73 |
-
pass
|
74 |
-
return jsonify(result="No search results found")
|
75 |
except KeyError as e:
|
76 |
abort(400, description="Missing value in request body: " + str(e))
|
77 |
except Exception as e:
|
78 |
abort(400, description="Error: " + str(e))
|
79 |
-
finally:
|
80 |
-
try:
|
81 |
-
# quit selenium and kill the vnc
|
82 |
-
driver.quit()
|
83 |
-
os.system("pkill -1 Xvnc")
|
84 |
-
except Exception:
|
85 |
-
pass
|
86 |
|
87 |
@tools.route("/", methods=["GET"])
|
88 |
def home() -> str:
|
|
|
51 |
@tools.route("/websearch", methods=["POST"])
|
52 |
def google_search() -> Response:
|
53 |
try:
|
54 |
+
data = json.loads(request.data)
|
55 |
+
import requests
|
56 |
+
# Call the Playwright service
|
57 |
+
resp = requests.post("http://localhost:5000/search", json={"query": data["query"]}, timeout=30)
|
58 |
+
resp.raise_for_status()
|
59 |
+
result = resp.json().get("result", "No result returned")
|
60 |
+
return jsonify(result=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
except KeyError as e:
|
62 |
abort(400, description="Missing value in request body: " + str(e))
|
63 |
except Exception as e:
|
64 |
abort(400, description="Error: " + str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
@tools.route("/", methods=["GET"])
|
67 |
def home() -> str:
|