File size: 1,783 Bytes
44d5293
 
 
 
75c45d2
 
 
44d5293
 
 
 
 
 
 
 
 
 
 
 
75c45d2
44d5293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# Load a publicly accessible model
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-2.7B")
model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-2.7B")

def get_alternative_xpaths(html_structure, original_xpath):
    prompt = f"""
    Given the following HTML structure and an original XPath, generate alternative XPaths that could help locate the same element if it has changed position.

    HTML Structure:
    {html_structure}

    Original XPath: {original_xpath}

    Suggested alternative XPaths:
    """
    # Generate response
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(inputs['input_ids'], max_length=200, temperature=0.3)
    response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)

    alternative_xpaths = response_text.strip().split('\n')
    return alternative_xpaths

def locate_element_with_self_healing(html_structure, original_xpath):
    print("Original XPath failed. Attempting to heal...")
    alternative_xpaths = get_alternative_xpaths(html_structure, original_xpath)
    
    return {
        "original_xpath": original_xpath,
        "alternative_xpaths": alternative_xpaths
    }

interface = gr.Interface(
    fn=locate_element_with_self_healing,
    inputs=[
        gr.Textbox(label="HTML Structure", placeholder="Paste the HTML structure here..."),
        gr.Textbox(label="Original XPath", placeholder="//div[@id='submit-button']")
    ],
    outputs=[
        gr.JSON(label="Healing Result")
    ],
    title="Self-Healing XPath API",
    description="This tool provides alternative XPaths if the original XPath is not found in the given HTML structure."
)

interface.launch()