crcdng commited on
Commit
884e0b8
·
1 Parent(s): 0148bd4

asteroid factor

Browse files
Files changed (2) hide show
  1. app.py +37 -23
  2. requirements.txt +0 -1
app.py CHANGED
@@ -1,29 +1,37 @@
 
 
1
  import gradio as gr
2
- import orjson
3
  import os
4
  import re
5
  import requests
6
 
7
- NASA_API_KEY = os.getenv("NASA_API_KEY")
8
 
9
- def calc_asteroid_factor(json, weight=1.0):
 
10
  """
11
- Calculates the astoriod doom probability.
12
 
13
  Args:
14
- json (object): The data structure returned frm NASA
15
  weight (float): The weight to apply to the hazard factor.
16
 
17
  Returns:
18
  Weighted factor for Near Earth Object impact for the next seven days.
19
  """
20
- # vibe coded, replace with real code later
21
- matches = re.findall(r'"is_potentially_hazardous_asteroid"\s*:\s*(true|false)', json)
22
- hazard_list = list(filter(lambda x: x == "true", matches))
23
- return (len(hazard_list)/len(matches) * weight) # Calculate the hazard factor as a ratio of hazardous to total asteroids
24
 
 
 
 
 
 
 
 
 
 
25
 
26
- def fetch_nasa():
 
27
  """
28
  Fetches data from the NASA Near Earth Object Web Service.
29
 
@@ -31,22 +39,18 @@ def fetch_nasa():
31
  Near Earth Objects for the next seven days.
32
  """
33
 
34
- url = "https://api.nasa.gov/neo/rest/v1/feed"
35
-
36
- rawdata = requests.get(
37
  url,
38
- params={"api_key": NASA_API_KEY},
39
  )
40
 
41
- if rawdata.status_code != 200:
42
- return "Error fetching data from NASA API."
 
 
43
 
44
- json = orjson.loads(rawdata.content)
45
-
46
- return json
47
 
48
-
49
- def calc_factor():
50
  """
51
  Calculates the overall doom probability.
52
 
@@ -54,7 +58,17 @@ def calc_factor():
54
  The overall doom probability for the next seven days.
55
  """
56
 
57
- return calc_asteroid_factor(fetch_nasa(), weight=1.0)
 
 
 
 
 
 
 
 
 
 
58
 
59
- demo = gr.Interface(fn=calc_factor, inputs=None, outputs="text")
60
  demo.launch(mcp_server=True)
 
1
+ # -*- coding: utf-8 -*-
2
+
3
  import gradio as gr
4
+ import json
5
  import os
6
  import re
7
  import requests
8
 
 
9
 
10
+
11
+ def calc_asteroid_factor(data, weight=1.0):
12
  """
13
+ Calculates the asteroid doom probability.
14
 
15
  Args:
16
+ data (object): The data structure returned from NASA
17
  weight (float): The weight to apply to the hazard factor.
18
 
19
  Returns:
20
  Weighted factor for Near Earth Object impact for the next seven days.
21
  """
 
 
 
 
22
 
23
+ objects = [obj for sublist in data["near_earth_objects"].values() for obj in sublist]
24
+
25
+ num_objects = len(objects)
26
+
27
+ hazardous_objects = [obj for sublist in data["near_earth_objects"].values() for obj in sublist if obj["is_potentially_hazardous_asteroid"] == True]
28
+
29
+ num_hazardous_objects = len(hazardous_objects)
30
+
31
+ return (num_hazardous_objects/num_objects * weight) # hazard factor
32
 
33
+
34
+ def fetch_asteroid_data(url, api_key):
35
  """
36
  Fetches data from the NASA Near Earth Object Web Service.
37
 
 
39
  Near Earth Objects for the next seven days.
40
  """
41
 
42
+ request_data = requests.get(
 
 
43
  url,
44
+ params={"api_key": api_key},
45
  )
46
 
47
+ if request_data.status_code != 200:
48
+ return None
49
+ else:
50
+ return json.loads(request_data.content)
51
 
 
 
 
52
 
53
+ def calc_doom_probability():
 
54
  """
55
  Calculates the overall doom probability.
56
 
 
58
  The overall doom probability for the next seven days.
59
  """
60
 
61
+ nasa_url = "https://api.nasa.gov/neo/rest/v1/feed"
62
+ nasa_api_key = os.getenv("NASA_API_KEY")
63
+
64
+ if not nasa_api_key:
65
+ return "ERROR: NASA_API_KEY not set."
66
+ else:
67
+ nasa_data = fetch_asteroid_data(nasa_url, nasa_api_key)
68
+ if not nasa_data:
69
+ return "ERROR: Unable to fetch data from NASA API."
70
+ else:
71
+ return calc_asteroid_factor(nasa_data, weight=1.0)
72
 
73
+ demo = gr.Interface(fn=calc_doom_probability, inputs=None, outputs="text")
74
  demo.launch(mcp_server=True)
requirements.txt DELETED
@@ -1 +0,0 @@
1
- orjson