CesarLeblanc commited on
Commit
52efc69
·
verified ·
1 Parent(s): 1c03971

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -25
app.py CHANGED
@@ -9,35 +9,21 @@ classification_model = pipeline("text-classification", model="models/text_classi
9
  masking_model = pipeline("fill-mask", model="models/fill_mask_model", tokenizer="models/fill_mask_model", top_k=100)
10
 
11
  eunis_habitats = pd.read_excel('data/eunis_habitats.xlsx')
12
-
13
- def return_habitat_image(habitat_label):
14
- floraveg_url = f"https://floraveg.eu/habitat/overview/{habitat_label}"
15
- response = requests.get(floraveg_url)
16
- if response.status_code == 200:
17
- soup = BeautifulSoup(response.text, 'html.parser')
18
- img_tag = soup.find('img', src=lambda x: x and x.startswith("https://files.ibot.cas.cz/cevs/images/syntaxa/thumbs/"))
19
- if img_tag:
20
- image_url = img_tag['src']
21
- else:
22
- image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/2048px-No_image_available.svg.png"
23
- else:
24
- image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/2048px-No_image_available.svg.png"
25
- image = gr.Image(value=image_url)
26
- return image
27
 
28
- def return_species_image(species):
29
- species = species.capitalize()
30
- floraveg_url = f"https://floraveg.eu/taxon/overview/{species}"
 
 
 
 
 
31
  response = requests.get(floraveg_url)
32
  if response.status_code == 200:
33
  soup = BeautifulSoup(response.text, 'html.parser')
34
- img_tag = soup.find('img', src=lambda x: x and x.startswith("https://files.ibot.cas.cz/cevs/images/taxa/large/"))
35
  if img_tag:
36
  image_url = img_tag['src']
37
- else:
38
- image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/2048px-No_image_available.svg.png"
39
- else:
40
- image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/2048px-No_image_available.svg.png"
41
  image = gr.Image(value=image_url)
42
  return image
43
 
@@ -71,7 +57,7 @@ def classification(text, k):
71
  text = f"This vegetation plot probably belongs to the habitat type {', '.join(habitat_labels[:-1])}, or {habitat_labels[-1]}."
72
  text += f"\nThe most likely habitat type (i.e., {habitat_labels[0]}) is named '{habitat_name}'."
73
  text += f"\nSee an image of this habitat type below."
74
- image_output = return_habitat_image(habitat_labels[0])
75
  return text, image_output
76
 
77
  def masking(text, k):
@@ -125,7 +111,7 @@ def masking(text, k):
125
  text = f"The most likely missing species are {', '.join(best_predictions[:-1].capitalize())}, and {best_predictions[-1].capitalize()} (positions {', '.join(map(str, best_positions[:-1]))}, and {best_positions[-1]})."
126
  text += f"\nThe completed vegetation plot is thus '{best_sentence}'."
127
  text += f"\nSee an image of this species (i.e., {best_predictions[0].capitalize()}) below."
128
- image = return_species_image(best_predictions[0])
129
  return text, image
130
 
131
  with gr.Blocks() as demo:
 
9
  masking_model = pipeline("fill-mask", model="models/fill_mask_model", tokenizer="models/fill_mask_model", top_k=100)
10
 
11
  eunis_habitats = pd.read_excel('data/eunis_habitats.xlsx')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ def return_image(task, label):
14
+ image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/2048px-No_image_available.svg.png"
15
+ if task == "classification":
16
+ floraveg_url = f"https://floraveg.eu/habitat/overview/{label}"
17
+ floraveg_tag = "https://files.ibot.cas.cz/cevs/images/syntaxa/thumbs/"
18
+ elif task == "masking":
19
+ floraveg_url = f"https://floraveg.eu/taxon/overview/{label.capitalize()}"
20
+ floraveg_tag = "https://files.ibot.cas.cz/cevs/images/taxa/large/"
21
  response = requests.get(floraveg_url)
22
  if response.status_code == 200:
23
  soup = BeautifulSoup(response.text, 'html.parser')
24
+ img_tag = soup.find('img', src=lambda x: x and x.startswith(floraveg_tag))
25
  if img_tag:
26
  image_url = img_tag['src']
 
 
 
 
27
  image = gr.Image(value=image_url)
28
  return image
29
 
 
57
  text = f"This vegetation plot probably belongs to the habitat type {', '.join(habitat_labels[:-1])}, or {habitat_labels[-1]}."
58
  text += f"\nThe most likely habitat type (i.e., {habitat_labels[0]}) is named '{habitat_name}'."
59
  text += f"\nSee an image of this habitat type below."
60
+ image_output = return_image("classification", habitat_labels[0])
61
  return text, image_output
62
 
63
  def masking(text, k):
 
111
  text = f"The most likely missing species are {', '.join(best_predictions[:-1].capitalize())}, and {best_predictions[-1].capitalize()} (positions {', '.join(map(str, best_positions[:-1]))}, and {best_positions[-1]})."
112
  text += f"\nThe completed vegetation plot is thus '{best_sentence}'."
113
  text += f"\nSee an image of this species (i.e., {best_predictions[0].capitalize()}) below."
114
+ image = return_image("masking", best_predictions[0])
115
  return text, image
116
 
117
  with gr.Blocks() as demo: