Spaces:
Running
Running
File size: 2,160 Bytes
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 52 53 54 55 56 57 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# Load a language model (e.g., Llama) for generating alternative XPath suggestions
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b") # Replace with the actual model
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b")
# Helper function to generate alternative XPaths
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:
"""
# Encode and generate with the model
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)
# Process the response to extract XPath suggestions
alternative_xpaths = response_text.strip().split('\n')
return alternative_xpaths
# Gradio interface function for self-healing XPath
def locate_element_with_self_healing(html_structure, original_xpath):
# Simulate the failure to find an element and ask for alternatives
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
}
# Define Gradio interface
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."
)
# Launch the Gradio interface
interface.launch()
|