cryogenic22 commited on
Commit
dcb5fe5
·
verified ·
1 Parent(s): 0169d33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # app.py
2
  """
3
- Streamlined Streamlit application for AI Astrology app with simplified user input.
4
  """
5
 
6
  import streamlit as st
@@ -8,6 +8,7 @@ from datetime import datetime
8
  import pytz
9
  from geopy.geocoders import Nominatim
10
  from geopy.exc import GeocoderTimedOut, GeocoderServiceError
 
11
  from dotenv import load_dotenv
12
  from astro_core import ChartCalculator
13
  from ai_interpreter import AstroAI
@@ -28,16 +29,34 @@ def init_session_state():
28
  if 'calculation_error' not in st.session_state:
29
  st.session_state.calculation_error = None
30
 
31
- def get_coordinates(location_name: str) -> tuple:
32
- """Get coordinates from location name using geocoding"""
33
  try:
 
34
  geolocator = Nominatim(user_agent="ai_astrology_app")
 
 
 
35
  location = geolocator.geocode(location_name)
36
  if location:
37
- return location.latitude, location.longitude
38
- return None
39
- except (GeocoderTimedOut, GeocoderServiceError):
40
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  def calculate_chart(user_data: dict):
43
  """Calculate birth chart based on user data"""
@@ -81,12 +100,6 @@ def main():
81
  with col2:
82
  birth_time = st.time_input("Birth Time")
83
  birth_place = st.text_input("Birth Place (City, Country)")
84
- # Get local timezone based on location if possible, otherwise show selector
85
- timezone = st.selectbox(
86
- "Timezone",
87
- pytz.common_timezones, # Using common timezones instead of all timezones
88
- index=pytz.common_timezones.index('UTC')
89
- )
90
 
91
  submit_button = st.form_submit_button("Generate Birth Chart")
92
 
@@ -94,11 +107,12 @@ def main():
94
  if not birth_place:
95
  st.error("Please enter your birth place.")
96
  else:
97
- coordinates = get_coordinates(birth_place)
98
- if coordinates:
99
- latitude, longitude = coordinates
100
- st.session_state.location_coords = coordinates
101
-
 
102
  # Reset error state
103
  st.session_state.calculation_error = None
104
 
@@ -109,11 +123,14 @@ def main():
109
  'birth_date': birth_date,
110
  'birth_time': birth_time,
111
  'birth_place': birth_place,
112
- 'latitude': latitude,
113
- 'longitude': longitude,
114
- 'timezone': timezone
115
  }
116
 
 
 
 
117
  # Calculate birth chart
118
  with st.spinner("Calculating your birth chart..."):
119
  st.session_state.birth_chart = calculate_chart(st.session_state.user_info)
@@ -122,8 +139,6 @@ def main():
122
  st.success("Birth chart calculated successfully!")
123
  elif st.session_state.calculation_error:
124
  st.error(f"Error calculating birth chart: {st.session_state.calculation_error}")
125
- else:
126
- st.error("Could not find coordinates for the given location. Please check the location name.")
127
 
128
  # Display areas (only show if birth chart is calculated)
129
  if st.session_state.birth_chart and st.session_state.user_info:
@@ -137,6 +152,7 @@ def main():
137
  st.write(f"Date: {st.session_state.user_info['birth_date'].strftime('%B %d, %Y')}")
138
  st.write(f"Time: {st.session_state.user_info['birth_time'].strftime('%H:%M')}")
139
  st.write(f"Location: {st.session_state.user_info['birth_place']}")
 
140
 
141
  # Display planetary positions
142
  st.write("**Planetary Positions:**")
 
1
  # app.py
2
  """
3
+ Streamlined Streamlit application for AI Astrology app with automatic timezone detection.
4
  """
5
 
6
  import streamlit as st
 
8
  import pytz
9
  from geopy.geocoders import Nominatim
10
  from geopy.exc import GeocoderTimedOut, GeocoderServiceError
11
+ from timezonefinder import TimezoneFinder
12
  from dotenv import load_dotenv
13
  from astro_core import ChartCalculator
14
  from ai_interpreter import AstroAI
 
29
  if 'calculation_error' not in st.session_state:
30
  st.session_state.calculation_error = None
31
 
32
+ def get_location_details(location_name: str) -> dict:
33
+ """Get coordinates and timezone from location name"""
34
  try:
35
+ # Initialize geocoder and timezone finder
36
  geolocator = Nominatim(user_agent="ai_astrology_app")
37
+ tf = TimezoneFinder()
38
+
39
+ # Get location
40
  location = geolocator.geocode(location_name)
41
  if location:
42
+ latitude, longitude = location.latitude, location.longitude
43
+
44
+ # Get timezone string for the coordinates
45
+ timezone_str = tf.timezone_at(lat=latitude, lng=longitude)
46
+ if not timezone_str:
47
+ timezone_str = 'UTC' # Fallback to UTC if timezone not found
48
+
49
+ return {
50
+ 'latitude': latitude,
51
+ 'longitude': longitude,
52
+ 'timezone': timezone_str,
53
+ 'error': None
54
+ }
55
+ return {'error': 'Location not found'}
56
+ except (GeocoderTimedOut, GeocoderServiceError) as e:
57
+ return {'error': f'Geocoding error: {str(e)}'}
58
+ except Exception as e:
59
+ return {'error': f'Unexpected error: {str(e)}'}
60
 
61
  def calculate_chart(user_data: dict):
62
  """Calculate birth chart based on user data"""
 
100
  with col2:
101
  birth_time = st.time_input("Birth Time")
102
  birth_place = st.text_input("Birth Place (City, Country)")
 
 
 
 
 
 
103
 
104
  submit_button = st.form_submit_button("Generate Birth Chart")
105
 
 
107
  if not birth_place:
108
  st.error("Please enter your birth place.")
109
  else:
110
+ with st.spinner("Looking up location details..."):
111
+ location_details = get_location_details(birth_place)
112
+
113
+ if location_details.get('error'):
114
+ st.error(location_details['error'])
115
+ else:
116
  # Reset error state
117
  st.session_state.calculation_error = None
118
 
 
123
  'birth_date': birth_date,
124
  'birth_time': birth_time,
125
  'birth_place': birth_place,
126
+ 'latitude': location_details['latitude'],
127
+ 'longitude': location_details['longitude'],
128
+ 'timezone': location_details['timezone']
129
  }
130
 
131
+ # Display detected timezone
132
+ st.info(f"Detected timezone for {birth_place}: {location_details['timezone']}")
133
+
134
  # Calculate birth chart
135
  with st.spinner("Calculating your birth chart..."):
136
  st.session_state.birth_chart = calculate_chart(st.session_state.user_info)
 
139
  st.success("Birth chart calculated successfully!")
140
  elif st.session_state.calculation_error:
141
  st.error(f"Error calculating birth chart: {st.session_state.calculation_error}")
 
 
142
 
143
  # Display areas (only show if birth chart is calculated)
144
  if st.session_state.birth_chart and st.session_state.user_info:
 
152
  st.write(f"Date: {st.session_state.user_info['birth_date'].strftime('%B %d, %Y')}")
153
  st.write(f"Time: {st.session_state.user_info['birth_time'].strftime('%H:%M')}")
154
  st.write(f"Location: {st.session_state.user_info['birth_place']}")
155
+ st.write(f"Timezone: {st.session_state.user_info['timezone']}")
156
 
157
  # Display planetary positions
158
  st.write("**Planetary Positions:**")