Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import nest_asyncio
|
2 |
nest_asyncio.apply()
|
3 |
|
@@ -31,44 +33,39 @@ VERSION_NUMBER = 2
|
|
31 |
# Asynchronous function to generate handwriting image via Pyppeteer
|
32 |
# ----------------------------
|
33 |
async def _generate_handwriting_image(text_prompt, screenshot_path):
|
34 |
-
"""
|
35 |
-
Launches a headless browser, goes to Calligraphr, types the text,
|
36 |
-
and takes a screenshot of the rendered handwriting.
|
37 |
-
"""
|
38 |
-
# Launch Chromium with additional flags for containerized environments
|
39 |
-
browser = await launch(
|
40 |
-
headless=True,
|
41 |
-
handleSIGINT=False,
|
42 |
-
handleSIGTERM=False,
|
43 |
-
handleSIGHUP=False,
|
44 |
-
args=[
|
45 |
-
'--no-sandbox',
|
46 |
-
'--disable-setuid-sandbox',
|
47 |
-
'--disable-dev-shm-usage',
|
48 |
-
'--disable-gpu',
|
49 |
-
'--single-process',
|
50 |
-
'--no-zygote',
|
51 |
-
'--window-size=1920,1080'
|
52 |
-
]
|
53 |
-
)
|
54 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
page = await browser.newPage()
|
56 |
-
await page.goto('https://www.calligraphr.com/en/font/', {
|
57 |
-
|
|
|
|
|
|
|
58 |
await page.type('#text-input', text_prompt)
|
59 |
-
|
60 |
-
# Give the page time to render the handwriting
|
61 |
-
await asyncio.sleep(3)
|
62 |
-
|
63 |
-
# Screenshot a portion of the page that should contain the handwriting
|
64 |
await page.screenshot({
|
65 |
'path': screenshot_path,
|
66 |
'clip': {'x': 100, 'y': 200, 'width': 600, 'height': 150}
|
67 |
})
|
68 |
return screenshot_path
|
69 |
-
|
|
|
|
|
70 |
finally:
|
71 |
-
|
|
|
72 |
|
73 |
def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.png"):
|
74 |
"""
|
@@ -218,6 +215,11 @@ interface = gr.Interface(
|
|
218 |
allow_flagging="never"
|
219 |
)
|
220 |
|
|
|
|
|
221 |
if __name__ == "__main__":
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
1 |
+
!apt-get install -y chromium-browser chromium-chromedriver libnss3 libxss1 libatk-bridge2.0-0 libgtk-3-0 libgbm-dev
|
2 |
+
|
3 |
import nest_asyncio
|
4 |
nest_asyncio.apply()
|
5 |
|
|
|
33 |
# Asynchronous function to generate handwriting image via Pyppeteer
|
34 |
# ----------------------------
|
35 |
async def _generate_handwriting_image(text_prompt, screenshot_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
try:
|
37 |
+
browser = await launch(
|
38 |
+
headless=True,
|
39 |
+
executablePath="/usr/bin/chromium-browser",
|
40 |
+
args=[
|
41 |
+
'--no-sandbox',
|
42 |
+
'--disable-setuid-sandbox',
|
43 |
+
'--disable-dev-shm-usage',
|
44 |
+
'--disable-gpu',
|
45 |
+
'--single-process',
|
46 |
+
'--no-zygote',
|
47 |
+
'--window-size=1920,1080'
|
48 |
+
]
|
49 |
+
)
|
50 |
page = await browser.newPage()
|
51 |
+
await page.goto('https://www.calligraphr.com/en/font/', {
|
52 |
+
'waitUntil': 'networkidle2',
|
53 |
+
'timeout': 60000
|
54 |
+
})
|
55 |
+
await page.waitForSelector('#text-input', {'timeout': 30000})
|
56 |
await page.type('#text-input', text_prompt)
|
57 |
+
await asyncio.sleep(5)
|
|
|
|
|
|
|
|
|
58 |
await page.screenshot({
|
59 |
'path': screenshot_path,
|
60 |
'clip': {'x': 100, 'y': 200, 'width': 600, 'height': 150}
|
61 |
})
|
62 |
return screenshot_path
|
63 |
+
except Exception as e:
|
64 |
+
logging.error(f"Pyppeteer error: {str(e)}")
|
65 |
+
return None
|
66 |
finally:
|
67 |
+
if 'browser' in locals():
|
68 |
+
await browser.close()
|
69 |
|
70 |
def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.png"):
|
71 |
"""
|
|
|
215 |
allow_flagging="never"
|
216 |
)
|
217 |
|
218 |
+
# ... [rest of your existing code] ...
|
219 |
+
|
220 |
if __name__ == "__main__":
|
221 |
+
interface.launch(
|
222 |
+
server_name="0.0.0.0",
|
223 |
+
server_port=int(os.environ.get("PORT", 7860)),
|
224 |
+
enable_queue=True
|
225 |
+
)
|