Update app.py
Browse files
app.py
CHANGED
|
@@ -3,27 +3,43 @@ import asyncio
|
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
import gradio as gr
|
| 6 |
-
from playwright.async_api import async_playwright
|
| 7 |
-
from playwright_stealth import stealth
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 9 |
from threading import Thread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# --- AI Setup ---
|
| 12 |
-
MODEL_ID = "Qwen/Qwen2.5-3B-Instruct" # Small & Fast for ZeroGPU
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@spaces.GPU
|
| 17 |
def chat_fn(message, history):
|
|
|
|
| 18 |
messages = []
|
| 19 |
for user_msg, assistant_msg in history:
|
| 20 |
messages.append({"role": "user", "content": user_msg})
|
| 21 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 22 |
messages.append({"role": "user", "content": message})
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
thread.start()
|
| 28 |
|
| 29 |
response = ""
|
|
@@ -32,74 +48,59 @@ def chat_fn(message, history):
|
|
| 32 |
yield response
|
| 33 |
|
| 34 |
# --- Browser Logic ---
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
await
|
| 57 |
-
|
| 58 |
-
return await self.page.screenshot()
|
| 59 |
-
|
| 60 |
-
async def type_at(self, selector, text):
|
| 61 |
-
if self.page:
|
| 62 |
-
await self.page.fill(selector, text)
|
| 63 |
-
await self.page.keyboard.press("Enter")
|
| 64 |
-
await asyncio.sleep(2)
|
| 65 |
-
return await self.page.screenshot()
|
| 66 |
-
|
| 67 |
-
browser_mgr = BrowserManager()
|
| 68 |
|
| 69 |
-
def
|
| 70 |
-
|
| 71 |
-
def web_type(sel, txt): return asyncio.run(browser_mgr.type_at(sel, txt))
|
| 72 |
|
| 73 |
-
# --- UI
|
| 74 |
-
with gr.Blocks(
|
| 75 |
-
gr.
|
| 76 |
|
| 77 |
with gr.Row():
|
| 78 |
-
#
|
| 79 |
-
with gr.Column(
|
| 80 |
-
gr.ChatInterface(fn=chat_fn, type="messages")
|
| 81 |
-
|
| 82 |
-
# RIGHT: BROWSER
|
| 83 |
-
with gr.Column(scale=1):
|
| 84 |
-
url_input = gr.Textbox(label="URL", value="https://www.google.com")
|
| 85 |
-
nav_btn = gr.Button("🌐 Go / Refresh")
|
| 86 |
|
|
|
|
|
|
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
# Event Handlers
|
| 98 |
-
nav_btn.click(web_nav, inputs=url_input, outputs=browser_img)
|
| 99 |
-
click_btn.click(web_click, inputs=sel_input, outputs=browser_img)
|
| 100 |
-
type_btn.click(web_type, inputs=[sel_input, type_input], outputs=browser_img)
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
|
| 105 |
demo.launch()
|
|
|
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 7 |
from threading import Thread
|
| 8 |
+
from playwright.async_api import async_playwright
|
| 9 |
+
from playwright_stealth import stealth
|
| 10 |
+
|
| 11 |
+
# --- OS Setup: Playwright Install ---
|
| 12 |
+
os.system("playwright install chromium")
|
| 13 |
+
|
| 14 |
+
# --- AI Model Setup ---
|
| 15 |
+
# Hum Llama-3.2-1B use kar rahe hain taaki RAM crash na ho
|
| 16 |
+
MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 17 |
|
|
|
|
|
|
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 19 |
+
# Model ko CPU par load karenge, @spaces.GPU isse khud GPU par le jayega
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
MODEL_ID,
|
| 22 |
+
torch_dtype=torch.bfloat16,
|
| 23 |
+
device_map=None
|
| 24 |
+
).to("cpu")
|
| 25 |
|
| 26 |
@spaces.GPU
|
| 27 |
def chat_fn(message, history):
|
| 28 |
+
# Convert Gradio history to Llama format
|
| 29 |
messages = []
|
| 30 |
for user_msg, assistant_msg in history:
|
| 31 |
messages.append({"role": "user", "content": user_msg})
|
| 32 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
|
| 35 |
+
# Move model to GPU inside the decorated function
|
| 36 |
+
model.to("cuda")
|
| 37 |
+
|
| 38 |
+
input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to("cuda")
|
| 39 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 40 |
+
|
| 41 |
+
gen_kwargs = dict(input_ids=input_ids, streamer=streamer, max_new_tokens=512, temperature=0.7)
|
| 42 |
+
thread = Thread(target=model.generate, kwargs=gen_kwargs)
|
| 43 |
thread.start()
|
| 44 |
|
| 45 |
response = ""
|
|
|
|
| 48 |
yield response
|
| 49 |
|
| 50 |
# --- Browser Logic ---
|
| 51 |
+
async def browser_task(url, selector=None, text=None, action="nav"):
|
| 52 |
+
async with async_playwright() as p:
|
| 53 |
+
browser = await p.chromium.launch(headless=True)
|
| 54 |
+
context = await browser.new_context(viewport={'width': 1024, 'height': 768})
|
| 55 |
+
page = await context.new_page()
|
| 56 |
+
await stealth(page)
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
await page.goto(url, wait_until="networkidle", timeout=30000)
|
| 60 |
+
if action == "click" and selector:
|
| 61 |
+
await page.click(selector)
|
| 62 |
+
await asyncio.sleep(1)
|
| 63 |
+
elif action == "type" and selector and text:
|
| 64 |
+
await page.fill(selector, text)
|
| 65 |
+
await page.keyboard.press("Enter")
|
| 66 |
+
await asyncio.sleep(2)
|
| 67 |
+
|
| 68 |
+
screenshot = await page.screenshot()
|
| 69 |
+
await browser.close()
|
| 70 |
+
return screenshot
|
| 71 |
+
except Exception as e:
|
| 72 |
+
await browser.close()
|
| 73 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
def web_run(url, sel, txt, action):
|
| 76 |
+
return asyncio.run(browser_task(url, sel, txt, action))
|
|
|
|
| 77 |
|
| 78 |
+
# --- Gradio UI ---
|
| 79 |
+
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("## Dual AI: Smart Chat + Stealth Browser")
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
+
# Left side: Chat
|
| 84 |
+
with gr.Column():
|
| 85 |
+
chatbot = gr.ChatInterface(fn=chat_fn, type="messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# Right side: Browser
|
| 88 |
+
with gr.Column():
|
| 89 |
+
url_box = gr.Textbox(label="URL", value="https://www.google.com")
|
| 90 |
with gr.Row():
|
| 91 |
+
sel_box = gr.Textbox(label="Selector", placeholder="#id or .class")
|
| 92 |
+
txt_box = gr.Textbox(label="Text to type")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
+
btn_go = gr.Button("🌐 Visit")
|
| 96 |
+
btn_click = gr.Button("🖱️ Click")
|
| 97 |
+
btn_type = gr.Button("⌨️ Type")
|
| 98 |
+
|
| 99 |
+
browser_output = gr.Image(label="Browser View")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
# Browser Button Events
|
| 102 |
+
btn_go.click(fn=lambda u: web_run(u, None, None, "nav"), inputs=url_box, outputs=browser_output)
|
| 103 |
+
btn_click.click(fn=lambda u, s: web_run(u, s, None, "click"), inputs=[url_box, sel_box], outputs=browser_output)
|
| 104 |
+
btn_type.click(fn=lambda u, s, t: web_run(u, s, t, "type"), inputs=[url_box, sel_box, txt_box], outputs=browser_output)
|
| 105 |
|
| 106 |
demo.launch()
|