Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,77 +34,29 @@ llm = ChatGoogleGenerativeAI(
|
|
| 34 |
google_api_key=os.getenv("GOOGLE_API_KEY")
|
| 35 |
)
|
| 36 |
|
| 37 |
-
def
|
| 38 |
"""
|
| 39 |
-
|
| 40 |
|
| 41 |
Returns:
|
| 42 |
-
dict: A
|
| 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 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
events = data.get("events", [])
|
| 67 |
-
|
| 68 |
-
# Filter space-related events
|
| 69 |
-
space_categories = ["meteor", "asteroid", "space debris", "solar activity", "geomagnetic storm", "comet"]
|
| 70 |
-
space_related = [
|
| 71 |
-
event for event in events
|
| 72 |
-
if any(cat.get("title", "").lower() in space_categories for cat in event.get("categories", []))
|
| 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"
|
| 100 |
-
f"π
Date: {event_date}\n"
|
| 101 |
-
f"π Location: {location}\n"
|
| 102 |
-
f"π Category: {category}\n"
|
| 103 |
-
f"π’ Insight: {insight}\n"
|
| 104 |
)
|
| 105 |
|
| 106 |
-
|
| 107 |
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
|
|
|
|
| 34 |
google_api_key=os.getenv("GOOGLE_API_KEY")
|
| 35 |
)
|
| 36 |
|
| 37 |
+
def current_space_news():
|
| 38 |
"""
|
| 39 |
+
Retrieves and formats the latest space-related news using Tavily search.
|
| 40 |
|
| 41 |
Returns:
|
| 42 |
+
dict: A structured response with key headlines and links.
|
| 43 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
+
# Use Tavily API to fetch real-time space news
|
| 46 |
+
news_results = search.invoke({"query": "Latest space news, including recent discoveries, missions, and launches."})
|
| 47 |
+
|
| 48 |
+
if not news_results:
|
| 49 |
+
return {"output": "π **Space News Update:** No recent news found at the moment. Check NASA, ESA, or SpaceX websites for updates."}
|
| 50 |
+
|
| 51 |
+
# Format the top 3 space news articles
|
| 52 |
+
formatted_news = "\n\n".join(
|
| 53 |
+
[f"π° **{article.get('title', 'Untitled')}**\nπ [Read More]({article.get('url', '#'}) )" for article in news_results[:3]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
+
return {"output": f"π **Latest Space News:**\n\n{formatted_news}"}
|
| 57 |
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return {"output": f"π **Space News Update:** Unable to fetch news. Error: {str(e)}"}
|
| 60 |
|
| 61 |
|
| 62 |
|