xpathHealing / app.py
sridharKikkeri's picture
Create app.py
44d5293 verified
raw
history blame
2.16 kB
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()