sridharKikkeri commited on
Commit
44d5293
·
verified ·
1 Parent(s): 8f0a455

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # Load a language model (e.g., Llama) for generating alternative XPath suggestions
6
+ tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b") # Replace with the actual model
7
+ model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b")
8
+
9
+ # Helper function to generate alternative XPaths
10
+ def get_alternative_xpaths(html_structure, original_xpath):
11
+ prompt = f"""
12
+ Given the following HTML structure and an original XPath, generate alternative XPaths that could help locate the same element if it has changed position.
13
+
14
+ HTML Structure:
15
+ {html_structure}
16
+
17
+ Original XPath: {original_xpath}
18
+
19
+ Suggested alternative XPaths:
20
+ """
21
+ # Encode and generate with the model
22
+ inputs = tokenizer(prompt, return_tensors="pt")
23
+ outputs = model.generate(inputs['input_ids'], max_length=200, temperature=0.3)
24
+ response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
25
+
26
+ # Process the response to extract XPath suggestions
27
+ alternative_xpaths = response_text.strip().split('\n')
28
+ return alternative_xpaths
29
+
30
+ # Gradio interface function for self-healing XPath
31
+ def locate_element_with_self_healing(html_structure, original_xpath):
32
+ # Simulate the failure to find an element and ask for alternatives
33
+ print("Original XPath failed. Attempting to heal...")
34
+ alternative_xpaths = get_alternative_xpaths(html_structure, original_xpath)
35
+
36
+ return {
37
+ "original_xpath": original_xpath,
38
+ "alternative_xpaths": alternative_xpaths
39
+ }
40
+
41
+ # Define Gradio interface
42
+ interface = gr.Interface(
43
+ fn=locate_element_with_self_healing,
44
+ inputs=[
45
+ gr.Textbox(label="HTML Structure", placeholder="Paste the HTML structure here..."),
46
+ gr.Textbox(label="Original XPath", placeholder="//div[@id='submit-button']")
47
+ ],
48
+ outputs=[
49
+ gr.JSON(label="Healing Result")
50
+ ],
51
+ title="Self-Healing XPath API",
52
+ description="This tool provides alternative XPaths if the original XPath is not found in the given HTML structure."
53
+ )
54
+
55
+ # Launch the Gradio interface
56
+ interface.launch()