SHAMIL SHAHBAZ AWAN commited on
Commit
6f76ac1
·
verified ·
1 Parent(s): 73d1349

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -91,25 +91,30 @@ def astronomy_image_agent(user_input: str):
91
  """
92
  Retrieves astronomy-related images from NASA's Image and Video Library or the Astronomy Picture of the Day (APOD) API.
93
 
94
- If the user query contains "galaxy", it fetches galaxy images from NASA's Image and Video Library.
95
- If no specific keyword is found, it defaults to fetching the Astronomy Picture of the Day (APOD).
 
96
 
97
  Args:
98
  user_input (str): The user input containing the query.
99
 
100
  Returns:
101
- dict: A dictionary containing details about the requested astronomy image,
102
- including the title, description, and URL. If an error occurs, an error message is returned.
103
-
104
- Raises:
105
- HTTPError: If the request to the NASA API fails.
106
- ValueError: If no relevant image or data is found.
107
  """
108
- user_input = user_input.lower()
109
- if "galaxy" in user_input:
110
- # Query NASA's Image and Video Library API for galaxy images.
 
 
 
 
 
 
 
 
111
  search_url = "https://images-api.nasa.gov/search"
112
- params = {"q": "galaxy", "media_type": "image"}
113
  try:
114
  response = requests.get(search_url, params=params)
115
  response.raise_for_status()
@@ -126,13 +131,13 @@ def astronomy_image_agent(user_input: str):
126
  f"Description: {description}\n"
127
  f"Image URL: {image_url}"
128
  )
129
- return {"output": f"✨ Galaxy Image Agent:\n\n{result}"}
130
  else:
131
- return {"output": "No galaxy images found."}
132
  except Exception as e:
133
- return {"output": f"Galaxy image search failed: {str(e)}"}
134
  else:
135
- # Default to the APOD API if no keyword is detected.
136
  url = f"https://api.nasa.gov/planetary/apod?api_key={os.getenv('NASA_API_KEY')}"
137
  try:
138
  response = requests.get(url)
@@ -237,7 +242,6 @@ search = TavilySearchResults(tavily_api_key=os.getenv("TAVILY_API_KEY"))
237
  tools = [search, iss_location_agent, space_events_agent, astronomy_image_agent, educational_resources_agent]
238
  llm_with_tools = llm.bind_tools(tools)
239
 
240
- # System message
241
  sys_msg = SystemMessage(content='''This system is designed to provide real-time astronomical data, visualizations, and educational content. Below are the key functions and tools integrated into the system and their specific purposes:
242
 
243
  1. **Tavily Search (`search`) Integration**:
@@ -285,7 +289,6 @@ sys_msg = SystemMessage(content='''This system is designed to provide real-time
285
 
286
  This setup ensures that the assistant can efficiently address a wide range of user queries related to space, astronomy, and related educational content while keeping interactions intuitive and engaging.
287
  ''')
288
-
289
  # Node
290
  def assistant(state: MessagesState) -> MessagesState:
291
  return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"][-10:])]}
@@ -325,7 +328,7 @@ for message in st.session_state.messages:
325
  st.write(message["content"])
326
 
327
  # User input via chat
328
- if user_input := st.chat_input("Ask about space news, ISS location, or astronomy images"):
329
  st.session_state.messages.append({"role": "user", "content": user_input})
330
  st.chat_message("user").write(user_input)
331
 
 
91
  """
92
  Retrieves astronomy-related images from NASA's Image and Video Library or the Astronomy Picture of the Day (APOD) API.
93
 
94
+ If the user query contains specific keywords (e.g., "galaxy", "jupiter", "mars", etc.), it fetches images
95
+ from NASA's Image and Video Library using that keyword. If no specific keyword is found, it defaults to fetching
96
+ the Astronomy Picture of the Day (APOD).
97
 
98
  Args:
99
  user_input (str): The user input containing the query.
100
 
101
  Returns:
102
+ dict: A dictionary containing details about the requested astronomy image, including the title,
103
+ description, and URL. If an error occurs, an error message is returned.
 
 
 
 
104
  """
105
+ user_input_lower = user_input.lower()
106
+ # List of keywords that trigger a search on NASA's Image and Video Library
107
+ search_keywords = ["galaxy", "jupiter", "mars", "nebula", "saturn", "comet", "asteroid", "moon"]
108
+ found_keyword = None
109
+ for keyword in search_keywords:
110
+ if keyword in user_input_lower:
111
+ found_keyword = keyword
112
+ break
113
+
114
+ if found_keyword:
115
+ # Query NASA's Image and Video Library API using the found keyword.
116
  search_url = "https://images-api.nasa.gov/search"
117
+ params = {"q": found_keyword, "media_type": "image"}
118
  try:
119
  response = requests.get(search_url, params=params)
120
  response.raise_for_status()
 
131
  f"Description: {description}\n"
132
  f"Image URL: {image_url}"
133
  )
134
+ return {"output": f"✨ {found_keyword.capitalize()} Image Agent:\n\n{result}"}
135
  else:
136
+ return {"output": f"No images found for {found_keyword}."}
137
  except Exception as e:
138
+ return {"output": f"{found_keyword.capitalize()} image search failed: {str(e)}"}
139
  else:
140
+ # Default to the APOD API if no specific keyword is detected.
141
  url = f"https://api.nasa.gov/planetary/apod?api_key={os.getenv('NASA_API_KEY')}"
142
  try:
143
  response = requests.get(url)
 
242
  tools = [search, iss_location_agent, space_events_agent, astronomy_image_agent, educational_resources_agent]
243
  llm_with_tools = llm.bind_tools(tools)
244
 
 
245
  sys_msg = SystemMessage(content='''This system is designed to provide real-time astronomical data, visualizations, and educational content. Below are the key functions and tools integrated into the system and their specific purposes:
246
 
247
  1. **Tavily Search (`search`) Integration**:
 
289
 
290
  This setup ensures that the assistant can efficiently address a wide range of user queries related to space, astronomy, and related educational content while keeping interactions intuitive and engaging.
291
  ''')
 
292
  # Node
293
  def assistant(state: MessagesState) -> MessagesState:
294
  return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"][-10:])]}
 
328
  st.write(message["content"])
329
 
330
  # User input via chat
331
+ if user_input := st.chat_input("Ask about space news, ISS location, Educational resource or astronomy images"):
332
  st.session_state.messages.append({"role": "user", "content": user_input})
333
  st.chat_message("user").write(user_input)
334