Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files33 unmodified lines343536373839404142434445464748495051525354555657585960616233 unmodified linesgoogle_api_key=os.getenv("GOOGLE_API_KEY"))def current_space_news():"""Retrieves and formats the latest space-related news using Tavily search.Returns:dict: A structured response with key headlines and links."""try:# Use Tavily API to fetch real-time space newsnews_results = search.invoke({"query": "Latest space news, including recent discoveries, missions, and launches."})if not news_results:return {"output": "π **Space News Update:** No recent news found at the moment. Check NASA, ESA, or SpaceX websites for updates."}# Format the top 3 space news articlesformatted_news = "\n\n".join([f"π° **{article.get('title', 'Untitled')}**\nπ [Read More]({article.get('url', '#'}) )" for article in news_results[:3]])return {"output": f"π **Latest Space News:**\n\n{formatted_news}"}except Exception as e:return {"output": f"π **Space News Update:** Unable to fetch news. Error: {str(e)}"}def astronomy_image_agent(user_input: str):"""33 unmodified lines34353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838433 unmodified linesgoogle_api_key=os.getenv("GOOGLE_API_KEY"))def space_events_agent():"""Fetches and formats upcoming space events from NASA's EONET API.Returns:dict: A dictionary with a concise and well-formatted summary of recent space-related events."""import osimport requestsfrom datetime import datetimebase_url = "https://eonet.gsfc.nasa.gov/api/v2.1/events"params = {"api_key": os.getenv("NASA_API_KEY")}try:response = requests.get(base_url, params=params, timeout=5) # Set a timeout to avoid long waitsresponse.raise_for_status()except requests.exceptions.RequestException as e:return {"output": f"π **Space Events Agent:** Unable to fetch data. Error: {str(e)}"}events = response.json().get("events", [])# Filter events related to space (exclude wildfires, icebergs, general disasters)space_related = [event for event in eventsif any(cat.get("title", "").lower() in ["meteor", "asteroid", "space debris", "solar activity", "geomagnetic storm"]for cat in event.get("categories", []))]if not space_related:return {"output": "π **Space Events Agent:** No major space-related events at the moment."}# Sort events by date (latest first)space_related = sorted(space_related, key=lambda e: e.get("geometries", [{}])[0].get("date", ""), reverse=True)# Limit to top 5 latest eventsformatted_events = "\n".join([f"π **{event.get('title', 'Unknown Event')}**\n"f"π Date: {event.get('geometries', [{}])[0].get('date', 'N/A')}\n"f"π Location: {event.get('geometries', [{}])[0].get('coordinates', 'Unknown')}\n"f"π Category: {event.get('categories', [{}])[0].get('title', 'Unknown')}\n"for event in space_related[:5]])return {"output": f"π **Latest Space Events:**\n\n{formatted_events}"}def astronomy_image_agent(user_input: str):"""
app.py+39-17