sridharKikkeri commited on
Commit
75c45d2
·
verified ·
1 Parent(s): 44d5293

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -10
app.py CHANGED
@@ -2,11 +2,10 @@ 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.
@@ -18,18 +17,15 @@ def get_alternative_xpaths(html_structure, 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
 
@@ -38,7 +34,6 @@ def locate_element_with_self_healing(html_structure, 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=[
@@ -52,5 +47,4 @@ interface = gr.Interface(
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()
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
 
5
+ # Load a publicly accessible model
6
+ tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-2.7B")
7
+ model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-2.7B")
8
 
 
9
  def get_alternative_xpaths(html_structure, original_xpath):
10
  prompt = f"""
11
  Given the following HTML structure and an original XPath, generate alternative XPaths that could help locate the same element if it has changed position.
 
17
 
18
  Suggested alternative XPaths:
19
  """
20
+ # Generate response
21
  inputs = tokenizer(prompt, return_tensors="pt")
22
  outputs = model.generate(inputs['input_ids'], max_length=200, temperature=0.3)
23
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
 
 
25
  alternative_xpaths = response_text.strip().split('\n')
26
  return alternative_xpaths
27
 
 
28
  def locate_element_with_self_healing(html_structure, original_xpath):
 
29
  print("Original XPath failed. Attempting to heal...")
30
  alternative_xpaths = get_alternative_xpaths(html_structure, original_xpath)
31
 
 
34
  "alternative_xpaths": alternative_xpaths
35
  }
36
 
 
37
  interface = gr.Interface(
38
  fn=locate_element_with_self_healing,
39
  inputs=[
 
47
  description="This tool provides alternative XPaths if the original XPath is not found in the given HTML structure."
48
  )
49
 
 
50
  interface.launch()