soctopus2327 commited on
Commit
0049a45
Β·
verified Β·
1 Parent(s): ff0a5bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -14
app.py CHANGED
@@ -241,24 +241,54 @@ for i, challenge in enumerate(st.session_state.daily_challenges):
241
  st.session_state.points = len(completed_challenges) * 10 # 10 points per challenge
242
  st.markdown(f"<h2 style='text-align: center;'>πŸ’° Points: {st.session_state.points}</h2>", unsafe_allow_html=True)
243
 
244
- # Check if all challenges are completed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  if len(completed_challenges) == len(st.session_state.daily_challenges):
246
  st.success("All challenges completed! πŸŽ‰ You've unlocked the secret section!")
247
 
248
  # Fetch endangered species data for the user's city
249
- species_data = fetch_endangered_species(city)
250
- if species_data:
251
- st.subheader("πŸ¦‹ Endangered Species Unlocked!")
252
- st.write(f"**Species**: {species_data.get('name', 'Unknown')}")
253
- st.write(f"**Population**: {species_data.get('population', 'Unknown')}")
254
- st.write(f"**Description**: {species_data.get('description', 'No description available')}")
255
-
256
- # Generate an image of the endangered species
257
- image_description = f"Generate an image of the endangered species: {species_data.get('name', 'Unknown')}"
258
- species_image_bytes = generate_image(image_description)
259
- if species_image_bytes:
260
- species_image = Image.open(BytesIO(species_image_bytes))
261
- st.image(species_image, caption=f"Endangered Species: {species_data.get('name', 'Unknown')}", use_column_width=True)
 
 
 
 
262
 
263
  # User's Environmental Interests Section
264
  st.subheader("🌍 Get Involved!")
 
241
  st.session_state.points = len(completed_challenges) * 10 # 10 points per challenge
242
  st.markdown(f"<h2 style='text-align: center;'>πŸ’° Points: {st.session_state.points}</h2>", unsafe_allow_html=True)
243
 
244
+ # Function to fetch endangered species data for a region
245
+ def fetch_all_endangered_species(city: str) -> list:
246
+ prompt = (
247
+ f"Provide a list of endangered species found near {city}, "
248
+ "including their names, population estimates, and descriptions. "
249
+ "Return the data in JSON format as a list of dictionaries."
250
+ )
251
+ response = openai.ChatCompletion.create(
252
+ model="gpt-3.5-turbo",
253
+ messages=[{"role": "user", "content": prompt}],
254
+ max_tokens=300,
255
+ temperature=0.8
256
+ )
257
+ response_content = response.choices[0].message['content'].strip()
258
+
259
+ try:
260
+ species_list = eval(response_content) # Assuming the response is a JSON-like structure
261
+ if isinstance(species_list, list) and all(isinstance(species, dict) for species in species_list):
262
+ return species_list
263
+ else:
264
+ st.error("Unexpected response format. Could not parse species data.")
265
+ return []
266
+ except Exception as e:
267
+ st.error(f"Error fetching endangered species data: {e}")
268
+ return []
269
+
270
+ # Display the endangered species section
271
  if len(completed_challenges) == len(st.session_state.daily_challenges):
272
  st.success("All challenges completed! πŸŽ‰ You've unlocked the secret section!")
273
 
274
  # Fetch endangered species data for the user's city
275
+ species_data_list = fetch_all_endangered_species(city)
276
+ if species_data_list:
277
+ st.subheader("πŸ¦‹ Endangered Species in Your Region")
278
+
279
+ for species_data in species_data_list:
280
+ species_name = species_data.get('name', 'Unknown')
281
+ st.write(f"**Species**: {species_name}")
282
+ st.write(f"**Population**: {species_data.get('population', 'Unknown')}")
283
+ st.write(f"**Description**: {species_data.get('description', 'No description available')}")
284
+
285
+ # Generate an image of each endangered species
286
+ image_description = f"Generate an image of the endangered species: {species_name}"
287
+ species_image_bytes = generate_image(image_description)
288
+ if species_image_bytes:
289
+ species_image = Image.open(BytesIO(species_image_bytes))
290
+ st.image(species_image, caption=f"Endangered Species: {species_name}", use_column_width=True)
291
+ st.write("---")
292
 
293
  # User's Environmental Interests Section
294
  st.subheader("🌍 Get Involved!")