Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
# app.py
|
| 2 |
"""
|
| 3 |
-
|
| 4 |
-
Handles the user interface and coordinates between astrology calculations and AI interpretation.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import streamlit as st
|
| 8 |
from datetime import datetime
|
| 9 |
import pytz
|
|
|
|
|
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
from astro_core import ChartCalculator
|
| 12 |
from ai_interpreter import AstroAI
|
|
@@ -20,6 +21,33 @@ def init_session_state():
|
|
| 20 |
st.session_state.birth_chart = None
|
| 21 |
if 'chat_history' not in st.session_state:
|
| 22 |
st.session_state.chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def main():
|
| 25 |
st.title("AI Astrology Assistant")
|
|
@@ -27,64 +55,104 @@ def main():
|
|
| 27 |
# Initialize session state
|
| 28 |
init_session_state()
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
st.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# Combine date and time
|
| 49 |
-
birth_datetime = datetime.combine(date, time)
|
| 50 |
|
| 51 |
-
# Calculate birth chart
|
| 52 |
-
st.session_state.birth_chart = calculator.calculate_birth_chart(
|
| 53 |
-
birth_datetime, latitude, longitude, timezone
|
| 54 |
-
)
|
| 55 |
-
st.success("Chart calculated!")
|
| 56 |
-
|
| 57 |
-
# Display areas
|
| 58 |
-
col1, col2 = st.columns([2, 3])
|
| 59 |
-
|
| 60 |
-
with col1:
|
| 61 |
-
st.subheader("Birth Chart")
|
| 62 |
-
if st.session_state.birth_chart:
|
| 63 |
# Display planetary positions
|
| 64 |
-
st.write("
|
| 65 |
for planet, data in st.session_state.birth_chart['planets'].items():
|
| 66 |
st.write(f"{planet}: {data['degrees']:.2f}° {data['sign']} in House {data['house']}")
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
with st.spinner("Generating interpretation..."):
|
|
|
|
| 75 |
interpretation = interpreter.get_interpretation(
|
| 76 |
st.session_state.birth_chart,
|
| 77 |
-
|
| 78 |
)
|
| 79 |
st.session_state.chat_history.append({
|
| 80 |
-
"question":
|
| 81 |
"answer": interpretation
|
| 82 |
})
|
| 83 |
|
| 84 |
# Display chat history
|
|
|
|
| 85 |
for exchange in st.session_state.chat_history:
|
| 86 |
-
st.
|
| 87 |
-
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
main()
|
|
|
|
| 1 |
# app.py
|
| 2 |
"""
|
| 3 |
+
Streamlined Streamlit application for AI Astrology app with simplified user input.
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import streamlit as st
|
| 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
|
|
|
|
| 21 |
st.session_state.birth_chart = None
|
| 22 |
if 'chat_history' not in st.session_state:
|
| 23 |
st.session_state.chat_history = []
|
| 24 |
+
if 'user_info' not in st.session_state:
|
| 25 |
+
st.session_state.user_info = None
|
| 26 |
+
if 'location_coords' not in st.session_state:
|
| 27 |
+
st.session_state.location_coords = None
|
| 28 |
+
|
| 29 |
+
def get_coordinates(location_name: str) -> tuple:
|
| 30 |
+
"""Get coordinates from location name using geocoding"""
|
| 31 |
+
try:
|
| 32 |
+
geolocator = Nominatim(user_agent="ai_astrology_app")
|
| 33 |
+
location = geolocator.geocode(location_name)
|
| 34 |
+
if location:
|
| 35 |
+
return location.latitude, location.longitude
|
| 36 |
+
return None
|
| 37 |
+
except (GeocoderTimedOut, GeocoderServiceError):
|
| 38 |
+
return None
|
| 39 |
+
|
| 40 |
+
def calculate_chart(user_data: dict):
|
| 41 |
+
"""Calculate birth chart based on user data"""
|
| 42 |
+
calculator = ChartCalculator()
|
| 43 |
+
birth_datetime = datetime.combine(user_data['birth_date'], user_data['birth_time'])
|
| 44 |
+
|
| 45 |
+
return calculator.calculate_birth_chart(
|
| 46 |
+
birth_datetime,
|
| 47 |
+
user_data['latitude'],
|
| 48 |
+
user_data['longitude'],
|
| 49 |
+
user_data['timezone']
|
| 50 |
+
)
|
| 51 |
|
| 52 |
def main():
|
| 53 |
st.title("AI Astrology Assistant")
|
|
|
|
| 55 |
# Initialize session state
|
| 56 |
init_session_state()
|
| 57 |
|
| 58 |
+
# User Information Form
|
| 59 |
+
with st.form("user_info_form"):
|
| 60 |
+
st.subheader("Personal Information")
|
| 61 |
+
|
| 62 |
+
col1, col2 = st.columns(2)
|
| 63 |
+
with col1:
|
| 64 |
+
name = st.text_input("Full Name")
|
| 65 |
+
gender = st.selectbox("Gender", ["Male", "Female", "Non-binary", "Prefer not to say"])
|
| 66 |
+
birth_date = st.date_input("Birth Date", min_value=datetime(1900, 1, 1))
|
| 67 |
+
|
| 68 |
+
with col2:
|
| 69 |
+
birth_time = st.time_input("Birth Time")
|
| 70 |
+
birth_place = st.text_input("Birth Place (City, Country)")
|
| 71 |
+
timezone = st.selectbox("Timezone", pytz.all_timezones)
|
| 72 |
+
|
| 73 |
+
submit_button = st.form_submit_button("Generate Birth Chart")
|
| 74 |
+
|
| 75 |
+
if submit_button and birth_place:
|
| 76 |
+
coordinates = get_coordinates(birth_place)
|
| 77 |
+
if coordinates:
|
| 78 |
+
latitude, longitude = coordinates
|
| 79 |
+
st.session_state.location_coords = coordinates
|
| 80 |
+
|
| 81 |
+
# Store user information
|
| 82 |
+
st.session_state.user_info = {
|
| 83 |
+
'name': name,
|
| 84 |
+
'gender': gender,
|
| 85 |
+
'birth_date': birth_date,
|
| 86 |
+
'birth_time': birth_time,
|
| 87 |
+
'birth_place': birth_place,
|
| 88 |
+
'latitude': latitude,
|
| 89 |
+
'longitude': longitude,
|
| 90 |
+
'timezone': timezone
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
# Calculate birth chart
|
| 94 |
+
with st.spinner("Calculating your birth chart..."):
|
| 95 |
+
st.session_state.birth_chart = calculate_chart(st.session_state.user_info)
|
| 96 |
+
st.success("Birth chart calculated successfully!")
|
| 97 |
+
else:
|
| 98 |
+
st.error("Could not find coordinates for the given location. Please check the location name.")
|
| 99 |
|
| 100 |
+
# Display areas (only show if birth chart is calculated)
|
| 101 |
+
if st.session_state.birth_chart and st.session_state.user_info:
|
| 102 |
+
col1, col2 = st.columns([2, 3])
|
| 103 |
+
|
| 104 |
+
with col1:
|
| 105 |
+
st.subheader(f"Birth Chart for {st.session_state.user_info['name']}")
|
| 106 |
+
|
| 107 |
+
# Display birth details
|
| 108 |
+
st.write("**Birth Details:**")
|
| 109 |
+
st.write(f"Date: {st.session_state.user_info['birth_date'].strftime('%B %d, %Y')}")
|
| 110 |
+
st.write(f"Time: {st.session_state.user_info['birth_time'].strftime('%H:%M')}")
|
| 111 |
+
st.write(f"Location: {st.session_state.user_info['birth_place']}")
|
|
|
|
|
|
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Display planetary positions
|
| 114 |
+
st.write("**Planetary Positions:**")
|
| 115 |
for planet, data in st.session_state.birth_chart['planets'].items():
|
| 116 |
st.write(f"{planet}: {data['degrees']:.2f}° {data['sign']} in House {data['house']}")
|
| 117 |
+
|
| 118 |
+
with col2:
|
| 119 |
+
st.subheader("AI Interpretation")
|
| 120 |
+
# Suggested questions
|
| 121 |
+
question_templates = [
|
| 122 |
+
"What are the main themes in my birth chart?",
|
| 123 |
+
f"How does my {st.session_state.user_info['gender'].lower()} energy manifest in this chart?",
|
| 124 |
+
"What career paths might suit me based on my chart?",
|
| 125 |
+
"What are my relationship patterns according to this chart?",
|
| 126 |
+
"What are my greatest strengths based on this chart?"
|
| 127 |
+
]
|
| 128 |
+
|
| 129 |
+
selected_question = st.selectbox(
|
| 130 |
+
"Choose a question or type your own below:",
|
| 131 |
+
[""] + question_templates
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
custom_question = st.text_input(
|
| 135 |
+
"Ask about your chart:",
|
| 136 |
+
value=selected_question
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
if custom_question:
|
| 140 |
with st.spinner("Generating interpretation..."):
|
| 141 |
+
interpreter = AstroAI()
|
| 142 |
interpretation = interpreter.get_interpretation(
|
| 143 |
st.session_state.birth_chart,
|
| 144 |
+
custom_question
|
| 145 |
)
|
| 146 |
st.session_state.chat_history.append({
|
| 147 |
+
"question": custom_question,
|
| 148 |
"answer": interpretation
|
| 149 |
})
|
| 150 |
|
| 151 |
# Display chat history
|
| 152 |
+
st.write("**Previous Interpretations:**")
|
| 153 |
for exchange in st.session_state.chat_history:
|
| 154 |
+
with st.expander(f"Q: {exchange['question']}", expanded=False):
|
| 155 |
+
st.write(exchange['answer'])
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
main()
|