makeitfr commited on
Commit
f992ea3
·
verified ·
1 Parent(s): 90ca42e

Upload ui_element_client.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ui_element_client.py +38 -12
ui_element_client.py CHANGED
@@ -20,10 +20,16 @@ class UIElementDetectionClient:
20
  def health_check(self):
21
  """Check if API is running."""
22
  try:
23
- response = requests.get(f"{self.api_url}/health", timeout=5)
 
24
  return response.json()
25
- except Exception as e:
26
- return {"error": str(e)}
 
 
 
 
 
27
 
28
  def analyze_image(self, image_path):
29
  """
@@ -39,15 +45,28 @@ class UIElementDetectionClient:
39
  raise FileNotFoundError(f"Image not found: {image_path}")
40
 
41
  print(f"[Client] Analyzing: {image_path}")
42
- print(f"[Client] Uploading to {self.api_url}/analyze...")
 
 
 
43
 
44
  with open(image_path, 'rb') as f:
45
  files = {'file': f}
46
- response = requests.post(
47
- f"{self.api_url}/analyze",
48
- files=files,
49
- timeout=300
50
- )
 
 
 
 
 
 
 
 
 
 
51
 
52
  if response.status_code != 200:
53
  raise Exception(f"API error: {response.status_code} - {response.text}")
@@ -143,7 +162,7 @@ def main():
143
  result = client.analyze_image(args.image)
144
  element = None
145
  for elem in result['analysis']['elements']:
146
- if elem['template_id'] == args.element:
147
  element = elem
148
  break
149
 
@@ -161,7 +180,10 @@ def main():
161
  elements = client.find_elements_in_region(args.image, *args.region)
162
  print(f"\n[Found {len(elements)} elements in region {args.region}]")
163
  for elem in elements:
164
- print(f" - {elem['template_id']} @ ({elem['center']['x']}, {elem['center']['y']})")
 
 
 
165
 
166
  else:
167
  # Full analysis
@@ -169,7 +191,11 @@ def main():
169
 
170
  print(f"\n[Top 5 Elements by Confidence]")
171
  for i, elem in enumerate(result['analysis']['elements'][:5], 1):
172
- print(f" {i}. {elem['template_id']} @ ({elem['center']['x']}, {elem['center']['y']}) - {elem['confidence']:.4f}")
 
 
 
 
173
 
174
  except Exception as e:
175
  print(f"[ERROR] {str(e)}")
 
20
  def health_check(self):
21
  """Check if API is running."""
22
  try:
23
+ # Try new API endpoint first
24
+ response = requests.get(f"{self.api_url}/api/health", timeout=5)
25
  return response.json()
26
+ except Exception:
27
+ # Fallback to old endpoint
28
+ try:
29
+ response = requests.get(f"{self.api_url}/health", timeout=5)
30
+ return response.json()
31
+ except Exception as e:
32
+ return {"error": str(e)}
33
 
34
  def analyze_image(self, image_path):
35
  """
 
45
  raise FileNotFoundError(f"Image not found: {image_path}")
46
 
47
  print(f"[Client] Analyzing: {image_path}")
48
+
49
+ # Try new API endpoint first
50
+ api_endpoint = f"{self.api_url}/api/analyze"
51
+ print(f"[Client] Uploading to {api_endpoint}...")
52
 
53
  with open(image_path, 'rb') as f:
54
  files = {'file': f}
55
+ try:
56
+ response = requests.post(
57
+ api_endpoint,
58
+ files=files,
59
+ timeout=300
60
+ )
61
+ except Exception:
62
+ # Fallback to old endpoint
63
+ api_endpoint = f"{self.api_url}/analyze"
64
+ print(f"[Client] Retrying with {api_endpoint}...")
65
+ response = requests.post(
66
+ api_endpoint,
67
+ files=files,
68
+ timeout=300
69
+ )
70
 
71
  if response.status_code != 200:
72
  raise Exception(f"API error: {response.status_code} - {response.text}")
 
162
  result = client.analyze_image(args.image)
163
  element = None
164
  for elem in result['analysis']['elements']:
165
+ if elem.get('element_id') == args.element or elem.get('template_id') == args.element:
166
  element = elem
167
  break
168
 
 
180
  elements = client.find_elements_in_region(args.image, *args.region)
181
  print(f"\n[Found {len(elements)} elements in region {args.region}]")
182
  for elem in elements:
183
+ elem_id = elem.get('element_id') or elem.get('template_id', 'unknown')
184
+ x = elem.get('x') or elem.get('center', {}).get('x', 0)
185
+ y = elem.get('y') or elem.get('center', {}).get('y', 0)
186
+ print(f" - {elem_id} @ ({x}, {y})")
187
 
188
  else:
189
  # Full analysis
 
191
 
192
  print(f"\n[Top 5 Elements by Confidence]")
193
  for i, elem in enumerate(result['analysis']['elements'][:5], 1):
194
+ elem_id = elem.get('element_id') or elem.get('template_id', f"elem_{i}")
195
+ x = elem.get('x') or elem.get('center', {}).get('x', 0)
196
+ y = elem.get('y') or elem.get('center', {}).get('y', 0)
197
+ conf = elem.get('confidence', 0)
198
+ print(f" {i}. {elem_id} @ ({x}, {y}) - {conf:.4f}")
199
 
200
  except Exception as e:
201
  print(f"[ERROR] {str(e)}")