Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,15 +19,31 @@ IMAGEGEN_API_URL = "https://api-inference.huggingface.co/models/Artples/LAI-Imag
|
|
| 19 |
# Headers for Hugging Face API requests
|
| 20 |
headers = {"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"}
|
| 21 |
|
| 22 |
-
# CSS
|
| 23 |
st.markdown("""
|
| 24 |
<style>
|
| 25 |
-
body {
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
.
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
</style>
|
| 32 |
""", unsafe_allow_html=True)
|
| 33 |
|
|
@@ -57,7 +73,7 @@ def fetch_real_data(city: str) -> dict:
|
|
| 57 |
"weather_condition": weather_data['weather'][0].get('main', 'Data not available')
|
| 58 |
}
|
| 59 |
|
| 60 |
-
# Function to determine mood
|
| 61 |
def determine_mood(data: dict) -> str:
|
| 62 |
weather_condition = data["weather_condition"].lower()
|
| 63 |
temperature = data["temperature"]
|
|
@@ -90,11 +106,48 @@ def generate_story_with_ai(narrative: str, mood: str) -> str:
|
|
| 90 |
)
|
| 91 |
return response.choices[0].message['content'].strip()
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# Function to fetch NGOs using OpenAI
|
| 94 |
def fetch_nearby_ngos_with_openai(city: str, interests: list) -> list:
|
| 95 |
prompt = (
|
| 96 |
f"List NGOs near {city} that focus on {', '.join(interests)}. "
|
| 97 |
-
"Provide the names, locations, and focus areas in JSON format."
|
| 98 |
)
|
| 99 |
response = openai.ChatCompletion.create(
|
| 100 |
model="gpt-3.5-turbo",
|
|
@@ -103,8 +156,14 @@ def fetch_nearby_ngos_with_openai(city: str, interests: list) -> list:
|
|
| 103 |
temperature=0.7
|
| 104 |
)
|
| 105 |
response_content = response.choices[0].message['content'].strip()
|
|
|
|
| 106 |
try:
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
st.error(f"Error fetching NGO data: {e}")
|
| 110 |
return []
|
|
@@ -151,7 +210,7 @@ if st.button("Find Nearby NGOs"):
|
|
| 151 |
if st.session_state.ngos:
|
| 152 |
st.subheader("π NGOs Near You")
|
| 153 |
for ngo in st.session_state.ngos:
|
| 154 |
-
st.write(f"**{ngo
|
| 155 |
-
st.write(f"π Location: {ngo
|
| 156 |
-
st.write(f"π± Focus Area: {ngo
|
| 157 |
st.write("---")
|
|
|
|
| 19 |
# Headers for Hugging Face API requests
|
| 20 |
headers = {"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"}
|
| 21 |
|
| 22 |
+
# Inject custom CSS for green theme
|
| 23 |
st.markdown("""
|
| 24 |
<style>
|
| 25 |
+
body {
|
| 26 |
+
background-color: #ffffff;
|
| 27 |
+
}
|
| 28 |
+
.stApp {
|
| 29 |
+
color: #2e7d32;
|
| 30 |
+
font-family: 'Arial', sans-serif;
|
| 31 |
+
}
|
| 32 |
+
.stButton>button {
|
| 33 |
+
background-color: #66bb6a;
|
| 34 |
+
color: #fff;
|
| 35 |
+
font-weight: bold;
|
| 36 |
+
}
|
| 37 |
+
.stTextInput>div>input {
|
| 38 |
+
background-color: #e8f5e9;
|
| 39 |
+
color: #2e7d32;
|
| 40 |
+
}
|
| 41 |
+
.stMarkdown h1, .stMarkdown h2, .stMarkdown h3, .stMarkdown p {
|
| 42 |
+
color: #388e3c;
|
| 43 |
+
}
|
| 44 |
+
.stMarkdown h2 {
|
| 45 |
+
font-weight: bold;
|
| 46 |
+
}
|
| 47 |
</style>
|
| 48 |
""", unsafe_allow_html=True)
|
| 49 |
|
|
|
|
| 73 |
"weather_condition": weather_data['weather'][0].get('main', 'Data not available')
|
| 74 |
}
|
| 75 |
|
| 76 |
+
# Function to determine mood based on weather data
|
| 77 |
def determine_mood(data: dict) -> str:
|
| 78 |
weather_condition = data["weather_condition"].lower()
|
| 79 |
temperature = data["temperature"]
|
|
|
|
| 106 |
)
|
| 107 |
return response.choices[0].message['content'].strip()
|
| 108 |
|
| 109 |
+
# Function to generate simulated environmental data
|
| 110 |
+
def generate_simulated_data(city: str) -> dict:
|
| 111 |
+
prompt = (
|
| 112 |
+
f"Generate simulated environmental data for {city} in JSON format with fields:\n"
|
| 113 |
+
f"1. AQI\n2. Deforestation Rate\n3. Water Quality\n4. Biodiversity Impact"
|
| 114 |
+
)
|
| 115 |
+
response = openai.ChatCompletion.create(
|
| 116 |
+
model="gpt-3.5-turbo",
|
| 117 |
+
messages=[{"role": "user", "content": prompt}],
|
| 118 |
+
max_tokens=100,
|
| 119 |
+
temperature=0.8
|
| 120 |
+
)
|
| 121 |
+
response_content = response.choices[0].message['content'].strip()
|
| 122 |
+
try:
|
| 123 |
+
return eval(response_content)
|
| 124 |
+
except Exception as e:
|
| 125 |
+
st.error(f"Error parsing simulated data: {e}")
|
| 126 |
+
return {}
|
| 127 |
+
|
| 128 |
+
# Function to generate music from Hugging Face API
|
| 129 |
+
def generate_music(description: str) -> bytes:
|
| 130 |
+
payload = {"inputs": description}
|
| 131 |
+
response = requests.post(MUSICGEN_API_URL, headers=headers, json=payload)
|
| 132 |
+
if response.status_code != 200:
|
| 133 |
+
st.error(f"Error generating music: {response.status_code} {response.text}")
|
| 134 |
+
return None
|
| 135 |
+
return response.content
|
| 136 |
+
|
| 137 |
+
# Function to generate an image based on the story
|
| 138 |
+
def generate_image(description: str) -> bytes:
|
| 139 |
+
payload = {"inputs": description}
|
| 140 |
+
response = requests.post(IMAGEGEN_API_URL, headers=headers, json=payload)
|
| 141 |
+
if response.status_code != 200:
|
| 142 |
+
st.error(f"Error generating image: {response.status_code} {response.text}")
|
| 143 |
+
return None
|
| 144 |
+
return response.content
|
| 145 |
+
|
| 146 |
# Function to fetch NGOs using OpenAI
|
| 147 |
def fetch_nearby_ngos_with_openai(city: str, interests: list) -> list:
|
| 148 |
prompt = (
|
| 149 |
f"List NGOs near {city} that focus on {', '.join(interests)}. "
|
| 150 |
+
"Provide the names, locations, and focus areas in JSON format as a list of dictionaries."
|
| 151 |
)
|
| 152 |
response = openai.ChatCompletion.create(
|
| 153 |
model="gpt-3.5-turbo",
|
|
|
|
| 156 |
temperature=0.7
|
| 157 |
)
|
| 158 |
response_content = response.choices[0].message['content'].strip()
|
| 159 |
+
|
| 160 |
try:
|
| 161 |
+
ngo_list = eval(response_content)
|
| 162 |
+
if isinstance(ngo_list, list) and all(isinstance(ngo, dict) for ngo in ngo_list):
|
| 163 |
+
return ngo_list
|
| 164 |
+
else:
|
| 165 |
+
st.error("Unexpected response format. Could not parse NGO data.")
|
| 166 |
+
return []
|
| 167 |
except Exception as e:
|
| 168 |
st.error(f"Error fetching NGO data: {e}")
|
| 169 |
return []
|
|
|
|
| 210 |
if st.session_state.ngos:
|
| 211 |
st.subheader("π NGOs Near You")
|
| 212 |
for ngo in st.session_state.ngos:
|
| 213 |
+
st.write(f"**{ngo.get('name', 'Unknown NGO')}**")
|
| 214 |
+
st.write(f"π Location: {ngo.get('location', 'Unknown Location')}")
|
| 215 |
+
st.write(f"π± Focus Area: {ngo.get('focus', 'Unknown Focus Area')}")
|
| 216 |
st.write("---")
|