Spaces:
Sleeping
Sleeping
File size: 8,398 Bytes
a0cfb47 31cd393 a0cfb47 d4af652 a0cfb47 d4af652 a0cfb47 9d0ea95 a0cfb47 d4af652 a0cfb47 74f516c a0cfb47 74f516c a0cfb47 74f516c a0cfb47 d4af652 afd0e9f d4af652 afd0e9f d4af652 afd0e9f d4af652 afd0e9f a0cfb47 fb0eb6d 9d0ea95 fb0eb6d 9d0ea95 fb0eb6d 9d0ea95 fb0eb6d 9d0ea95 fb0eb6d 9d0ea95 d4af652 9d0ea95 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
import streamlit as st
import requests
import openai
from io import BytesIO
from PIL import Image
# Set page configuration as the first Streamlit command
st.set_page_config(page_title="Eco-Symphony", page_icon="π±", layout="centered")
# Set API keys from Streamlit Secrets
openai.api_key = st.secrets["OPENAI_API_KEY"]
OPENWEATHER_API_KEY = st.secrets["OPENWEATHER_API_KEY"]
HUGGINGFACE_API_KEY = st.secrets["HUGGINGFACE_API_KEY"]
# Hugging Face API URLs
MUSICGEN_API_URL = "https://api-inference.huggingface.co/models/facebook/musicgen-small"
IMAGEGEN_API_URL = "https://api-inference.huggingface.co/models/Artples/LAI-ImageGeneration-vSDXL-2"
# Headers for Hugging Face API requests
headers = {"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"}
# Inject custom CSS for green theme
st.markdown("""
<style>
body {
background-color: #ffffff;
}
.stApp {
color: #2e7d32;
font-family: 'Arial', sans-serif;
}
.stButton>button {
background-color: #66bb6a;
color: #fff;
font-weight: bold;
}
.stTextInput>div>input {
background-color: #e8f5e9;
color: #2e7d32;
}
.stMarkdown h1, .stMarkdown h2, .stMarkdown h3, .stMarkdown p {
color: #388e3c;
}
.stMarkdown h2 {
font-weight: bold;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state variables
if "real_data" not in st.session_state:
st.session_state.real_data = {}
if "story" not in st.session_state:
st.session_state.story = ""
if "music_bytes" not in st.session_state:
st.session_state.music_bytes = None
if "image_bytes" not in st.session_state:
st.session_state.image_bytes = None
if "ngos" not in st.session_state:
st.session_state.ngos = []
# Function to fetch weather data
def fetch_real_data(city: str) -> dict:
weather_url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_API_KEY}&units=metric'
weather_response = requests.get(weather_url)
if weather_response.status_code != 200:
st.error("Error fetching weather data.")
return {}
weather_data = weather_response.json()
return {
"temperature": weather_data['main'].get('temp', 'Data not available'),
"humidity": weather_data['main'].get('humidity', 'Data not available'),
"weather_condition": weather_data['weather'][0].get('main', 'Data not available')
}
# Function to determine mood based on weather data
def determine_mood(data: dict) -> str:
weather_condition = data["weather_condition"].lower()
temperature = data["temperature"]
if "rain" in weather_condition:
return "rainy"
elif "clear" in weather_condition and temperature > 25:
return "sunny"
elif "cloud" in weather_condition:
return "cloudy"
elif temperature < 15:
return "cool"
else:
return "neutral"
# Function to create a narrative
def create_narrative(city: str, data: dict) -> str:
return f"In {city}, the weather is {data['weather_condition']} with a temperature of {data['temperature']}Β°C."
# Function to generate a story using OpenAI
def generate_story_with_ai(narrative: str, mood: str) -> str:
messages = [
{"role": "system", "content": "You are a creative storyteller using characters and imagery."},
{"role": "user", "content": f"{narrative} The mood is '{mood}', write a story about how the environment feels in 50 words."}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=150,
temperature=0.7
)
return response.choices[0].message['content'].strip()
# Function to generate music from Hugging Face API
def generate_music(description: str) -> bytes:
payload = {"inputs": description}
response = requests.post(MUSICGEN_API_URL, headers=headers, json=payload)
if response.status_code != 200:
st.error(f"Error generating music: {response.status_code} {response.text}")
return None
return response.content
# Function to generate an image based on the story
def generate_image(description: str) -> bytes:
payload = {"inputs": description}
response = requests.post(IMAGEGEN_API_URL, headers=headers, json=payload)
if response.status_code != 200:
st.error(f"Error generating image: {response.status_code} {response.text}")
return None
return response.content
# Function to fetch NGOs using OpenAI
def fetch_nearby_ngos_with_openai(city: str, interests: list) -> list:
prompt = (
f"List NGOs near {city} that focus on {', '.join(interests)}. "
"Provide the names, locations, and focus areas in JSON format as a list of dictionaries."
)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=200,
temperature=0.7
)
response_content = response.choices[0].message['content'].strip()
try:
ngo_list = eval(response_content)
if isinstance(ngo_list, list) and all(isinstance(ngo, dict) for ngo in ngo_list):
return ngo_list
else:
st.error("Unexpected response format. Could not parse NGO data.")
return []
except Exception as e:
st.error(f"Error fetching NGO data: {e}")
return []
# Streamlit UI
st.title("πΏ Eco-Symphony πΆ")
st.write("Enter a city to explore real-time environmental data, generate AI-created music, and see an AI-generated image based on the story.")
city = st.text_input("Enter City Name:", placeholder="Type the name of a city...")
if st.button("Generate Environmental Data, Music, and Image"):
st.session_state.real_data = fetch_real_data(city)
if st.session_state.real_data:
# Generate narrative and mood
narrative = create_narrative(city, st.session_state.real_data)
mood = determine_mood(st.session_state.real_data)
# Generate AI story
st.session_state.story = generate_story_with_ai(narrative, mood)
# Generate Music and Image Based on Story and Mood
music_description = f"{mood} mood with {st.session_state.real_data['weather_condition'].lower()} weather"
st.session_state.music_bytes = generate_music(music_description)
st.session_state.image_bytes = generate_image(st.session_state.story)
# Display Music and Image at the Top
if st.session_state.music_bytes:
st.subheader("πΆ Generated Music")
st.audio(BytesIO(st.session_state.music_bytes), format="audio/wav")
if st.session_state.image_bytes:
st.subheader("πΌοΈ Generated Image")
st.image(Image.open(BytesIO(st.session_state.image_bytes)), caption="Generated Image based on Story", use_column_width=True)
# Display Environmental Narrative and Data
if st.session_state.real_data:
st.subheader("π Environmental Narrative")
narrative = create_narrative(city, st.session_state.real_data)
st.write(narrative)
st.subheader("π Real Weather Data")
st.write("Temperature (Β°C):", st.session_state.real_data.get("temperature", "Data not available"))
st.write("Humidity (%):", st.session_state.real_data.get("humidity", "Data not available"))
st.write("Weather Condition:", st.session_state.real_data.get("weather_condition", "Data not available"))
if st.session_state.story:
st.subheader("π AI-Generated Story")
st.write(st.session_state.story)
# Get User's Environmental Interests
st.subheader("π Get Involved!")
st.write("Choose your areas of interest for saving the environment:")
interests = st.multiselect(
"Select Areas of Interest:",
["Afforestation", "Water Conservation", "Biodiversity Protection", "Recycling", "Climate Change Awareness"]
)
if st.button("Find Nearby NGOs"):
if interests:
st.session_state.ngos = fetch_nearby_ngos_with_openai(city, interests)
else:
st.warning("Please select at least one area of interest.")
# Display NGO information
if st.session_state.ngos:
st.subheader("π NGOs Near You")
for ngo in st.session_state.ngos:
st.write(f"**{ngo.get('name', 'Unknown NGO')}**")
st.write(f"π Location: {ngo.get('location', 'Unknown Location')}")
st.write(f"π± Focus Area: {ngo.get('focus', 'Unknown Focus Area')}")
st.write("---")
|