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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -63
app.py CHANGED
@@ -34,77 +34,29 @@ llm = ChatGoogleGenerativeAI(
34
  google_api_key=os.getenv("GOOGLE_API_KEY")
35
  )
36
 
37
- def space_events_agent():
38
  """
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"]
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
- return {"output": f"πŸš€ **Latest Space Events:**\n\n" + "\n".join(formatted_events)}
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