Wall06 commited on
Commit
c8f566e
·
verified ·
1 Parent(s): fb5a224

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -2,8 +2,7 @@ import gradio as gr
2
  import requests
3
  import pandas as pd
4
  import plotly.express as px
5
- import qrcode
6
- from io import BytesIO
7
 
8
  # ================================================================
9
  # Helper Function
@@ -34,20 +33,24 @@ def process_species_data(species_name, habitat, water_disturbance, land_disturba
34
  except:
35
  extract = "Error fetching data."
36
 
 
37
  words = extract.split()
38
  if len(words) < 200:
39
  extract += "\n\n" + ("Conservation of this species is critical for biodiversity. " * 5)
40
 
41
  summary = " ".join(words[:350])
42
 
43
- summary += f"""
44
-
45
  Environmental Impact Assessment:
46
  - Water Disturbance: {water_disturbance}/100
47
  - Land Disturbance: {land_disturbance}/100
48
  - Noise Level: {noise_level}/100
49
  - Habitat: {habitat}
 
 
50
  """
 
51
 
52
  # 2. Fetch GBIF Map
53
  gbif_url = f"https://api.gbif.org/v1/occurrence/search?scientificName={species_name}&limit=200"
@@ -68,10 +71,17 @@ Environmental Impact Assessment:
68
  fig = px.scatter_mapbox(pd.DataFrame({"lat":[], "lon":[]}), lat="lat", lon="lon", zoom=1)
69
  fig.update_layout(mapbox_style="open-street-map")
70
 
71
- # 3. QR Code
72
- qr = qrcode.make(f"BioVigilus: {species_name}")
 
 
 
 
 
 
73
 
74
- return summary, fig, qr
 
75
 
76
  # ================================================================
77
  # CSS Styling (HTML Injection)
@@ -86,12 +96,11 @@ h1 { color: #2e7d32; text-align: center; }
86
  """
87
 
88
  # ================================================================
89
- # UI Layout (Strict Compatibility Mode)
90
  # ================================================================
91
 
92
- # NOTE: Removed 'theme' and 'css' arguments to prevent errors on old versions
93
  with gr.Blocks() as demo:
94
- gr.HTML(css_code) # Inject CSS here instead
95
 
96
  gr.Markdown("# 🌿 BioVigilus — Biodiversity Impact Analyzer")
97
  gr.Markdown("Analyze species, habitats, and environmental stress factors.")
@@ -108,9 +117,10 @@ with gr.Blocks() as demo:
108
  with gr.Column(elem_classes="custom-box"):
109
  out_summary = gr.Textbox(label="Summary", lines=10)
110
  out_map = gr.Plot(label="Map")
111
- out_qr = gr.Image(label="QR Code", type="pil")
 
112
 
113
- btn.click(process_species_data, [species_name, habitat, water, land, noise], [out_summary, out_map, out_qr])
114
 
115
  if __name__ == "__main__":
116
  demo.launch()
 
2
  import requests
3
  import pandas as pd
4
  import plotly.express as px
5
+ import os
 
6
 
7
  # ================================================================
8
  # Helper Function
 
33
  except:
34
  extract = "Error fetching data."
35
 
36
+ # Pad text if too short
37
  words = extract.split()
38
  if len(words) < 200:
39
  extract += "\n\n" + ("Conservation of this species is critical for biodiversity. " * 5)
40
 
41
  summary = " ".join(words[:350])
42
 
43
+ # Add Analysis
44
+ analysis_text = f"""
45
  Environmental Impact Assessment:
46
  - Water Disturbance: {water_disturbance}/100
47
  - Land Disturbance: {land_disturbance}/100
48
  - Noise Level: {noise_level}/100
49
  - Habitat: {habitat}
50
+
51
+ Higher disturbance values indicate increased pressure on this species' survival and reproduction.
52
  """
53
+ summary += analysis_text
54
 
55
  # 2. Fetch GBIF Map
56
  gbif_url = f"https://api.gbif.org/v1/occurrence/search?scientificName={species_name}&limit=200"
 
71
  fig = px.scatter_mapbox(pd.DataFrame({"lat":[], "lon":[]}), lat="lat", lon="lon", zoom=1)
72
  fig.update_layout(mapbox_style="open-street-map")
73
 
74
+ # 3. Create Downloadable Report File
75
+ filename = "BioVigilus_Report.txt"
76
+ with open(filename, "w", encoding="utf-8") as f:
77
+ f.write(f"=== BioVigilus Biodiversity Report ===\n\n")
78
+ f.write(f"Species: {species_name}\n")
79
+ f.write(f"Source: Wikipedia & GBIF Data\n")
80
+ f.write(f"--------------------------------------\n")
81
+ f.write(summary)
82
 
83
+ # Return path to the file for download
84
+ return summary, fig, filename
85
 
86
  # ================================================================
87
  # CSS Styling (HTML Injection)
 
96
  """
97
 
98
  # ================================================================
99
+ # UI Layout
100
  # ================================================================
101
 
 
102
  with gr.Blocks() as demo:
103
+ gr.HTML(css_code)
104
 
105
  gr.Markdown("# 🌿 BioVigilus — Biodiversity Impact Analyzer")
106
  gr.Markdown("Analyze species, habitats, and environmental stress factors.")
 
117
  with gr.Column(elem_classes="custom-box"):
118
  out_summary = gr.Textbox(label="Summary", lines=10)
119
  out_map = gr.Plot(label="Map")
120
+ # Changed from Image (QR) to File (Download)
121
+ out_file = gr.File(label="Download Report")
122
 
123
+ btn.click(process_species_data, [species_name, habitat, water, land, noise], [out_summary, out_map, out_file])
124
 
125
  if __name__ == "__main__":
126
  demo.launch()