Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files35 unmodified lines363738394039404241424344454648495047484950515253545556575859606162636564656613 unmodified lines80818285838485868788899091929335 unmodified lines
def space_events_agent(): """ Fetches and formats upcoming space events from NASA's EONET API.
Fetches space events from NASA's EONET API, processes them, and generates a response using LLM. Returns: dict: A dictionary with the formatted space events or a message if no events are found. dict: A dictionary with the LLM-generated response based on space events. Raises: HTTPError: If the request to the NASA API fails. Note: Requires NASA API key set in the environment variable `NASA_API_KEY`. """ import os import requests
base_url = "https://eonet.gsfc.nasa.gov/api/v2.1/events" params = {"api_key": os.getenv("NASA_API_KEY")} try: # Fetching events from NASA's EONET API response = requests.get(base_url, params=params) response.raise_for_status() except Exception as e: return {"output": f"๐ **Space Events Agent:** Error fetching events: {str(e)}"} # Processing the events events = response.json().get("events", []) if events: # Extract event details event_details = [] for event in events: title = event.get('title', 'N/A') # Retrieve category using safe defaults category = (event.get('categories', [{}])[0]).get('title', 'Unknown Category') geometries = event.get('geometries', []) if geometries:13 unmodified lines event_details.append(detail) formatted_events = "\n".join(event_details) return {"output": f"๐ **Space Events Agent:**\n\n{formatted_events}"} # Generate response using the LLM prompt = f"Here are the upcoming space events:\n{formatted_events}\nCan you provide a summary of these events?" llm_response = llm.chat(messages=[{"role": "system", "content": "You are a space events assistant."}, {"role": "user", "content": prompt}]) return {"output": llm_response['message']['content']} else: # If no events are found return {"output": "๐ **Space Events Agent:** No events found at the moment."}
app.py+12-8