Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import requests
|
|
3 |
import openai
|
4 |
from io import BytesIO
|
5 |
from PIL import Image
|
|
|
6 |
|
7 |
# Set page configuration as the first Streamlit command
|
8 |
st.set_page_config(page_title="Eco-Symphony", page_icon="π±", layout="centered")
|
@@ -59,6 +60,34 @@ if "image_bytes" not in st.session_state:
|
|
59 |
if "ngos" not in st.session_state:
|
60 |
st.session_state.ngos = []
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
# Function to fetch weather data
|
63 |
def fetch_real_data(city: str) -> dict:
|
64 |
weather_url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_API_KEY}&units=metric'
|
@@ -124,65 +153,76 @@ def generate_image(description: str) -> bytes:
|
|
124 |
return None
|
125 |
return response.content
|
126 |
|
127 |
-
#
|
128 |
-
def
|
129 |
-
|
130 |
-
f"List NGOs near {city} that focus on {', '.join(interests)}. "
|
131 |
-
"Provide the names, locations, and focus areas in JSON format as a list of dictionaries."
|
132 |
-
)
|
133 |
-
response = openai.ChatCompletion.create(
|
134 |
-
model="gpt-3.5-turbo",
|
135 |
-
messages=[{"role": "user", "content": prompt}],
|
136 |
-
max_tokens=200,
|
137 |
-
temperature=0.7
|
138 |
-
)
|
139 |
-
response_content = response.choices[0].message['content'].strip()
|
140 |
-
|
141 |
-
try:
|
142 |
-
ngo_list = eval(response_content)
|
143 |
-
if isinstance(ngo_list, list) and all(isinstance(ngo, dict) for ngo in ngo_list):
|
144 |
-
return ngo_list
|
145 |
-
else:
|
146 |
-
st.error("Unexpected response format. Could not parse NGO data.")
|
147 |
-
return []
|
148 |
-
except Exception as e:
|
149 |
-
st.error(f"Error fetching NGO data: {e}")
|
150 |
-
return []
|
151 |
|
152 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
st.title("πΏ Eco-Symphony πΆ")
|
154 |
-
st.write("Enter a city to explore real-time environmental data,
|
155 |
|
156 |
city = st.text_input("Enter City Name:", placeholder="Type the name of a city...")
|
157 |
|
158 |
-
if
|
|
|
159 |
st.session_state.real_data = fetch_real_data(city)
|
160 |
if st.session_state.real_data:
|
161 |
-
# Generate narrative and mood
|
162 |
narrative = create_narrative(city, st.session_state.real_data)
|
163 |
mood = determine_mood(st.session_state.real_data)
|
164 |
-
|
165 |
-
# Generate AI story
|
166 |
st.session_state.story = generate_story_with_ai(narrative, mood)
|
167 |
-
|
168 |
-
# Generate Music and Image Based on Story and Mood
|
169 |
music_description = f"{mood} mood with {st.session_state.real_data['weather_condition'].lower()} weather"
|
170 |
st.session_state.music_bytes = generate_music(music_description)
|
171 |
st.session_state.image_bytes = generate_image(st.session_state.story)
|
172 |
-
|
173 |
-
# Display
|
174 |
-
if st.session_state.music_bytes:
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
if st.session_state.image_bytes:
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
# Display
|
183 |
-
if st.session_state.real_data:
|
184 |
st.subheader("π Environmental Narrative")
|
185 |
-
narrative = create_narrative(city, st.session_state.real_data)
|
186 |
st.write(narrative)
|
187 |
|
188 |
st.subheader("π Real Weather Data")
|
@@ -190,29 +230,9 @@ if st.session_state.real_data:
|
|
190 |
st.write("Humidity (%):", st.session_state.real_data.get("humidity", "Data not available"))
|
191 |
st.write("Weather Condition:", st.session_state.real_data.get("weather_condition", "Data not available"))
|
192 |
|
193 |
-
if st.session_state.story:
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
#
|
198 |
-
|
199 |
-
st.write("Choose your areas of interest for saving the environment:")
|
200 |
-
interests = st.multiselect(
|
201 |
-
"Select Areas of Interest:",
|
202 |
-
["Afforestation", "Water Conservation", "Biodiversity Protection", "Recycling", "Climate Change Awareness"]
|
203 |
-
)
|
204 |
-
|
205 |
-
if st.button("Find Nearby NGOs"):
|
206 |
-
if interests:
|
207 |
-
st.session_state.ngos = fetch_nearby_ngos_with_openai(city, interests)
|
208 |
-
else:
|
209 |
-
st.warning("Please select at least one area of interest.")
|
210 |
-
|
211 |
-
# Display NGO information
|
212 |
-
if st.session_state.ngos:
|
213 |
-
st.subheader("π NGOs Near You")
|
214 |
-
for ngo in st.session_state.ngos:
|
215 |
-
st.write(f"**{ngo.get('name', 'Unknown NGO')}**")
|
216 |
-
st.write(f"π Location: {ngo.get('location', 'Unknown Location')}")
|
217 |
-
st.write(f"π± Focus Area: {ngo.get('focus', 'Unknown Focus Area')}")
|
218 |
-
st.write("---")
|
|
|
3 |
import openai
|
4 |
from io import BytesIO
|
5 |
from PIL import Image
|
6 |
+
import random
|
7 |
|
8 |
# Set page configuration as the first Streamlit command
|
9 |
st.set_page_config(page_title="Eco-Symphony", page_icon="π±", layout="centered")
|
|
|
60 |
if "ngos" not in st.session_state:
|
61 |
st.session_state.ngos = []
|
62 |
|
63 |
+
# Function to generate daily challenges using GPT
|
64 |
+
def generate_daily_challenges() -> list:
|
65 |
+
prompt = "Give me 5 small, easy-to-do eco-friendly daily challenges that can be completed in a day."
|
66 |
+
response = openai.ChatCompletion.create(
|
67 |
+
model="gpt-3.5-turbo",
|
68 |
+
messages=[{"role": "user", "content": prompt}],
|
69 |
+
max_tokens=100,
|
70 |
+
temperature=0.8
|
71 |
+
)
|
72 |
+
challenges = response.choices[0].message['content'].strip().split("\n")
|
73 |
+
return [challenge.strip() for challenge in challenges if challenge.strip()]
|
74 |
+
|
75 |
+
# Function to fetch endangered species based on user's city
|
76 |
+
def fetch_endangered_species(city: str) -> dict:
|
77 |
+
prompt = f"Provide details of an endangered species in {city}, including its name, image description, and current population."
|
78 |
+
response = openai.ChatCompletion.create(
|
79 |
+
model="gpt-3.5-turbo",
|
80 |
+
messages=[{"role": "user", "content": prompt}],
|
81 |
+
max_tokens=150,
|
82 |
+
temperature=0.8
|
83 |
+
)
|
84 |
+
response_content = response.choices[0].message['content'].strip()
|
85 |
+
try:
|
86 |
+
return eval(response_content) # Assuming the response is a JSON-like structure
|
87 |
+
except Exception as e:
|
88 |
+
st.error(f"Error parsing endangered species data: {e}")
|
89 |
+
return {}
|
90 |
+
|
91 |
# Function to fetch weather data
|
92 |
def fetch_real_data(city: str) -> dict:
|
93 |
weather_url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_API_KEY}&units=metric'
|
|
|
153 |
return None
|
154 |
return response.content
|
155 |
|
156 |
+
# Main daily challenges section
|
157 |
+
def daily_challenges_section(city):
|
158 |
+
st.subheader("π Daily Challenges")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
+
# Points counter
|
161 |
+
if 'points' not in st.session_state:
|
162 |
+
st.session_state['points'] = 0
|
163 |
+
|
164 |
+
st.markdown(f"<h2 style='text-align: center;'>π° Points: {st.session_state['points']}</h2>", unsafe_allow_html=True)
|
165 |
+
|
166 |
+
# Generate or load daily challenges
|
167 |
+
if 'daily_challenges' not in st.session_state:
|
168 |
+
st.session_state['daily_challenges'] = generate_daily_challenges()
|
169 |
+
|
170 |
+
# Create a checkable list for the challenges
|
171 |
+
completed_challenges = []
|
172 |
+
for i, challenge in enumerate(st.session_state['daily_challenges']):
|
173 |
+
if st.checkbox(challenge, key=f"challenge_{i}"):
|
174 |
+
completed_challenges.append(i)
|
175 |
+
|
176 |
+
# Update points based on completed challenges
|
177 |
+
st.session_state['points'] = len(completed_challenges) * 10 # Example: 10 points per challenge
|
178 |
+
|
179 |
+
# Check if all challenges are completed
|
180 |
+
if len(completed_challenges) == len(st.session_state['daily_challenges']):
|
181 |
+
st.success("All challenges completed! π You've unlocked the secret section!")
|
182 |
+
|
183 |
+
# Fetch endangered species data for the user's city
|
184 |
+
species_data = fetch_endangered_species(city)
|
185 |
+
if species_data:
|
186 |
+
st.subheader("π¦ Endangered Species Unlocked!")
|
187 |
+
st.write(f"**Species**: {species_data.get('name', 'Unknown')}")
|
188 |
+
st.write(f"**Population**: {species_data.get('population', 'Unknown')}")
|
189 |
+
st.write(f"**Description**: {species_data.get('description', 'No description available')}")
|
190 |
+
|
191 |
+
# Generate an image of the endangered species
|
192 |
+
image_description = f"Generate an image of the endangered species: {species_data.get('name', 'Unknown')}"
|
193 |
+
species_image_bytes = generate_image(image_description)
|
194 |
+
if species_image_bytes:
|
195 |
+
species_image = Image.open(BytesIO(species_image_bytes))
|
196 |
+
st.image(species_image, caption=f"Endangered Species: {species_data.get('name', 'Unknown')}", use_column_width=True)
|
197 |
+
|
198 |
+
# Add this to your Streamlit UI where appropriate
|
199 |
st.title("πΏ Eco-Symphony πΆ")
|
200 |
+
st.write("Enter a city to explore real-time environmental data, complete daily challenges, and unlock hidden content!")
|
201 |
|
202 |
city = st.text_input("Enter City Name:", placeholder="Type the name of a city...")
|
203 |
|
204 |
+
if city:
|
205 |
+
# Other sections of the app (weather data, music, etc.)
|
206 |
st.session_state.real_data = fetch_real_data(city)
|
207 |
if st.session_state.real_data:
|
|
|
208 |
narrative = create_narrative(city, st.session_state.real_data)
|
209 |
mood = determine_mood(st.session_state.real_data)
|
|
|
|
|
210 |
st.session_state.story = generate_story_with_ai(narrative, mood)
|
|
|
|
|
211 |
music_description = f"{mood} mood with {st.session_state.real_data['weather_condition'].lower()} weather"
|
212 |
st.session_state.music_bytes = generate_music(music_description)
|
213 |
st.session_state.image_bytes = generate_image(st.session_state.story)
|
214 |
+
|
215 |
+
# Display generated music and image at the top
|
216 |
+
if st.session_state.music_bytes:
|
217 |
+
st.subheader("πΆ Generated Music")
|
218 |
+
st.audio(BytesIO(st.session_state.music_bytes), format="audio/wav")
|
219 |
+
|
220 |
+
if st.session_state.image_bytes:
|
221 |
+
st.subheader("πΌοΈ Generated Image")
|
222 |
+
st.image(Image.open(BytesIO(st.session_state.image_bytes)), caption="Generated Image based on Story", use_column_width=True)
|
223 |
+
|
224 |
+
# Display the narrative and data
|
|
|
225 |
st.subheader("π Environmental Narrative")
|
|
|
226 |
st.write(narrative)
|
227 |
|
228 |
st.subheader("π Real Weather Data")
|
|
|
230 |
st.write("Humidity (%):", st.session_state.real_data.get("humidity", "Data not available"))
|
231 |
st.write("Weather Condition:", st.session_state.real_data.get("weather_condition", "Data not available"))
|
232 |
|
233 |
+
if st.session_state.story:
|
234 |
+
st.subheader("π AI-Generated Story")
|
235 |
+
st.write(st.session_state.story)
|
236 |
+
|
237 |
+
# Display daily challenges section
|
238 |
+
daily_challenges_section(city)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|