soctopus2327 commited on
Commit
fb0eb6d
·
verified ·
1 Parent(s): d4af652

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -106,25 +106,6 @@ def generate_story_with_ai(narrative: str, mood: str) -> str:
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}
@@ -174,15 +155,36 @@ st.write("Enter a city to explore real-time environmental data, generate AI-crea
174
 
175
  city = st.text_input("Enter City Name:", placeholder="Type the name of a city...")
176
 
177
- if st.button("Generate Environmental Data"):
178
  st.session_state.real_data = fetch_real_data(city)
179
  if st.session_state.real_data:
 
180
  narrative = create_narrative(city, st.session_state.real_data)
181
  mood = determine_mood(st.session_state.real_data)
 
 
182
  st.session_state.story = generate_story_with_ai(narrative, mood)
183
 
184
- # Display environmental data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  if st.session_state.real_data:
 
 
 
 
186
  st.subheader("📊 Real Weather Data")
187
  st.write("Temperature (°C):", st.session_state.real_data.get("temperature", "Data not available"))
188
  st.write("Humidity (%):", st.session_state.real_data.get("humidity", "Data not available"))
 
106
  )
107
  return response.choices[0].message['content'].strip()
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  # Function to generate music from Hugging Face API
110
  def generate_music(description: str) -> bytes:
111
  payload = {"inputs": description}
 
155
 
156
  city = st.text_input("Enter City Name:", placeholder="Type the name of a city...")
157
 
158
+ if st.button("Generate Environmental Data, Music, and Image"):
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 Music and Image at the Top
174
+ if st.session_state.music_bytes:
175
+ st.subheader("🎶 Generated Music")
176
+ st.audio(BytesIO(st.session_state.music_bytes), format="audio/wav")
177
+
178
+ if st.session_state.image_bytes:
179
+ st.subheader("🖼️ Generated Image")
180
+ st.image(Image.open(BytesIO(st.session_state.image_bytes)), caption="Generated Image based on Story", use_column_width=True)
181
+
182
+ # Display Environmental Narrative and Data
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")
189
  st.write("Temperature (°C):", st.session_state.real_data.get("temperature", "Data not available"))
190
  st.write("Humidity (%):", st.session_state.real_data.get("humidity", "Data not available"))