Spaces:
Configuration error
Configuration error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,142 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import shutil
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import gradio as gr
|
| 6 |
-
from selenium import webdriver
|
| 7 |
-
from selenium.webdriver.chrome.service import Service
|
| 8 |
-
from selenium.webdriver.chrome.options import Options
|
| 9 |
-
from selenium.webdriver.common.by import By
|
| 10 |
-
import time
|
| 11 |
-
|
| 12 |
-
# --- แแแแผแแแแแปแแแทแแแแแแ ---
|
| 13 |
-
PROFILE_PATH = "/tmp/chrome_profiles"
|
| 14 |
-
CONFIG_FILE = "/tmp/config_hf_v3.json"
|
| 15 |
-
SCREENSHOT_PATH = "/tmp/screenshot.png"
|
| 16 |
-
|
| 17 |
-
if not os.path.exists(PROFILE_PATH):
|
| 18 |
-
os.makedirs(PROFILE_PATH)
|
| 19 |
-
|
| 20 |
-
active_drivers = {}
|
| 21 |
-
|
| 22 |
-
def load_config():
|
| 23 |
-
if not os.path.exists(CONFIG_FILE):
|
| 24 |
-
default = {
|
| 25 |
-
"device_presets": [
|
| 26 |
-
{"name": "Windows Desktop", "ua": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "size": "1920,1080"},
|
| 27 |
-
{"name": "Android Mobile", "ua": "Mozilla/5.0 (Linux; Android 13; SM-S901B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36", "size": "360,800"},
|
| 28 |
-
{"name": "iPhone Pro", "ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1", "size": "393,852"}
|
| 29 |
-
]
|
| 30 |
-
}
|
| 31 |
-
with open(CONFIG_FILE, "w") as f: json.dump(default, f, indent=4)
|
| 32 |
-
return default
|
| 33 |
-
with open(CONFIG_FILE, "r") as f: return json.load(f)
|
| 34 |
-
|
| 35 |
-
config = load_config()
|
| 36 |
-
|
| 37 |
-
# --- แแปแแแถแ Logic ---
|
| 38 |
-
|
| 39 |
-
def get_profile_list():
|
| 40 |
-
profiles = [d for d in os.listdir(PROFILE_PATH) if os.path.isdir(os.path.join(PROFILE_PATH, d))]
|
| 41 |
-
rows = []
|
| 42 |
-
for p in profiles:
|
| 43 |
-
conf_path = os.path.join(PROFILE_PATH, p, "profile_config.json")
|
| 44 |
-
status = "๐ข Open" if p in active_drivers else "๐ด Closed"
|
| 45 |
-
rows.append([status, p])
|
| 46 |
-
return pd.DataFrame(rows, columns=["Status", "Name"])
|
| 47 |
-
|
| 48 |
-
def create_profile(name, device_idx):
|
| 49 |
-
if not name: return "โ ๏ธ แแผแแแแแ
แผแแแแแแ!", get_profile_list()
|
| 50 |
-
path = os.path.join(PROFILE_PATH, name)
|
| 51 |
-
if os.path.exists(path): return "โ ๏ธ แแถแแแแแแแแแแ แพแ!", get_profile_list()
|
| 52 |
-
os.makedirs(path)
|
| 53 |
-
preset = config["device_presets"][device_idx]
|
| 54 |
-
data = {"device_name": preset["name"], "device_user_agent": preset["ua"], "device_window_size": preset["size"]}
|
| 55 |
-
with open(os.path.join(path, "profile_config.json"), "w") as f:
|
| 56 |
-
json.dump(data, f, indent=4)
|
| 57 |
-
return f"โ
แแแแแพแ {name} แแแแแแ!", get_profile_list()
|
| 58 |
-
|
| 59 |
-
def open_chrome(name, target_url):
|
| 60 |
-
if not name: return "โ ๏ธ แแผแแแแแพแแแพแ Profile!", None
|
| 61 |
-
if not target_url: target_url = "https://www.google.com"
|
| 62 |
-
|
| 63 |
-
path = os.path.join(PROFILE_PATH, name)
|
| 64 |
-
with open(os.path.join(path, "profile_config.json"), "r") as f: conf = json.load(f)
|
| 65 |
-
|
| 66 |
-
options = Options()
|
| 67 |
-
options.binary_location = "/usr/bin/chromium"
|
| 68 |
-
options.add_argument("--headless=new")
|
| 69 |
-
options.add_argument("--no-sandbox")
|
| 70 |
-
options.add_argument("--disable-dev-shm-usage")
|
| 71 |
-
options.add_argument(f"user-agent={conf['device_user_agent']}")
|
| 72 |
-
options.add_argument(f"--window-size={conf['device_window_size']}")
|
| 73 |
-
options.add_argument(f"--user-data-dir={path}")
|
| 74 |
-
|
| 75 |
-
try:
|
| 76 |
-
if name not in active_drivers:
|
| 77 |
-
service = Service(executable_path="/usr/bin/chromedriver")
|
| 78 |
-
driver = webdriver.Chrome(service=service, options=options)
|
| 79 |
-
active_drivers[name] = driver
|
| 80 |
-
else:
|
| 81 |
-
driver = active_drivers[name]
|
| 82 |
-
|
| 83 |
-
driver.get(target_url)
|
| 84 |
-
time.sleep(3) # แแแแ
แถแ Loading
|
| 85 |
-
driver.save_screenshot(SCREENSHOT_PATH)
|
| 86 |
-
return f"โ
{name} แแแแปแแแ
แแถแแ: {target_url}", SCREENSHOT_PATH
|
| 87 |
-
except Exception as e:
|
| 88 |
-
return f"โ แแแ แปแ: {str(e)}", None
|
| 89 |
-
|
| 90 |
-
def capture_now(name):
|
| 91 |
-
if name in active_drivers:
|
| 92 |
-
active_drivers[name].save_screenshot(SCREENSHOT_PATH)
|
| 93 |
-
return "๐ธ แแแแถแแแแแแแ!", SCREENSHOT_PATH
|
| 94 |
-
return "โ ๏ธ Profile แแทแแแถแแแแพแแแ", None
|
| 95 |
-
|
| 96 |
-
def close_chrome(name):
|
| 97 |
-
if name in active_drivers:
|
| 98 |
-
active_drivers[name].quit()
|
| 99 |
-
del active_drivers[name]
|
| 100 |
-
return "โ
แแทแแแแแแแ"
|
| 101 |
-
return "โน๏ธ แแทแแแถแแแแพแแแ"
|
| 102 |
-
|
| 103 |
-
# --- UI ---
|
| 104 |
-
|
| 105 |
-
with gr.Blocks(title="Control Chromium", theme=gr.themes.Soft()) as demo:
|
| 106 |
-
gr.Markdown("# ๐น๏ธ Chromium Control Panel (Headless Vision)")
|
| 107 |
-
|
| 108 |
-
with gr.Row():
|
| 109 |
-
with gr.Column(scale=1):
|
| 110 |
-
gr.Markdown("### ๐ แแแแแธ Profile")
|
| 111 |
-
table = gr.Dataframe(value=get_profile_list(), interactive=False)
|
| 112 |
-
sel_name = gr.Textbox(label="Profile แแแแแแแพแแแพแ")
|
| 113 |
-
table.select(lambda e: e.value if e.index[1] == 1 else None, None, sel_name)
|
| 114 |
-
|
| 115 |
-
gr.Markdown("### โ แแแแแพแแแแแธ")
|
| 116 |
-
new_n = gr.Textbox(label="แแแแแ Profile")
|
| 117 |
-
new_d = gr.Dropdown(choices=[d["name"] for d in config["device_presets"]], type="index", label="Device", value=0)
|
| 118 |
-
c_btn = gr.Button("แแแแแพแ Profile")
|
| 119 |
-
|
| 120 |
-
with gr.Column(scale=2):
|
| 121 |
-
gr.Markdown("### ๐ฎ แแแแแถ Browser")
|
| 122 |
-
url_input = gr.Textbox(label="แแแแแถแแแแแ
แแแแ
(URL)", placeholder="https://facebook.com")
|
| 123 |
-
|
| 124 |
-
with gr.Row():
|
| 125 |
-
open_btn = gr.Button("๐ แแพแ/แแ
แแถแแ URL", variant="primary")
|
| 126 |
-
shot_btn = gr.Button("๐ธ แแแแผแแขแแแแแแ (Refresh View)")
|
| 127 |
-
exit_btn = gr.Button("๐ แแทแ Chrome", variant="stop")
|
| 128 |
-
|
| 129 |
-
status_log = gr.Textbox(label="Status Log")
|
| 130 |
-
screen_view = gr.Image(label="Browser Live View (Headless Screenshot)")
|
| 131 |
-
|
| 132 |
-
# Events
|
| 133 |
-
c_btn.click(create_profile, [new_n, new_d], [status_log, table])
|
| 134 |
-
|
| 135 |
-
open_btn.click(open_chrome, [sel_name, url_input], [status_log, screen_view])
|
| 136 |
-
|
| 137 |
-
shot_btn.click(capture_now, [sel_name], [status_log, screen_view])
|
| 138 |
-
|
| 139 |
-
exit_btn.click(close_chrome, [sel_name], [status_log]).then(get_profile_list, outputs=table)
|
| 140 |
-
|
| 141 |
-
if __name__ == "__main__":
|
| 142 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|