SHAMIL SHAHBAZ AWAN commited on
Commit
690d926
Β·
verified Β·
1 Parent(s): 1d39d7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -39,22 +39,31 @@ def space_events_agent():
39
  Fetches and formats recent space-related events from NASA's EONET API.
40
 
41
  Returns:
42
- dict: A dictionary with a well-structured summary of key space-related events.
43
  """
44
  import os
45
  import requests
46
- from datetime import datetime
47
 
48
  base_url = "https://eonet.gsfc.nasa.gov/api/v2.1/events"
49
- params = {"api_key": os.getenv("NASA_API_KEY")}
 
 
 
 
 
50
 
51
  try:
52
- response = requests.get(base_url, params=params, timeout=10) # Set a timeout to avoid long waits
53
  response.raise_for_status()
 
 
 
 
 
54
  except requests.exceptions.RequestException as e:
55
  return {"output": f"πŸš€ **Space Events Agent:** Unable to fetch data. Error: {str(e)}"}
56
 
57
- events = response.json().get("events", [])
58
 
59
  # Filter space-related events
60
  space_categories = ["meteor", "asteroid", "space debris", "solar activity", "geomagnetic storm", "comet"]
@@ -64,29 +73,27 @@ def space_events_agent():
64
  ]
65
 
66
  if not space_related:
67
- return {"output": "πŸš€ **Space Events Agent:** No major space-related events at the moment. Stay tuned for upcoming discoveries!"}
68
 
69
  # Sort events by date (latest first)
70
  space_related = sorted(space_related, key=lambda e: e.get("geometries", [{}])[0].get("date", ""), reverse=True)
71
 
72
- # Format detailed event response
73
  formatted_events = []
74
- for event in space_related[:5]: # Limit to top 5 events
75
  title = event.get('title', 'Unknown Event')
76
  category = event.get('categories', [{}])[0].get('title', 'Unknown')
77
  geometries = event.get('geometries', [])
78
  event_date = geometries[0].get('date', 'Unknown Date') if geometries else 'Unknown Date'
79
  location = geometries[0].get('coordinates', 'Unknown Location') if geometries else 'Unknown Location'
80
 
81
- # Add additional insights based on event type
82
- if "asteroid" in category.lower():
83
- insight = "πŸ”­ A newly discovered asteroid is passing near Earth! Scientists are tracking its trajectory for potential close approaches."
84
- elif "solar" in category.lower():
85
- insight = "β˜€οΈ A strong solar flare has been detected, possibly affecting Earth's geomagnetic field and satellite communications."
86
- elif "meteor" in category.lower():
87
- insight = "🌠 A spectacular meteor shower is expected to light up the skies. Best viewing times will depend on your location."
88
- else:
89
- insight = "πŸš€ Scientists are analyzing this event for its potential impact on space exploration and planetary science."
90
 
91
  formatted_events.append(
92
  f"πŸ”­ **{title}**\n"
@@ -100,6 +107,7 @@ def space_events_agent():
100
 
101
 
102
 
 
103
  def astronomy_image_agent(user_input: str):
104
  """
105
  Retrieves astronomy-related images from NASA's Image and Video Library or the Astronomy Picture of the Day (APOD) API.
 
39
  Fetches and formats recent space-related events from NASA's EONET API.
40
 
41
  Returns:
42
+ dict: A well-structured summary of key space-related events.
43
  """
44
  import os
45
  import requests
 
46
 
47
  base_url = "https://eonet.gsfc.nasa.gov/api/v2.1/events"
48
+ api_key = os.getenv("NASA_API_KEY")
49
+
50
+ if not api_key:
51
+ return {"output": "πŸš€ **Space Events Agent:** Missing NASA API key. Please check your API configuration."}
52
+
53
+ params = {"api_key": api_key}
54
 
55
  try:
56
+ response = requests.get(base_url, params=params, timeout=10) # Increased timeout
57
  response.raise_for_status()
58
+ data = response.json()
59
+ except requests.exceptions.Timeout:
60
+ return {"output": "πŸš€ **Space Events Agent:** The request timed out. Please try again later."}
61
+ except requests.exceptions.ConnectionError:
62
+ return {"output": "πŸš€ **Space Events Agent:** Unable to connect to NASA's servers. Check your internet or try later."}
63
  except requests.exceptions.RequestException as e:
64
  return {"output": f"πŸš€ **Space Events Agent:** Unable to fetch data. Error: {str(e)}"}
65
 
66
+ events = data.get("events", [])
67
 
68
  # Filter space-related events
69
  space_categories = ["meteor", "asteroid", "space debris", "solar activity", "geomagnetic storm", "comet"]
 
73
  ]
74
 
75
  if not space_related:
76
+ return {"output": "πŸš€ **Space Events Agent:** No recent space-related events. Stay tuned for the latest discoveries!"}
77
 
78
  # Sort events by date (latest first)
79
  space_related = sorted(space_related, key=lambda e: e.get("geometries", [{}])[0].get("date", ""), reverse=True)
80
 
81
+ # Limit to top 5 events and add details
82
  formatted_events = []
83
+ for event in space_related[:5]:
84
  title = event.get('title', 'Unknown Event')
85
  category = event.get('categories', [{}])[0].get('title', 'Unknown')
86
  geometries = event.get('geometries', [])
87
  event_date = geometries[0].get('date', 'Unknown Date') if geometries else 'Unknown Date'
88
  location = geometries[0].get('coordinates', 'Unknown Location') if geometries else 'Unknown Location'
89
 
90
+ # Additional insights based on event type
91
+ insights = {
92
+ "asteroid": "πŸ”­ A newly discovered asteroid is passing near Earth! Scientists are tracking its trajectory.",
93
+ "solar": "β˜€οΈ A strong solar flare was detected, potentially affecting Earth's geomagnetic field and satellites.",
94
+ "meteor": "🌠 A spectacular meteor shower is expected. Best viewing times depend on your location.",
95
+ }
96
+ insight = insights.get(category.lower(), "πŸš€ Scientists are analyzing this event for its impact on space exploration.")
 
 
97
 
98
  formatted_events.append(
99
  f"πŸ”­ **{title}**\n"
 
107
 
108
 
109
 
110
+
111
  def astronomy_image_agent(user_input: str):
112
  """
113
  Retrieves astronomy-related images from NASA's Image and Video Library or the Astronomy Picture of the Day (APOD) API.