THEZYZSTUDIO commited on
Commit
de06120
ยท
verified ยท
1 Parent(s): f28f1a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -109
app.py CHANGED
@@ -214,7 +214,9 @@ def _get_mouse_pos() -> tuple[int, int]:
214
 
215
  def capture_screen_raw() -> tuple[object, int, int]:
216
  """
217
- ูŠู„ุชู‚ุท ุงู„ุดุงุดุฉ ุจู€ 5 ุทุฑู‚ ู…ุชุณู„ุณู„ุฉ โ€” ุชุถู…ู† ู†ุฌุงุญ ูˆุงุญุฏุฉ:
 
 
218
  1. scrot (ุงู„ุฃุณุฑุน)
219
  2. import (ImageMagick)
220
  3. xwd + convert
@@ -224,9 +226,10 @@ def capture_screen_raw() -> tuple[object, int, int]:
224
  from PIL import Image
225
  env = {**os.environ, "DISPLAY": DISPLAY}
226
  tmp = f"/tmp/zs_{int(time.time()*1000)}"
 
227
 
228
  def _load(path) -> tuple:
229
- """ุชุญู…ูŠู„ ุงู„ุตูˆุฑุฉ ุฅุฐุง ูˆูุฌุฏุช ูˆุญุฌู…ู‡ุง > 0"""
230
  if not path or not os.path.exists(path):
231
  return None, 0, 0
232
  size = os.path.getsize(path)
@@ -237,123 +240,139 @@ def capture_screen_raw() -> tuple[object, int, int]:
237
  w, h = img.size
238
  if w < 10 or h < 10:
239
  return None, 0, 0
240
- try: os.unlink(path)
241
- except: pass
242
  return img, w, h
243
  except Exception as ex:
244
  print(f"[_load] {ex}")
245
  return None, 0, 0
246
 
247
- # โ”€โ”€ ุทุฑูŠู‚ุฉ 1: scrot โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
248
- try:
249
- p = tmp + "_1.png"
250
- r = subprocess.run(
251
- ["scrot", "-q", "95", p],
252
- env=env, timeout=8, capture_output=True
253
- )
254
- img, w, h = _load(p)
255
- if img:
256
- print("[capture] โœ… scrot")
257
- return img, w, h
258
- print(f"[capture] scrot failed rc={r.returncode} err={r.stderr[:80]}")
259
- except Exception as e:
260
- print(f"[capture] scrot ex: {e}")
261
 
262
- # โ”€โ”€ ุทุฑูŠู‚ุฉ 2: ImageMagick import โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
263
  try:
264
- p = tmp + "_2.png"
265
- r = subprocess.run(
266
- ["import", "-window", "root", "-silent", p],
267
- env=env, timeout=10, capture_output=True
268
- )
269
- img, w, h = _load(p)
270
- if img:
271
- print("[capture] โœ… import (ImageMagick)")
272
- return img, w, h
273
- print(f"[capture] import failed rc={r.returncode}")
274
- except Exception as e:
275
- print(f"[capture] import ex: {e}")
 
 
 
276
 
277
- # โ”€โ”€ ุทุฑูŠู‚ุฉ 3: xwd + convert โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
278
- try:
279
- xwd_p = tmp + "_3.xwd"
280
- png_p = tmp + "_3.png"
281
- r1 = subprocess.run(
282
- ["xwd", "-root", "-silent", "-out", xwd_p],
283
- env=env, timeout=10, capture_output=True
284
- )
285
- r2 = subprocess.run(
286
- ["convert", xwd_p, png_p],
287
- timeout=10, capture_output=True
288
- )
289
- try: os.unlink(xwd_p)
290
- except: pass
291
- img, w, h = _load(png_p)
292
- if img:
293
- print("[capture] โœ… xwd+convert")
294
- return img, w, h
295
- print(f"[capture] xwd rc={r1.returncode} convert rc={r2.returncode}")
296
- except Exception as e:
297
- print(f"[capture] xwd ex: {e}")
298
 
299
- # โ”€โ”€ ุทุฑูŠู‚ุฉ 4: ffmpeg x11grab โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
300
- try:
301
- p = tmp + "_4.png"
302
- sw, sh = _get_screen_size()
303
- r = subprocess.run([
304
- "ffmpeg", "-y",
305
- "-f", "x11grab",
306
- "-video_size", f"{sw}x{sh}",
307
- "-i", DISPLAY,
308
- "-vframes", "1",
309
- "-q:v", "2",
310
- p
311
- ], env=env, timeout=12, capture_output=True)
312
- img, w, h = _load(p)
313
- if img:
314
- print("[capture] โœ… ffmpeg x11grab")
315
- return img, w, h
316
- print(f"[capture] ffmpeg rc={r.returncode} err={r.stderr[-120:]}")
317
- except Exception as e:
318
- print(f"[capture] ffmpeg ex: {e}")
 
319
 
320
- # โ”€โ”€ ุทุฑูŠู‚ุฉ 5: python-xlib (Xlib ู…ุจุงุดุฑุฉ) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
321
- try:
322
- from Xlib import display as Xdisplay
323
- from Xlib.ext.xtest import fake_input
324
- xdisp = Xdisplay.Display(DISPLAY)
325
- root = xdisp.screen().root
326
- geom = root.get_geometry()
327
- w, h = geom.width, geom.height
328
- raw = root.get_image(0, 0, w, h,
329
- Xdisplay.X.ZPixmap,
330
- 0xFFFFFFFF)
331
- import struct
332
- data = raw.data
333
- # BGRA โ†’ RGB
334
- pixels = []
335
- for i in range(0, len(data), 4):
336
- b, g, r_, a = struct.unpack_from('BBBB', data, i)
337
- pixels.extend([r_, g, b])
338
- img = Image.frombytes("RGB", (w, h), bytes(pixels))
339
- print("[capture] โœ… python-xlib")
340
- return img, w, h
341
- except Exception as e:
342
- print(f"[capture] xlib ex: {e}")
343
 
344
- # โ”€โ”€ ุทุฑูŠู‚ุฉ 6: ุตูˆุฑุฉ placeholder ูˆุงุถุญุฉ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
345
- print("[capture] โš ๏ธ ALL methods failed โ€” returning placeholder")
346
- try:
347
- from PIL import ImageDraw
348
- sw, sh = _get_screen_size()
349
- img = Image.new("RGB", (sw or 1280, sh or 720), (20, 20, 30))
350
- draw = ImageDraw.Draw(img)
351
- draw.rectangle([(0, 0), (sw, 40)], fill=(30, 30, 50))
352
- draw.text((10, 10), f"โš ๏ธ Screenshot failed โ€” DISPLAY={DISPLAY}", fill=(255, 100, 100))
353
- draw.text((10, 50), "Methods tried: scrot, import, xwd, ffmpeg, xlib", fill=(150, 150, 150))
354
- return img, sw or 1280, sh or 720
355
- except:
356
- return None, 0, 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
 
358
 
359
  def capture_screen(scale=0.6, quality=70) -> str:
@@ -1136,4 +1155,11 @@ async def quick_search(query: str):
1136
 
1137
  if __name__ == "__main__":
1138
  port = int(os.environ.get("PORT", 7860))
1139
- uvicorn.run("app:app", host="0.0.0.0", port=port, log_level="info")
 
 
 
 
 
 
 
 
214
 
215
  def capture_screen_raw() -> tuple[object, int, int]:
216
  """
217
+ ูŠู„ุชู‚ุท ุงู„ุดุงุดุฉ ุจู€ 5 ุทุฑู‚ ู…ุชุณู„ุณู„ุฉ โ€” ุชุถู…ู† ู†ุฌุงุญ ูˆุงุญุฏุฉ.
218
+ ูƒู„ ุงู„ู…ู„ูุงุช ุงู„ู…ุคู‚ุชุฉ (ู†ุงุฌุญุฉ ุฃูˆ ูุงุดู„ุฉ) ุชูุญุฐู ุฏุงุฆู…ุงู‹ ู„ู…ู†ุน ุชุฑุงูƒู…ู‡ุง ููŠ /tmp
219
+ ูˆุงู…ุชู„ุงุก ุงู„ู‚ุฑุต/ุงู„ุฐุงูƒุฑุฉ ุจุนุฏ ุนุฏุฉ ู„ู‚ุทุงุช ุดุงุดุฉ ู…ุชุชุงู„ูŠุฉ.
220
  1. scrot (ุงู„ุฃุณุฑุน)
221
  2. import (ImageMagick)
222
  3. xwd + convert
 
226
  from PIL import Image
227
  env = {**os.environ, "DISPLAY": DISPLAY}
228
  tmp = f"/tmp/zs_{int(time.time()*1000)}"
229
+ _tmp_files_to_clean = [] # ูƒู„ ุงู„ู…ู„ูุงุช ุงู„ู…ุคู‚ุชุฉ ุงู„ุชูŠ ุฃูู†ุดุฆุช ููŠ ู‡ุฐู‡ ุงู„ู…ุญุงูˆู„ุฉ
230
 
231
  def _load(path) -> tuple:
232
+ """ุชุญู…ูŠู„ ุงู„ุตูˆุฑุฉ ุฅุฐุง ูˆูุฌุฏุช ูˆุญุฌู…ู‡ุง > 0 โ€” ู„ุง ูŠุญุฐู ุงู„ู…ู„ู ู‡ู†ุงุŒ ุงู„ุญุฐู ู…ุฑูƒุฒูŠ ููŠ ุงู„ู†ู‡ุงูŠุฉ"""
233
  if not path or not os.path.exists(path):
234
  return None, 0, 0
235
  size = os.path.getsize(path)
 
240
  w, h = img.size
241
  if w < 10 or h < 10:
242
  return None, 0, 0
 
 
243
  return img, w, h
244
  except Exception as ex:
245
  print(f"[_load] {ex}")
246
  return None, 0, 0
247
 
248
+ def _cleanup():
249
+ """ูŠุญุฐู ูƒู„ ุงู„ู…ู„ูุงุช ุงู„ู…ุคู‚ุชุฉ ุงู„ู…ุชุจู‚ูŠุฉ ู…ู† ูƒู„ ุงู„ู…ุญุงูˆู„ุงุช (ู†ุงุฌุญุฉ ุฃูˆ ูุงุดู„ุฉ)"""
250
+ for p in _tmp_files_to_clean:
251
+ try:
252
+ if p and os.path.exists(p):
253
+ os.unlink(p)
254
+ except Exception:
255
+ pass
 
 
 
 
 
 
256
 
 
257
  try:
258
+ # โ”€โ”€ ุทุฑูŠู‚ุฉ 1: scrot โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
259
+ try:
260
+ p = tmp + "_1.png"
261
+ _tmp_files_to_clean.append(p)
262
+ r = subprocess.run(
263
+ ["scrot", "-q", "95", p],
264
+ env=env, timeout=8, capture_output=True
265
+ )
266
+ img, w, h = _load(p)
267
+ if img:
268
+ print("[capture] โœ… scrot")
269
+ return img, w, h
270
+ print(f"[capture] scrot failed rc={r.returncode} err={r.stderr[:80]}")
271
+ except Exception as e:
272
+ print(f"[capture] scrot ex: {e}")
273
 
274
+ # โ”€โ”€ ุทุฑูŠู‚ุฉ 2: ImageMagick import โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
275
+ try:
276
+ p = tmp + "_2.png"
277
+ _tmp_files_to_clean.append(p)
278
+ r = subprocess.run(
279
+ ["import", "-window", "root", "-silent", p],
280
+ env=env, timeout=10, capture_output=True
281
+ )
282
+ img, w, h = _load(p)
283
+ if img:
284
+ print("[capture] โœ… import (ImageMagick)")
285
+ return img, w, h
286
+ print(f"[capture] import failed rc={r.returncode}")
287
+ except Exception as e:
288
+ print(f"[capture] import ex: {e}")
 
 
 
 
 
 
289
 
290
+ # โ”€โ”€ ุทุฑูŠู‚ุฉ 3: xwd + convert โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
291
+ try:
292
+ xwd_p = tmp + "_3.xwd"
293
+ png_p = tmp + "_3.png"
294
+ _tmp_files_to_clean.append(xwd_p)
295
+ _tmp_files_to_clean.append(png_p)
296
+ r1 = subprocess.run(
297
+ ["xwd", "-root", "-silent", "-out", xwd_p],
298
+ env=env, timeout=10, capture_output=True
299
+ )
300
+ r2 = subprocess.run(
301
+ ["convert", xwd_p, png_p],
302
+ timeout=10, capture_output=True
303
+ )
304
+ img, w, h = _load(png_p)
305
+ if img:
306
+ print("[capture] โœ… xwd+convert")
307
+ return img, w, h
308
+ print(f"[capture] xwd rc={r1.returncode} convert rc={r2.returncode}")
309
+ except Exception as e:
310
+ print(f"[capture] xwd ex: {e}")
311
 
312
+ # โ”€โ”€ ุทุฑูŠู‚ุฉ 4: ffmpeg x11grab โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
313
+ try:
314
+ p = tmp + "_4.png"
315
+ _tmp_files_to_clean.append(p)
316
+ sw, sh = _get_screen_size()
317
+ r = subprocess.run([
318
+ "ffmpeg", "-y",
319
+ "-f", "x11grab",
320
+ "-video_size", f"{sw}x{sh}",
321
+ "-i", DISPLAY,
322
+ "-vframes", "1",
323
+ "-q:v", "2",
324
+ p
325
+ ], env=env, timeout=12, capture_output=True)
326
+ img, w, h = _load(p)
327
+ if img:
328
+ print("[capture] โœ… ffmpeg x11grab")
329
+ return img, w, h
330
+ print(f"[capture] ffmpeg rc={r.returncode} err={r.stderr[-120:]}")
331
+ except Exception as e:
332
+ print(f"[capture] ffmpeg ex: {e}")
 
 
333
 
334
+ # โ”€โ”€ ุทุฑูŠู‚ุฉ 5: python-xlib (Xlib ู…ุจุงุดุฑุฉ) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
335
+ try:
336
+ from Xlib import display as Xdisplay
337
+ from Xlib.ext.xtest import fake_input
338
+ xdisp = Xdisplay.Display(DISPLAY)
339
+ root = xdisp.screen().root
340
+ geom = root.get_geometry()
341
+ w, h = geom.width, geom.height
342
+ raw = root.get_image(0, 0, w, h,
343
+ Xdisplay.X.ZPixmap,
344
+ 0xFFFFFFFF)
345
+ import struct
346
+ data = raw.data
347
+ # BGRA โ†’ RGB
348
+ pixels = []
349
+ for i in range(0, len(data), 4):
350
+ b, g, r_, a = struct.unpack_from('BBBB', data, i)
351
+ pixels.extend([r_, g, b])
352
+ img = Image.frombytes("RGB", (w, h), bytes(pixels))
353
+ print("[capture] โœ… python-xlib")
354
+ return img, w, h
355
+ except Exception as e:
356
+ print(f"[capture] xlib ex: {e}")
357
+
358
+ # โ”€โ”€ ุทุฑูŠู‚ุฉ 6: ุตูˆุฑุฉ placeholder ูˆุงุถุญุฉ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
359
+ print("[capture] โš ๏ธ ALL methods failed โ€” returning placeholder")
360
+ try:
361
+ from PIL import ImageDraw
362
+ sw, sh = _get_screen_size()
363
+ img = Image.new("RGB", (sw or 1280, sh or 720), (20, 20, 30))
364
+ draw = ImageDraw.Draw(img)
365
+ draw.rectangle([(0, 0), (sw, 40)], fill=(30, 30, 50))
366
+ draw.text((10, 10), f"โš ๏ธ Screenshot failed โ€” DISPLAY={DISPLAY}", fill=(255, 100, 100))
367
+ draw.text((10, 50), "Methods tried: scrot, import, xwd, ffmpeg, xlib", fill=(150, 150, 150))
368
+ return img, sw or 1280, sh or 720
369
+ except:
370
+ return None, 0, 0
371
+ finally:
372
+ # โ”€โ”€ ูŠุญุฐู ูƒู„ ุงู„ู…ู„ูุงุช ุงู„ู…ุคู‚ุชุฉ ุฏุงุฆู…ุงู‹ุŒ ู†ุฌุญุช ุงู„ู…ุญุงูˆู„ุฉ ุฃูˆ ูุดู„ุช โ”€โ”€
373
+ # ู‡ุฐุง ูŠู…ู†ุน ุชุฑุงูƒู… ู…ู„ูุงุช /tmp ุงู„ุชูŠ ูƒุงู†ุช ุชู…ุชู„ุฆ ุจุนุฏ ุนุฏุฉ ู„ู‚ุทุงุช ูˆุชุชุณุจุจ
374
+ # ุจู‚ุชู„ ุงู„ุนู…ู„ูŠุงุช (SIGTERM/OOM) ุนู„ู‰ ุจูŠุฆุงุช ู…ุซู„ Hugging Face ุงู„ู…ุฌุงู†ูŠุฉ.
375
+ _cleanup()
376
 
377
 
378
  def capture_screen(scale=0.6, quality=70) -> str:
 
1155
 
1156
  if __name__ == "__main__":
1157
  port = int(os.environ.get("PORT", 7860))
1158
+ uvicorn.run("app:app", host="0.0.0.0", port=port, log_level="info")
1159
+
1160
+
1161
+
1162
+
1163
+
1164
+
1165
+