Spaces:
Runtime error
Runtime error
CesarLeblanc
commited on
Commit
•
544f914
1
Parent(s):
c8844e9
app.py
CHANGED
@@ -2,10 +2,14 @@ import gradio as gr
|
|
2 |
from transformers import pipeline
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
|
|
5 |
|
6 |
# Initialize models
|
7 |
classification_model = pipeline("text-classification", model="models/text_classification_model", tokenizer="models/text_classification_model", top_k=5)
|
8 |
mask_model = pipeline("fill-mask", model="models/fill_mask_model", tokenizer="models/fill_mask_model", top_k=100)
|
|
|
|
|
|
|
9 |
|
10 |
def return_habitat_image(habitat_label):
|
11 |
floraveg_url = f"https://floraveg.eu/habitat/overview/{habitat_label}"
|
@@ -70,6 +74,8 @@ def classification(text, k):
|
|
70 |
text = f"This vegetation plot belongs to the habitat {habitat_labels[0]}."
|
71 |
else:
|
72 |
text = f"This vegetation plot belongs to the habitats {', '.join(habitat_labels[:-1])} and {habitat_labels[-1]}."
|
|
|
|
|
73 |
image_output = return_habitat_image(habitat_labels[0])
|
74 |
return text, image_output
|
75 |
|
@@ -82,9 +88,7 @@ def masking(text):
|
|
82 |
best_position = None
|
83 |
best_sentence = None
|
84 |
|
85 |
-
# Loop through each position in the sentence
|
86 |
for i in range(len(text_split) + 1):
|
87 |
-
# Create masked text
|
88 |
masked_text = ', '.join(text_split[:i] + ['[MASK]'] + text_split[i:])
|
89 |
|
90 |
j = 0
|
@@ -99,7 +103,6 @@ def masking(text):
|
|
99 |
score = prediction['score']
|
100 |
sentence = prediction['sequence']
|
101 |
|
102 |
-
# Update best prediction and position if score is higher
|
103 |
if score > max_score:
|
104 |
max_score = score
|
105 |
best_prediction = species
|
@@ -119,7 +122,7 @@ with gr.Blocks() as demo:
|
|
119 |
with gr.Row():
|
120 |
with gr.Column():
|
121 |
species = gr.Textbox(lines=2, label="Species", placeholder="Enter a list of comma-separated binomial names here.")
|
122 |
-
top_k = gr.Slider(1, 5, value=1, label="Top-k", info="Choose
|
123 |
with gr.Column():
|
124 |
text_output_1 = gr.Textbox()
|
125 |
text_output_2 = gr.Image()
|
@@ -138,7 +141,7 @@ with gr.Blocks() as demo:
|
|
138 |
gr.Markdown("""<h5 style="text-align: center;">An example of input</h5>""")
|
139 |
gr.Examples([["vaccinium myrtillus, dryopteris dilatata, molinia caerulea"]], [species_2], [image_output_1, image_output_2], masking, True)
|
140 |
|
141 |
-
text_button.click(classification, inputs=[species], outputs=[text_output_1, text_output_2])
|
142 |
image_button.click(masking, inputs=[species_2], outputs=[image_output_1, image_output_2])
|
143 |
|
144 |
demo.launch()
|
|
|
2 |
from transformers import pipeline
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
5 |
+
import pandas as pd
|
6 |
|
7 |
# Initialize models
|
8 |
classification_model = pipeline("text-classification", model="models/text_classification_model", tokenizer="models/text_classification_model", top_k=5)
|
9 |
mask_model = pipeline("fill-mask", model="models/fill_mask_model", tokenizer="models/fill_mask_model", top_k=100)
|
10 |
+
|
11 |
+
# Load data
|
12 |
+
eunis_habitats = pd.read_excel('data/eunis_habitats.xlsx')
|
13 |
|
14 |
def return_habitat_image(habitat_label):
|
15 |
floraveg_url = f"https://floraveg.eu/habitat/overview/{habitat_label}"
|
|
|
74 |
text = f"This vegetation plot belongs to the habitat {habitat_labels[0]}."
|
75 |
else:
|
76 |
text = f"This vegetation plot belongs to the habitats {', '.join(habitat_labels[:-1])} and {habitat_labels[-1]}."
|
77 |
+
habitat_name = eunis_habitats[eunis_habitats['EUNIS 2020 code'] == habitat_labels[0]]['EUNIS-2021 habitat name'].values[0]
|
78 |
+
text += f"\nThe most likely habitat is {habitat_name} (see image below)."
|
79 |
image_output = return_habitat_image(habitat_labels[0])
|
80 |
return text, image_output
|
81 |
|
|
|
88 |
best_position = None
|
89 |
best_sentence = None
|
90 |
|
|
|
91 |
for i in range(len(text_split) + 1):
|
|
|
92 |
masked_text = ', '.join(text_split[:i] + ['[MASK]'] + text_split[i:])
|
93 |
|
94 |
j = 0
|
|
|
103 |
score = prediction['score']
|
104 |
sentence = prediction['sequence']
|
105 |
|
|
|
106 |
if score > max_score:
|
107 |
max_score = score
|
108 |
best_prediction = species
|
|
|
122 |
with gr.Row():
|
123 |
with gr.Column():
|
124 |
species = gr.Textbox(lines=2, label="Species", placeholder="Enter a list of comma-separated binomial names here.")
|
125 |
+
top_k = gr.Slider(1, 5, value=1, label="Top-k", info="Choose the number of top habitats to display.")
|
126 |
with gr.Column():
|
127 |
text_output_1 = gr.Textbox()
|
128 |
text_output_2 = gr.Image()
|
|
|
141 |
gr.Markdown("""<h5 style="text-align: center;">An example of input</h5>""")
|
142 |
gr.Examples([["vaccinium myrtillus, dryopteris dilatata, molinia caerulea"]], [species_2], [image_output_1, image_output_2], masking, True)
|
143 |
|
144 |
+
text_button.click(classification, inputs=[species, top_k], outputs=[text_output_1, text_output_2])
|
145 |
image_button.click(masking, inputs=[species_2], outputs=[image_output_1, image_output_2])
|
146 |
|
147 |
demo.launch()
|