Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from datetime import datetime | |
| import pytz | |
| from geopy.geocoders import Nominatim | |
| from timezonefinder import TimezoneFinder | |
| from astro_core import ChartCalculator | |
| from ai_interpreter import AstroAI | |
| import plotly.graph_objects as go | |
| import numpy as np | |
| import pandas as pd | |
| from typing import Dict, Any | |
| # Page configuration | |
| st.set_page_config( | |
| page_title="AI Astrology Assistant", | |
| page_icon="🌟", | |
| layout="wide", | |
| initial_sidebar_state="expanded" | |
| ) | |
| # Custom CSS | |
| st.markdown(""" | |
| <style> | |
| .stApp { | |
| background: linear-gradient(135deg, #f5f7ff 0%, #f8f0ff 100%); | |
| } | |
| .main { | |
| background-color: transparent; | |
| } | |
| .stTextInput > div > div > input { | |
| background-color: rgba(255, 255, 255, 0.8); | |
| } | |
| .stButton > button { | |
| background-color: #6B46C1; | |
| color: white; | |
| border-radius: 8px; | |
| padding: 0.5rem 1rem; | |
| border: none; | |
| } | |
| .chat-message { | |
| padding: 1.5rem; | |
| border-radius: 10px; | |
| margin-bottom: 1rem; | |
| box-shadow: 0 2px 4px rgba(0,0,0,0.05); | |
| } | |
| .user-message { | |
| background-color: rgba(107, 70, 193, 0.1); | |
| margin-left: 20%; | |
| border-left: 4px solid #6B46C1; | |
| } | |
| .assistant-message { | |
| background-color: white; | |
| margin-right: 20%; | |
| border-left: 4px solid #9F7AEA; | |
| } | |
| .element-card { | |
| background-color: white; | |
| padding: 1rem; | |
| border-radius: 8px; | |
| margin-bottom: 0.5rem; | |
| box-shadow: 0 2px 4px rgba(0,0,0,0.05); | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| def get_safe_planet_data(chart_data: Dict[str, Any], planet: str) -> Dict[str, Any]: | |
| """Safely get planet data with error handling.""" | |
| try: | |
| planet_data = chart_data.get('planets', {}).get(planet, {}) | |
| if 'error' in planet_data or not planet_data: | |
| return {'error': f"Could not calculate {planet} position"} | |
| return planet_data | |
| except Exception as e: | |
| return {'error': str(e)} | |
| def create_chart_visualization(chart_data: Dict[str, Any]) -> go.Figure: | |
| """Create a professional astrological chart visualization.""" | |
| fig = go.Figure() | |
| # Define colors for zodiac signs and create the chart | |
| # ... | |
| return fig | |
| def init_session_state(): | |
| """Initialize session state variables.""" | |
| if 'birth_chart' not in st.session_state: | |
| st.session_state.birth_chart = None | |
| if 'chat_history' not in st.session_state: | |
| st.session_state.chat_history = [] | |
| if 'location_details' not in st.session_state: | |
| st.session_state.location_details = None | |
| if 'ai_interpreter' not in st.session_state: | |
| try: | |
| st.session_state.ai_interpreter = AstroAI() | |
| except ValueError as e: | |
| st.error(str(e)) | |
| st.session_state.ai_interpreter = None | |
| if 'initial_analysis' not in st.session_state: | |
| st.session_state.initial_analysis = None | |
| def get_location_details(location_name: str) -> Dict[str, Any]: | |
| """Get coordinates and timezone for a location.""" | |
| try: | |
| geolocator = Nominatim(user_agent="ai_astrology_app") | |
| tf = TimezoneFinder() | |
| location = geolocator.geocode(location_name) | |
| if location: | |
| latitude, longitude = location.latitude, location.longitude | |
| timezone_str = tf.timezone_at(lat=latitude, lng=longitude) or 'UTC' | |
| return { | |
| 'latitude': latitude, | |
| 'longitude': longitude, | |
| 'timezone': timezone_str, | |
| 'error': None | |
| } | |
| return {'error': 'Location not found'} | |
| except Exception as e: | |
| return {'error': f"Error finding location: {str(e)}"} | |
| def display_chart_details(chart_data: Dict[str, Any]): | |
| """Display detailed chart information.""" | |
| col1, col2, col3 = st.columns(3) | |
| # Display planetary positions, aspects, and elements/modalities | |
| # ... | |
| def main(): | |
| st.title("🌟 AI Astrology Assistant") | |
| init_session_state() | |
| if st.session_state.ai_interpreter is None: | |
| st.warning("⚠️ Astro AI not initialized. Please configure your API key.") | |
| return | |
| # Birth Information Form | |
| with st.form("birth_info"): | |
| st.subheader("Enter Birth Details") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| birth_date = st.date_input( | |
| "Birth Date", | |
| min_value=datetime(1900, 1, 1), | |
| max_value=datetime.now() | |
| ) | |
| birth_time = st.time_input("Birth Time") | |
| with col2: | |
| birth_place = st.text_input("Birth Place", placeholder="City, Country") | |
| calculate_button = st.form_submit_button("Calculate Birth Chart") | |
| if calculate_button: | |
| if not birth_place: | |
| st.error("Please enter your birth place.") | |
| else: | |
| with st.spinner("Calculating..."): | |
| location = get_location_details(birth_place) | |
| if location.get('error'): | |
| st.error(location['error']) | |
| else: | |
| calculator = ChartCalculator() | |
| birth_datetime = datetime.combine(birth_date, birth_time) | |
| st.session_state.birth_chart = calculator.calculate_birth_chart( | |
| birth_datetime, | |
| location['latitude'], | |
| location['longitude'] | |
| ) | |
| st.success("Birth chart calculated!") | |
| # Display results | |
| if st.session_state.birth_chart: | |
| tab1, tab2, tab3 = st.tabs(["Birth Chart", "Details", "AI Analysis"]) | |
| with tab1: | |
| st.plotly_chart(create_chart_visualization(st.session_state.birth_chart)) | |
| with tab2: | |
| display_chart_details(st.session_state.birth_chart) | |
| with tab3: | |
| st.write(st.session_state.initial_analysis) | |
| if __name__ == "__main__": | |
| main() | |