Spaces:
Sleeping
Sleeping
Ludovic Moncla
commited on
Commit
·
7cb1dfd
1
Parent(s):
11d4c17
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
| 3 |
|
|
@@ -16,10 +17,18 @@ def extract_coordinates(text):
|
|
| 16 |
# Collect coordinate entities
|
| 17 |
coords_text = [ent['word'] for ent in entities if ent['entity_group'] == 'Latlong']
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
def norm_coordinates(text):
|
|
@@ -34,6 +43,19 @@ def norm_coordinates(text):
|
|
| 34 |
|
| 35 |
return predicted_coordinates_from_pipeline[0]['generated_text']
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
examples = [
|
| 38 |
"* AACH ou ACH, s. f. petite ville d'Allemagne dans le cercle de Souabe, près de la source de l'Aach. Long. 26. 57. lat. 47. 55.",
|
| 39 |
"* ARCALU (Principauté d') petit état des Tartares-Monguls, sur la riviere d'Hoamko, où commence la grande muraille de la Chine, sous le 122e degré de longitude & le 42e de latitude septentrionale.",
|
|
@@ -63,7 +85,9 @@ with gr.Blocks() as demo:
|
|
| 63 |
run_btn.click(fn=extract_coordinates, inputs=inp, outputs=out_text)
|
| 64 |
|
| 65 |
out_text = gr.Textbox(label="Extracted and normalized DMS coordinates (fine-tuned mT5)")
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
with gr.Row():
|
| 69 |
|
|
|
|
| 1 |
+
import re
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
| 4 |
|
|
|
|
| 17 |
# Collect coordinate entities
|
| 18 |
coords_text = [ent['word'] for ent in entities if ent['entity_group'] == 'Latlong']
|
| 19 |
|
| 20 |
+
parsed_coords = []
|
| 21 |
+
for coord_str in coords_text:
|
| 22 |
+
matches = dms_pattern.findall(coord_str)
|
| 23 |
+
if len(matches) >= 2: # Expecting lat + lon
|
| 24 |
+
lat_match, lon_match = matches[0], matches[1]
|
| 25 |
+
lat = dms_to_dd(*lat_match)
|
| 26 |
+
lon = dms_to_dd(*lon_match)
|
| 27 |
+
parsed_coords.append([lat, lon])
|
| 28 |
+
|
| 29 |
+
result_text = "\n".join(coords_text) if coords_text else "No coordinates found"
|
| 30 |
+
return result_text, parsed_coords if parsed_coords else None
|
| 31 |
+
|
| 32 |
|
| 33 |
|
| 34 |
def norm_coordinates(text):
|
|
|
|
| 43 |
|
| 44 |
return predicted_coordinates_from_pipeline[0]['generated_text']
|
| 45 |
|
| 46 |
+
# Function to convert DMS to decimal
|
| 47 |
+
def dms_to_dd(degrees, minutes, seconds, direction):
|
| 48 |
+
dd = float(degrees) + float(minutes)/60 + float(seconds)/3600
|
| 49 |
+
if direction in ["S", "W"]:
|
| 50 |
+
dd *= -1
|
| 51 |
+
return dd
|
| 52 |
+
|
| 53 |
+
# Regex for DMS coordinates (example: 48°51′29.9″N or 2°17′40.2″E)
|
| 54 |
+
dms_pattern = re.compile(
|
| 55 |
+
r"(\d{1,3})[°\s]\s*(\d{1,2})[′']\s*(\d{1,2}(?:\.\d+)?)[″\"]?\s*([NSEW])"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
examples = [
|
| 60 |
"* AACH ou ACH, s. f. petite ville d'Allemagne dans le cercle de Souabe, près de la source de l'Aach. Long. 26. 57. lat. 47. 55.",
|
| 61 |
"* ARCALU (Principauté d') petit état des Tartares-Monguls, sur la riviere d'Hoamko, où commence la grande muraille de la Chine, sous le 122e degré de longitude & le 42e de latitude septentrionale.",
|
|
|
|
| 85 |
run_btn.click(fn=extract_coordinates, inputs=inp, outputs=out_text)
|
| 86 |
|
| 87 |
out_text = gr.Textbox(label="Extracted and normalized DMS coordinates (fine-tuned mT5)")
|
| 88 |
+
out_map = gr.Map(label="Coordinates on Map")
|
| 89 |
+
|
| 90 |
+
run_btn.click(fn=norm_coordinates, inputs=inp, outputs=[out_text, out_map])
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
|