initial commit with app and requirements
Browse files- app.py +71 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load the CodeT5 small model (lightweight)
|
| 5 |
+
model_name = "Salesforce/codet5-small"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
def fix_output(output):
|
| 10 |
+
# Simple cleanups and common fixes
|
| 11 |
+
output = output.replace("importsync_playwright", "import sync_playwright")
|
| 12 |
+
output = output.replace("\n\n", "\n")
|
| 13 |
+
lines = output.split("\n")
|
| 14 |
+
seen = set()
|
| 15 |
+
filtered = []
|
| 16 |
+
for line in lines:
|
| 17 |
+
if line.strip() not in seen:
|
| 18 |
+
filtered.append(line)
|
| 19 |
+
seen.add(line.strip())
|
| 20 |
+
output = "\n".join(filtered)
|
| 21 |
+
output = output.replace('.locator("//button[@type=\'submit\']").fill(', '.locator("//button[@type=\'submit\']").click(')
|
| 22 |
+
return output
|
| 23 |
+
|
| 24 |
+
def convert_selenium_to_playwright(selenium_code):
|
| 25 |
+
prompt = f"""
|
| 26 |
+
Convert the following Selenium Python code to Playwright Python code.
|
| 27 |
+
Return complete working Playwright code with proper syntax, including imports, browser launch, navigation, actions, and close.
|
| 28 |
+
|
| 29 |
+
Example:
|
| 30 |
+
|
| 31 |
+
Selenium:
|
| 32 |
+
from selenium import webdriver
|
| 33 |
+
from selenium.webdriver.common.by import By
|
| 34 |
+
driver = webdriver.Chrome()
|
| 35 |
+
driver.get("https://example.com")
|
| 36 |
+
element = driver.find_element(By.ID, "username")
|
| 37 |
+
element.send_keys("myusername")
|
| 38 |
+
driver.quit()
|
| 39 |
+
|
| 40 |
+
Playwright:
|
| 41 |
+
from playwright.sync_api import sync_playwright
|
| 42 |
+
|
| 43 |
+
with sync_playwright() as p:
|
| 44 |
+
browser = p.chromium.launch()
|
| 45 |
+
page = browser.new_page()
|
| 46 |
+
page.goto("https://example.com")
|
| 47 |
+
page.locator("#username").fill("myusername")
|
| 48 |
+
browser.close()
|
| 49 |
+
|
| 50 |
+
Now convert:
|
| 51 |
+
|
| 52 |
+
Selenium:
|
| 53 |
+
{selenium_code}
|
| 54 |
+
|
| 55 |
+
Playwright:
|
| 56 |
+
"""
|
| 57 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 58 |
+
outputs = model.generate(**inputs, max_length=512, num_beams=5, early_stopping=True)
|
| 59 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
+
return fix_output(result)
|
| 61 |
+
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=convert_selenium_to_playwright,
|
| 64 |
+
inputs=gr.Textbox(lines=15, placeholder="Paste your Selenium Python code here..."),
|
| 65 |
+
outputs="textbox",
|
| 66 |
+
title="Selenium to Playwright Code Converter",
|
| 67 |
+
description="Paste your Selenium Python code and get the equivalent Playwright Python code."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|