Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
import openai
|
4 |
-
import json
|
5 |
from io import BytesIO
|
6 |
from PIL import Image
|
7 |
|
@@ -49,9 +48,20 @@ st.markdown("""
|
|
49 |
""", unsafe_allow_html=True)
|
50 |
|
51 |
# Initialize session state variables
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# Function to generate daily eco-friendly challenges
|
57 |
def generate_daily_challenges() -> list:
|
@@ -131,20 +141,20 @@ def generate_image(description: str) -> bytes:
|
|
131 |
return response.content
|
132 |
|
133 |
# Function to fetch endangered species data
|
134 |
-
def fetch_endangered_species(city: str) ->
|
135 |
-
prompt = f"
|
136 |
response = openai.ChatCompletion.create(
|
137 |
model="gpt-3.5-turbo",
|
138 |
messages=[{"role": "user", "content": prompt}],
|
139 |
-
max_tokens=
|
140 |
-
temperature=0.
|
141 |
)
|
142 |
response_content = response.choices[0].message['content'].strip()
|
143 |
try:
|
144 |
-
return
|
145 |
-
except
|
146 |
st.error(f"Error parsing endangered species data: {e}")
|
147 |
-
return
|
148 |
|
149 |
# Function to fetch NGOs using OpenAI
|
150 |
def fetch_nearby_ngos_with_openai(city: str, interests: list) -> list:
|
@@ -161,8 +171,13 @@ def fetch_nearby_ngos_with_openai(city: str, interests: list) -> list:
|
|
161 |
response_content = response.choices[0].message['content'].strip()
|
162 |
|
163 |
try:
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
166 |
st.error(f"Error fetching NGO data: {e}")
|
167 |
return []
|
168 |
|
@@ -175,6 +190,7 @@ city = st.text_input("Enter City Name:", placeholder="Type the name of a city...
|
|
175 |
if st.button("Generate Environmental Data, Music, and Image"):
|
176 |
st.session_state.real_data = fetch_real_data(city)
|
177 |
if st.session_state.real_data:
|
|
|
178 |
narrative = create_narrative(city, st.session_state.real_data)
|
179 |
mood = determine_mood(st.session_state.real_data)
|
180 |
|
@@ -225,11 +241,38 @@ for i, challenge in enumerate(st.session_state.daily_challenges):
|
|
225 |
st.session_state.points = len(completed_challenges) * 10 # 10 points per challenge
|
226 |
st.markdown(f"<h2 style='text-align: center;'>π° Points: {st.session_state.points}</h2>", unsafe_allow_html=True)
|
227 |
|
228 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
if len(completed_challenges) == len(st.session_state.daily_challenges):
|
230 |
st.success("All challenges completed! π You've unlocked the secret section!")
|
231 |
|
232 |
-
|
|
|
233 |
if species_data_list:
|
234 |
st.subheader("π¦ Endangered Species in Your Region")
|
235 |
|
@@ -268,4 +311,4 @@ if st.session_state.ngos:
|
|
268 |
st.write(f"**{ngo.get('name', 'Unknown NGO')}**")
|
269 |
st.write(f"π Location: {ngo.get('location', 'Unknown Location')}")
|
270 |
st.write(f"π± Focus Area: {ngo.get('focus', 'Unknown Focus Area')}")
|
271 |
-
st.write("---")
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
import openai
|
|
|
4 |
from io import BytesIO
|
5 |
from PIL import Image
|
6 |
|
|
|
48 |
""", unsafe_allow_html=True)
|
49 |
|
50 |
# Initialize session state variables
|
51 |
+
if "real_data" not in st.session_state:
|
52 |
+
st.session_state.real_data = {}
|
53 |
+
if "story" not in st.session_state:
|
54 |
+
st.session_state.story = ""
|
55 |
+
if "music_bytes" not in st.session_state:
|
56 |
+
st.session_state.music_bytes = None
|
57 |
+
if "image_bytes" not in st.session_state:
|
58 |
+
st.session_state.image_bytes = None
|
59 |
+
if "ngos" not in st.session_state:
|
60 |
+
st.session_state.ngos = []
|
61 |
+
if "points" not in st.session_state:
|
62 |
+
st.session_state.points = 0
|
63 |
+
if "daily_challenges" not in st.session_state:
|
64 |
+
st.session_state.daily_challenges = []
|
65 |
|
66 |
# Function to generate daily eco-friendly challenges
|
67 |
def generate_daily_challenges() -> list:
|
|
|
141 |
return response.content
|
142 |
|
143 |
# Function to fetch endangered species data
|
144 |
+
def fetch_endangered_species(city: str) -> dict:
|
145 |
+
prompt = f"Provide details of an endangered species in {city}, including its name, image description, and current population."
|
146 |
response = openai.ChatCompletion.create(
|
147 |
model="gpt-3.5-turbo",
|
148 |
messages=[{"role": "user", "content": prompt}],
|
149 |
+
max_tokens=150,
|
150 |
+
temperature=0.8
|
151 |
)
|
152 |
response_content = response.choices[0].message['content'].strip()
|
153 |
try:
|
154 |
+
return eval(response_content) # Assuming the response is a JSON-like structure
|
155 |
+
except Exception as e:
|
156 |
st.error(f"Error parsing endangered species data: {e}")
|
157 |
+
return {}
|
158 |
|
159 |
# Function to fetch NGOs using OpenAI
|
160 |
def fetch_nearby_ngos_with_openai(city: str, interests: list) -> list:
|
|
|
171 |
response_content = response.choices[0].message['content'].strip()
|
172 |
|
173 |
try:
|
174 |
+
ngo_list = eval(response_content)
|
175 |
+
if isinstance(ngo_list, list) and all(isinstance(ngo, dict) for ngo in ngo_list):
|
176 |
+
return ngo_list
|
177 |
+
else:
|
178 |
+
st.error("Unexpected response format. Could not parse NGO data.")
|
179 |
+
return []
|
180 |
+
except Exception as e:
|
181 |
st.error(f"Error fetching NGO data: {e}")
|
182 |
return []
|
183 |
|
|
|
190 |
if st.button("Generate Environmental Data, Music, and Image"):
|
191 |
st.session_state.real_data = fetch_real_data(city)
|
192 |
if st.session_state.real_data:
|
193 |
+
# Generate narrative and mood
|
194 |
narrative = create_narrative(city, st.session_state.real_data)
|
195 |
mood = determine_mood(st.session_state.real_data)
|
196 |
|
|
|
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 |
|
|
|
311 |
st.write(f"**{ngo.get('name', 'Unknown NGO')}**")
|
312 |
st.write(f"π Location: {ngo.get('location', 'Unknown Location')}")
|
313 |
st.write(f"π± Focus Area: {ngo.get('focus', 'Unknown Focus Area')}")
|
314 |
+
st.write("---")
|