Spaces:
Runtime error
Runtime error
| from app.services.chathistory import ChatSession | |
| from app.services.environmental_condition import EnvironmentalData | |
| def map_air_quality_index(aqi): | |
| if aqi <= 50: | |
| return {"displayValue": "Good", "value": aqi, "color": "#00C853"} | |
| elif aqi <= 100: | |
| return {"displayValue": "Moderate", "value": aqi, "color": "#FFB74D"} | |
| elif aqi <= 150: | |
| return {"displayValue": "Unhealthy Tolerate", "value": aqi, "color": "#FF7043"} | |
| elif aqi <= 200: | |
| return {"displayValue": "Unhealthy", "value": aqi, "color": "#E53935"} | |
| else: | |
| return {"displayValue": "Very Unhealthy", "value": aqi, "color": "#8E24AA"} | |
| def map_pollution_level(aqi): | |
| if aqi <= 50: | |
| return 20 | |
| elif aqi <= 100: | |
| return 40 | |
| elif aqi <= 150: | |
| return 60 | |
| elif aqi <= 200: | |
| return 80 | |
| else: | |
| return 100 | |
| class CityNotProvidedError(Exception): | |
| pass | |
| class EnvironmentalConditions: | |
| def __init__(self, session_id): | |
| self.session_id = session_id | |
| self.chat_session = ChatSession(session_id, "session_id") | |
| self.user_city = self.chat_session.get_city() | |
| if not self.user_city: | |
| raise CityNotProvidedError("City information is required but not provided") | |
| self.city = self.user_city | |
| self.environment_data = EnvironmentalData(self.city) | |
| def get_conditon(self): | |
| data = self.environment_data.get_environmental_data() | |
| formatted_data = [ | |
| { | |
| "label": "Humidity", | |
| # Handle decimal values by converting to float first | |
| "value": int(float(data['Humidity'].strip(' %'))), | |
| "color": "#4FC3F7", | |
| "icon": "FaTint", | |
| "type": "numeric" | |
| }, | |
| { | |
| "label": "UV Rays", | |
| "value": data['UV_Index'] * 10, | |
| "color": "#FFB74D", | |
| "icon": "FaSun", | |
| "type": "numeric" | |
| }, | |
| { | |
| "label": "Pollution", | |
| "value": map_pollution_level(data['Air Quality Index']), | |
| "color": "#F06292", | |
| "icon": "FaLeaf", | |
| "type": "numeric" | |
| }, | |
| { | |
| "label": "Air Quality", | |
| **map_air_quality_index(data['Air Quality Index']), | |
| "icon": "FaCloud", | |
| "type": "categorical" | |
| }, | |
| { | |
| "label": "Wind", | |
| "value": float(data['Wind Speed'].strip(' m/s')) * 10, | |
| "color": "#9575CD", | |
| "icon": "FaWind", | |
| "type": "numeric" | |
| }, | |
| { | |
| "label": "Temperature", | |
| "value": int(float(data['Temperature'].strip(' °C'))), | |
| "color": "#FF7043", | |
| "icon": "FaThermometerHalf", | |
| "type": "numeric" | |
| } | |
| ] | |
| return formatted_data |