SpatialWebAgent commited on
Commit
b3bfa70
·
verified ·
1 Parent(s): e6ee290

Update disambiguation/disambiguate.py

Browse files
Files changed (1) hide show
  1. disambiguation/disambiguate.py +34 -12
disambiguation/disambiguate.py CHANGED
@@ -18,25 +18,47 @@ import regex_spatial
18
  import streamlit as st
19
  urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
20
 
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
 
24
 
25
  def dismabiguate_entities(doc, ent, ase, level_1, level_2, level_3, midmid):
26
  return get_coordinates(ent, ase, level_1, level_2, level_3, midmid)
27
  def get_coordinates(ent, ase, level_1, level_2, level_3, midmid):
28
- request_url = 'https://nominatim.openstreetmap.org/search.php?q='+ase+'&polygon_geojson=1&accept-language=en&format=jsonv2'
29
- headers = {
30
- "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15"
31
- }
32
- time.sleep(2)
33
- page = requests.get(request_url, headers=headers, verify=False)
34
-
35
- json_content = json.loads(page.content)
36
- # json_content = json.loads(aa)
37
- print(json_content, 'jjjjj')
38
- all_coordinates = json_content[0]['geojson']['coordinates'][0]
39
- centroid = (float(json_content[0]['lon']), float(json_content[0]['lat']))
 
 
 
 
40
  for p in all_coordinates:
41
  p2 = (p[0], p[1])
42
  angle = geoutil.calculate_bearing(centroid, p2)
 
18
  import streamlit as st
19
  urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
20
 
21
+ BACKEND_URL = "https://SpatialWebAgent-dockerb22.hf.space/api/predict/"
22
 
23
+ import requests
24
+ def call_backend(input_text):
25
+ try:
26
+ headers = {
27
+ "Authorization": f"Bearer {API_TOKEN}"
28
+ }
29
+ response = requests.post(
30
+ BACKEND_URL,
31
+ headers=headers,
32
+ json={"data": [input_text]},
33
+ timeout=10
34
+ )
35
+ if response.status_code == 200:
36
+ return response.json() # ✅ 保留原始 JSON 对象 (dict)
37
+ return {"error": f"❌ Backend Error (HTTP {response.status_code})"}
38
+ except Exception as e:
39
+ return {"error": f"⚠️ Connection Error: {str(e)}"}
40
 
41
 
42
 
43
  def dismabiguate_entities(doc, ent, ase, level_1, level_2, level_3, midmid):
44
  return get_coordinates(ent, ase, level_1, level_2, level_3, midmid)
45
  def get_coordinates(ent, ase, level_1, level_2, level_3, midmid):
46
+ # request_url = 'https://nominatim.openstreetmap.org/search.php?q='+ase+'&polygon_geojson=1&accept-language=en&format=jsonv2'
47
+ # headers = {
48
+ # "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15"
49
+ # }
50
+ # time.sleep(2)
51
+ # page = requests.get(request_url, headers=headers, verify=False)
52
+
53
+ # json_content = json.loads(page.content)
54
+ # print(json_content, 'jjjjj')
55
+ # all_coordinates = json_content[0]['geojson']['coordinates'][0]
56
+ # centroid = (float(json_content[0]['lon']), float(json_content[0]['lat']))
57
+
58
+ res = call_backend(ase)
59
+ all_coordinates = res[0]
60
+ centroid = res[1]
61
+
62
  for p in all_coordinates:
63
  p2 = (p[0], p[1])
64
  angle = geoutil.calculate_bearing(centroid, p2)