SHAMIL SHAHBAZ AWAN commited on
Commit
87f7faf
Β·
verified Β·
1 Parent(s): 1c645a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -34,29 +34,51 @@ llm = ChatGoogleGenerativeAI(
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
  def astronomy_image_agent(user_input: str):
62
  """
 
34
  google_api_key=os.getenv("GOOGLE_API_KEY")
35
  )
36
 
37
+ def space_events_agent():
38
  """
39
+ Fetches and formats upcoming space events from NASA's EONET API.
40
 
41
  Returns:
42
+ dict: A dictionary with a concise and well-formatted summary of recent 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=5) # 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 events related to space (exclude wildfires, icebergs, general disasters)
60
+ space_related = [
61
+ event for event in events
62
+ if any(cat.get("title", "").lower() in ["meteor", "asteroid", "space debris", "solar activity", "geomagnetic storm"]
63
+ for cat in event.get("categories", []))
64
+ ]
65
+
66
+ if not space_related:
67
+ return {"output": "πŸš€ **Space Events Agent:** No major space-related events at the moment."}
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
+ # Limit to top 5 latest events
73
+ formatted_events = "\n".join([
74
+ f"πŸ”­ **{event.get('title', 'Unknown Event')}**\n"
75
+ f"πŸ“… Date: {event.get('geometries', [{}])[0].get('date', 'N/A')}\n"
76
+ f"πŸ“ Location: {event.get('geometries', [{}])[0].get('coordinates', 'Unknown')}\n"
77
+ f"πŸ”Ž Category: {event.get('categories', [{}])[0].get('title', 'Unknown')}\n"
78
+ for event in space_related[:5]
79
+ ])
80
+
81
+ return {"output": f"πŸš€ **Latest Space Events:**\n\n{formatted_events}"}
82
 
83
  def astronomy_image_agent(user_input: str):
84
  """