Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,56 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import json
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
return
|
| 58 |
-
try:
|
| 59 |
-
_run_install(["playwright", "install", "--with-deps", "chromium"])
|
| 60 |
-
except Exception:
|
| 61 |
-
try:
|
| 62 |
-
_run_install(["python", "-m", "playwright", "install", "--with-deps", "chromium"])
|
| 63 |
-
except Exception:
|
| 64 |
-
_run_install(["python", "-m", "playwright", "install", "chromium"])
|
| 65 |
-
_checked = True
|
| 66 |
-
|
| 67 |
-
def screenshot(url: str) -> bytes:
|
| 68 |
-
ensure_browsers_installed()
|
| 69 |
-
from playwright.sync_api import sync_playwright
|
| 70 |
-
with sync_playwright() as pw:
|
| 71 |
-
browser = pw.chromium.launch(headless=True, args=["--no-sandbox", "--disable-dev-shm-usage"])
|
| 72 |
-
try:
|
| 73 |
-
page = browser.new_page(viewport={"width": 1280, "height": 720})
|
| 74 |
-
page.set_default_navigation_timeout(45000)
|
| 75 |
-
page.set_default_timeout(45000)
|
| 76 |
-
try:
|
| 77 |
-
page.goto(url, wait_until="networkidle")
|
| 78 |
-
except Exception:
|
| 79 |
-
page.goto(url, wait_until="domcontentloaded")
|
| 80 |
-
try:
|
| 81 |
-
page.wait_for_load_state("networkidle", timeout=5000)
|
| 82 |
-
except Exception:
|
| 83 |
-
pass
|
| 84 |
-
page.wait_for_timeout(600)
|
| 85 |
-
return page.screenshot(full_page=True, type="png")
|
| 86 |
-
finally:
|
| 87 |
-
try:
|
| 88 |
-
browser.close()
|
| 89 |
-
except Exception:
|
| 90 |
-
pass
|
| 91 |
-
|
| 92 |
-
@app.get("/")
|
| 93 |
-
def home():
|
| 94 |
-
return Response(INDEX, mimetype="text/html; charset=utf-8")
|
| 95 |
-
|
| 96 |
-
@app.get("/api/")
|
| 97 |
-
def api():
|
| 98 |
-
try:
|
| 99 |
-
url = normalize(request.args.get("url", ""))
|
| 100 |
-
png = screenshot(url)
|
| 101 |
-
return Response(png, mimetype="image/png")
|
| 102 |
-
except ValueError as e:
|
| 103 |
-
return Response(json.dumps({"error": str(e)}), status=400, mimetype="application/json")
|
| 104 |
-
except subprocess.CalledProcessError as e:
|
| 105 |
-
out = (e.stdout or "").strip()
|
| 106 |
-
return Response(json.dumps({"error": out or "Playwright install failed"}), status=500, mimetype="application/json")
|
| 107 |
-
except Exception as e:
|
| 108 |
-
return Response(json.dumps({"error": f"{type(e).__name__}: {e}"}), status=502, mimetype="application/json")
|
| 109 |
-
|
| 110 |
-
if __name__ == "__main__":
|
| 111 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
|
| 1 |
+
import http.server
|
| 2 |
+
import socketserver
|
| 3 |
+
import json
|
| 4 |
+
from curl_cffi import requests
|
| 5 |
+
|
| 6 |
+
PORT = 7860
|
| 7 |
+
URL = "https://multi-modal.ai.cloudflare.com/api/inference?model=@cf/deepgram/nova-3&field=audio"
|
| 8 |
+
|
| 9 |
+
HTML = """
|
| 10 |
+
<!DOCTYPE html>
|
| 11 |
+
<html>
|
| 12 |
+
<body>
|
| 13 |
+
<h1>Nova-3 Playground</h1>
|
| 14 |
+
<input type="file" id="f" accept="audio/mpeg">
|
| 15 |
+
<button onclick="u()">Transcribe</button>
|
| 16 |
+
<p>Status: <span id="s">Idle</span></p>
|
| 17 |
+
<pre id="r" style="background:#eee;padding:10px"></pre>
|
| 18 |
+
<script>
|
| 19 |
+
async function u() {
|
| 20 |
+
const f = document.getElementById('f').files[0];
|
| 21 |
+
if(!f) return;
|
| 22 |
+
document.getElementById('s').innerText = "Processing...";
|
| 23 |
+
try {
|
| 24 |
+
const res = await fetch('/api', { method: 'POST', body: f });
|
| 25 |
+
const data = await res.json();
|
| 26 |
+
document.getElementById('r').innerText = JSON.stringify(data, null, 2);
|
| 27 |
+
document.getElementById('s').innerText = "Done";
|
| 28 |
+
} catch(e) {
|
| 29 |
+
document.getElementById('s').innerText = "Error";
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
</script>
|
| 33 |
+
</body>
|
| 34 |
+
</html>
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
class Handler(http.server.BaseHTTPRequestHandler):
|
| 38 |
+
def do_GET(self):
|
| 39 |
+
self.send_response(200)
|
| 40 |
+
self.send_header("Content-type", "text/html")
|
| 41 |
+
self.end_headers()
|
| 42 |
+
self.wfile.write(HTML.encode())
|
| 43 |
+
|
| 44 |
+
def do_POST(self):
|
| 45 |
+
if self.path == '/api':
|
| 46 |
+
length = int(self.headers['Content-Length'])
|
| 47 |
+
body = self.rfile.read(length)
|
| 48 |
+
r = requests.post(URL, content=body, impersonate="chrome")
|
| 49 |
+
self.send_response(200)
|
| 50 |
+
self.send_header("Content-type", "application/json")
|
| 51 |
+
self.end_headers()
|
| 52 |
+
self.wfile.write(r.content)
|
| 53 |
+
|
| 54 |
+
with socketserver.TCPServer(("", PORT), Handler) as d:
|
| 55 |
+
print(f"Server: {PORT}")
|
| 56 |
+
d.serve_forever()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|