Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files37 unmodified lines38394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909137 unmodified lines"""Fetches and formats upcoming space events from NASA's EONET API.Args:state (AgentState): The current agent state.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`."""base_url = "https://eonet.gsfc.nasa.gov/api/v2.1/events"# Using a separate NASA API key for EONET if neededparams = {"api_key": os.getenv("NASA_API_KEY")}response = requests.get(base_url, params=params)response.raise_for_status()events = response.json().get("events", [])if events:event_details = []for event in events:# Safely retrieve category and coordinatescategory = (event.get('categories', [{}])[0]).get('title', 'Unknown Category')geometries = event.get('geometries', [])coordinates = geometries[0].get('coordinates', 'Unknown Location') if geometries else 'Unknown Location'event_details.append(f"β’ Event: {event.get('title', 'N/A')}\n"f" Type: {category}\n"f" Location: {coordinates}\n")formatted_events = "\n".join(event_details)return {"output": f"π Space Events Agent:\n\n{formatted_events}"}else:return {"output": f"π Space Events Agent: No events found at the moment."}loader1 = WebBaseLoader("https://spacelaunchnow.me/3.3.0/launch/upcoming/")docs1 = loader1.load()documents = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200).split_documents(docs1)vector = FAISS.from_documents(documents, GoogleGenerativeAIEmbeddings(model="models/embedding-001"))retriever = vector.as_retriever()retriever_tool = create_retriever_tool(retriever,"spacelaunchnow","Search for information about space real time news",)def astronomy_image_agent(user_input: str):"""37 unmodified lines38394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909137 unmodified lines"""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:first_geometry = geometries[0]coordinates = first_geometry.get('coordinates', 'Unknown Location')event_date = first_geometry.get('date', 'Unknown Date')else:coordinates = 'Unknown Location'event_date = 'Unknown Date'detail = (f"**Event:** {title}\n"f"**Category:** {category}\n"f"**Location:** {coordinates}\n"f"**Date:** {event_date}\n")event_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."}def astronomy_image_agent(user_input: str):"""
app.py+30-30