Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files35 unmodified lines36373839404142434445464748495051525354555657585960616263646566676813 unmodified lines828384858687888935 unmodified linesdef space_events_agent():"""Fetches and formats upcoming space events from NASA's EONET API.Returns:dict: A dictionary with the formatted space events or a message if no events are found.Raises:HTTPError: If the request to the NASA API fails.Note:Requires NASA API key set in the environment variable `NASA_API_KEY`."""import osimport requestsbase_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)response.raise_for_status()except Exception as e:return {"output": f"π **Space Events Agent:** Error fetching events: {str(e)}"}events = response.json().get("events", [])if events:event_details = []for event in events:title = event.get('title', 'N/A')# Retrieve category using safe defaultscategory = (event.get('categories', [{}])[0]).get('title', 'Unknown Category')geometries = event.get('geometries', [])if geometries:13 unmodified linesevent_details.append(detail)formatted_events = "\n".join(event_details)return {"output": f"π **Space Events Agent:**\n\n{formatted_events}"}else:return {"output": "π **Space Events Agent:** No events found at the moment."}35 unmodified lines3637383940414243444546474849505152535455565758596061626364656613 unmodified lines808182838485868788899091929335 unmodified linesdef space_events_agent():"""Fetches space events from NASA's EONET API, processes them, and generates a response using LLM.Returns: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`."""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 APIresponse = 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 eventsevents = response.json().get("events", [])if events:# Extract event detailsevent_details = []for event in events:title = event.get('title', 'N/A')category = (event.get('categories', [{}])[0]).get('title', 'Unknown Category')geometries = event.get('geometries', [])if geometries:13 unmodified linesevent_details.append(detail)formatted_events = "\n".join(event_details)# Generate response using the LLMprompt = 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 foundreturn {"output": "π **Space Events Agent:** No events found at the moment."}
app.py+12-8