Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files
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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
response = requests.get(base_url, params=params, timeout=10) #
|
| 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 =
|
| 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
|
| 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 |
-
#
|
| 73 |
formatted_events = []
|
| 74 |
-
for event in space_related[:5]:
|
| 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 |
-
#
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 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.
|