File size: 2,029 Bytes
de94ed3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191af2e
 
 
 
 
 
 
 
 
 
 
de94ed3
 
 
 
191af2e
 
 
 
 
 
 
 
 
de94ed3
191af2e
 
 
de94ed3
191af2e
 
 
 
 
 
 
de94ed3
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline

# Load your fine-tuned CamemBERT NER model
model_name = "GEODE/camembert-base-edda-span-classification"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForTokenClassification.from_pretrained(model_name)


ner_pipeline = pipeline("token-classification", model=model, tokenizer=tokenizer, aggregation_strategy="simple")

def extract_coordinates(text):
    # Run NER
    entities = ner_pipeline(text)
    
    # Collect coordinate entities
    coords_text = [ent['word'] for ent in entities if ent['entity_group'] == 'Latlong']
    
    if coords_text:
        return "\n".join(coords_text)
    else:
        return "No coordinates found"


examples = [
    "L’adresse est 48.8584, 2.2945 près de la Tour Eiffel.",
    "La latitude est 40.7128 et la longitude est -74.0060 pour New York.",
    "Les coordonnées du Colisée sont 41.8902, 12.4922.",
    "À Rio de Janeiro : -22.9068, -43.1729.",
    "Sydney se situe à -33.8688, 151.2093.",
    "Le Mont Fuji se trouve à 35.3606, 138.7274."
]


with gr.Blocks() as demo:
    gr.Markdown("## Geographic Coordinate Extractor (CamemBERT NER Demo)")
    
    with gr.Row():
        with gr.Column():
            inp = gr.Textbox(
                label="Enter text", 
                placeholder="e.g. * 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.",
                lines=3
            )

            run_btn = gr.Button("Extract & Show")
            run_btn.click(fn=extract_coordinates, inputs=inp, outputs=out_text)
    
        with gr.Column():
            out_text = gr.Textbox(label="Extracted coordinates")
            #out_map = gr.Map(label="Location on Map")
    
    with gr.Row():
        
        gr.Examples(
            examples=examples,
            inputs=inp,
            label="Examples"
        )

# Launch
if __name__ == "__main__":
    demo.launch()