Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import pytz
|
|
| 4 |
from geopy.geocoders import Nominatim
|
| 5 |
from timezonefinder import TimezoneFinder
|
| 6 |
from astro_core import ChartCalculator
|
|
|
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
import numpy as np
|
| 9 |
import pandas as pd
|
|
@@ -36,27 +37,21 @@ st.markdown("""
|
|
| 36 |
padding: 0.5rem 1rem;
|
| 37 |
border: none;
|
| 38 |
}
|
| 39 |
-
.stButton > button:hover {
|
| 40 |
-
background-color: #553C9A;
|
| 41 |
-
}
|
| 42 |
.chat-message {
|
| 43 |
-
padding:
|
| 44 |
-
border-radius:
|
| 45 |
-
margin-bottom:
|
|
|
|
| 46 |
}
|
| 47 |
.user-message {
|
| 48 |
background-color: rgba(107, 70, 193, 0.1);
|
| 49 |
margin-left: 20%;
|
|
|
|
| 50 |
}
|
| 51 |
.assistant-message {
|
| 52 |
background-color: white;
|
| 53 |
margin-right: 20%;
|
| 54 |
-
|
| 55 |
-
.element-card {
|
| 56 |
-
background-color: white;
|
| 57 |
-
border-radius: 8px;
|
| 58 |
-
padding: 1rem;
|
| 59 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 60 |
}
|
| 61 |
</style>
|
| 62 |
""", unsafe_allow_html=True)
|
|
@@ -69,8 +64,14 @@ def init_session_state():
|
|
| 69 |
st.session_state.chat_history = []
|
| 70 |
if 'location_details' not in st.session_state:
|
| 71 |
st.session_state.location_details = None
|
| 72 |
-
if '
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def get_location_details(location_name: str) -> Dict[str, Any]:
|
| 76 |
"""Get coordinates and timezone for a location"""
|
|
@@ -93,104 +94,17 @@ def get_location_details(location_name: str) -> Dict[str, Any]:
|
|
| 93 |
except Exception as e:
|
| 94 |
return {'error': f"Error finding location: {str(e)}"}
|
| 95 |
|
| 96 |
-
def create_chart_visualization(chart_data: Dict[str, Any]):
|
| 97 |
-
"""Create an interactive circular chart visualization"""
|
| 98 |
-
fig = go.Figure()
|
| 99 |
-
|
| 100 |
-
# Add zodiac ring
|
| 101 |
-
zodiac_positions = list(range(0, 360, 30))
|
| 102 |
-
zodiac_signs = ['โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ']
|
| 103 |
-
|
| 104 |
-
for pos, sign in zip(zodiac_positions, zodiac_signs):
|
| 105 |
-
fig.add_annotation(
|
| 106 |
-
x=1.1 * np.cos(np.radians(pos)),
|
| 107 |
-
y=1.1 * np.sin(np.radians(pos)),
|
| 108 |
-
text=sign,
|
| 109 |
-
showarrow=False,
|
| 110 |
-
font=dict(size=20)
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
# Add planets
|
| 114 |
-
for planet, data in chart_data['planets'].items():
|
| 115 |
-
if 'error' not in data:
|
| 116 |
-
angle = np.radians(data['longitude'])
|
| 117 |
-
r = 0.8 # Radius for planets
|
| 118 |
-
fig.add_annotation(
|
| 119 |
-
x=r * np.cos(angle),
|
| 120 |
-
y=r * np.sin(angle),
|
| 121 |
-
text=planet,
|
| 122 |
-
showarrow=True,
|
| 123 |
-
arrowhead=2
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
# Update layout
|
| 127 |
-
fig.update_layout(
|
| 128 |
-
showlegend=False,
|
| 129 |
-
xaxis=dict(
|
| 130 |
-
range=[-1.2, 1.2],
|
| 131 |
-
showgrid=False,
|
| 132 |
-
zeroline=False,
|
| 133 |
-
showticklabels=False
|
| 134 |
-
),
|
| 135 |
-
yaxis=dict(
|
| 136 |
-
range=[-1.2, 1.2],
|
| 137 |
-
showgrid=False,
|
| 138 |
-
zeroline=False,
|
| 139 |
-
showticklabels=False
|
| 140 |
-
),
|
| 141 |
-
plot_bgcolor='rgba(0,0,0,0)',
|
| 142 |
-
paper_bgcolor='rgba(0,0,0,0)',
|
| 143 |
-
height=600
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
return fig
|
| 147 |
-
|
| 148 |
-
def display_chart_analysis(chart_data: Dict[str, Any]):
|
| 149 |
-
"""Display the chart analysis in a structured way"""
|
| 150 |
-
if 'error' in chart_data:
|
| 151 |
-
st.error(chart_data['error'])
|
| 152 |
-
return
|
| 153 |
-
|
| 154 |
-
# Create three columns for different aspects of the analysis
|
| 155 |
-
col1, col2, col3 = st.columns(3)
|
| 156 |
-
|
| 157 |
-
with col1:
|
| 158 |
-
st.markdown("### Planetary Positions")
|
| 159 |
-
for planet, data in chart_data['planets'].items():
|
| 160 |
-
if 'error' not in data:
|
| 161 |
-
st.markdown(f"""
|
| 162 |
-
<div class="element-card">
|
| 163 |
-
<strong>{planet}</strong><br>
|
| 164 |
-
{data['sign']} ({data['degrees']:.1f}ยฐ)<br>
|
| 165 |
-
House {data.get('house', 'Unknown')}<br>
|
| 166 |
-
{'๐ Retrograde' if data.get('retrograde', False) else ''}
|
| 167 |
-
</div>
|
| 168 |
-
""", unsafe_allow_html=True)
|
| 169 |
-
|
| 170 |
-
with col2:
|
| 171 |
-
st.markdown("### Elements & Qualities")
|
| 172 |
-
element_data = pd.DataFrame(chart_data['patterns']['elements'], index=['Count']).T
|
| 173 |
-
st.bar_chart(element_data)
|
| 174 |
-
|
| 175 |
-
quality_data = pd.DataFrame(chart_data['patterns']['modalities'], index=['Count']).T
|
| 176 |
-
st.bar_chart(quality_data)
|
| 177 |
-
|
| 178 |
-
with col3:
|
| 179 |
-
st.markdown("### Major Aspects")
|
| 180 |
-
for aspect in chart_data['aspects']:
|
| 181 |
-
st.markdown(f"""
|
| 182 |
-
<div class="element-card">
|
| 183 |
-
{aspect['planet1']} {aspect['aspect']} {aspect['planet2']}<br>
|
| 184 |
-
Orb: {aspect['orb']}ยฐ ({aspect['nature']})
|
| 185 |
-
</div>
|
| 186 |
-
""", unsafe_allow_html=True)
|
| 187 |
-
|
| 188 |
def main():
|
| 189 |
st.title("๐ AI Astrology Assistant")
|
| 190 |
|
| 191 |
# Initialize session state
|
| 192 |
init_session_state()
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
# Birth Information Form
|
| 195 |
with st.form("birth_info"):
|
| 196 |
st.subheader("Enter Birth Details")
|
|
@@ -237,12 +151,16 @@ def main():
|
|
| 237 |
if 'error' in st.session_state.birth_chart:
|
| 238 |
st.error(st.session_state.birth_chart['error'])
|
| 239 |
else:
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
# Display chart and analysis if available
|
| 243 |
if st.session_state.birth_chart and 'error' not in st.session_state.birth_chart:
|
| 244 |
-
|
| 245 |
-
tab1, tab2 = st.tabs(["Chart Visualization", "Detailed Analysis"])
|
| 246 |
|
| 247 |
with tab1:
|
| 248 |
st.plotly_chart(
|
|
@@ -251,11 +169,42 @@ def main():
|
|
| 251 |
)
|
| 252 |
|
| 253 |
with tab2:
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
# Chat interface
|
| 257 |
st.markdown("### ๐ฌ Ask about your chart")
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
if user_question:
|
| 261 |
# Add user message to chat history
|
|
@@ -264,8 +213,12 @@ def main():
|
|
| 264 |
"content": user_question
|
| 265 |
})
|
| 266 |
|
| 267 |
-
#
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
st.session_state.chat_history.append({
|
| 271 |
"role": "assistant",
|
|
@@ -273,14 +226,16 @@ def main():
|
|
| 273 |
})
|
| 274 |
|
| 275 |
# Display chat history
|
| 276 |
-
|
| 277 |
-
st.markdown(
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
{message['
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
| 284 |
|
| 285 |
if __name__ == "__main__":
|
| 286 |
main()
|
|
|
|
| 4 |
from geopy.geocoders import Nominatim
|
| 5 |
from timezonefinder import TimezoneFinder
|
| 6 |
from astro_core import ChartCalculator
|
| 7 |
+
from ai_interpreter import AstroAI
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
import numpy as np
|
| 10 |
import pandas as pd
|
|
|
|
| 37 |
padding: 0.5rem 1rem;
|
| 38 |
border: none;
|
| 39 |
}
|
|
|
|
|
|
|
|
|
|
| 40 |
.chat-message {
|
| 41 |
+
padding: 1.5rem;
|
| 42 |
+
border-radius: 10px;
|
| 43 |
+
margin-bottom: 1rem;
|
| 44 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
| 45 |
}
|
| 46 |
.user-message {
|
| 47 |
background-color: rgba(107, 70, 193, 0.1);
|
| 48 |
margin-left: 20%;
|
| 49 |
+
border-left: 4px solid #6B46C1;
|
| 50 |
}
|
| 51 |
.assistant-message {
|
| 52 |
background-color: white;
|
| 53 |
margin-right: 20%;
|
| 54 |
+
border-left: 4px solid #9F7AEA;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
}
|
| 56 |
</style>
|
| 57 |
""", unsafe_allow_html=True)
|
|
|
|
| 64 |
st.session_state.chat_history = []
|
| 65 |
if 'location_details' not in st.session_state:
|
| 66 |
st.session_state.location_details = None
|
| 67 |
+
if 'ai_interpreter' not in st.session_state:
|
| 68 |
+
try:
|
| 69 |
+
st.session_state.ai_interpreter = AstroAI()
|
| 70 |
+
except ValueError as e:
|
| 71 |
+
st.error(str(e))
|
| 72 |
+
st.session_state.ai_interpreter = None
|
| 73 |
+
if 'initial_analysis' not in st.session_state:
|
| 74 |
+
st.session_state.initial_analysis = None
|
| 75 |
|
| 76 |
def get_location_details(location_name: str) -> Dict[str, Any]:
|
| 77 |
"""Get coordinates and timezone for a location"""
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
return {'error': f"Error finding location: {str(e)}"}
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
def main():
|
| 98 |
st.title("๐ AI Astrology Assistant")
|
| 99 |
|
| 100 |
# Initialize session state
|
| 101 |
init_session_state()
|
| 102 |
|
| 103 |
+
# Show API key status
|
| 104 |
+
if st.session_state.ai_interpreter is None:
|
| 105 |
+
st.warning("โ ๏ธ Claude API key not found. Please add CLAUDE_API_KEY to your environment variables or Hugging Face secrets.")
|
| 106 |
+
return
|
| 107 |
+
|
| 108 |
# Birth Information Form
|
| 109 |
with st.form("birth_info"):
|
| 110 |
st.subheader("Enter Birth Details")
|
|
|
|
| 151 |
if 'error' in st.session_state.birth_chart:
|
| 152 |
st.error(st.session_state.birth_chart['error'])
|
| 153 |
else:
|
| 154 |
+
# Generate initial AI analysis
|
| 155 |
+
with st.spinner("Generating astrological insights..."):
|
| 156 |
+
st.session_state.initial_analysis = st.session_state.ai_interpreter.get_initial_analysis(
|
| 157 |
+
st.session_state.birth_chart
|
| 158 |
+
)
|
| 159 |
+
st.success("Birth chart calculated and analyzed successfully!")
|
| 160 |
+
|
| 161 |
# Display chart and analysis if available
|
| 162 |
if st.session_state.birth_chart and 'error' not in st.session_state.birth_chart:
|
| 163 |
+
tab1, tab2, tab3 = st.tabs(["Chart Visualization", "Technical Details", "AI Analysis"])
|
|
|
|
| 164 |
|
| 165 |
with tab1:
|
| 166 |
st.plotly_chart(
|
|
|
|
| 169 |
)
|
| 170 |
|
| 171 |
with tab2:
|
| 172 |
+
st.subheader("Planetary Positions")
|
| 173 |
+
for planet, data in st.session_state.birth_chart['planets'].items():
|
| 174 |
+
if 'error' not in data:
|
| 175 |
+
st.markdown(f"""
|
| 176 |
+
<div style='background-color: white; padding: 1rem; border-radius: 8px; margin-bottom: 0.5rem;'>
|
| 177 |
+
<strong>{planet}</strong>: {data['degrees']:.1f}ยฐ {data['sign']} in House {data.get('house', 'Unknown')}
|
| 178 |
+
{' ๐' if data.get('retrograde', False) else ''}
|
| 179 |
+
</div>
|
| 180 |
+
""", unsafe_allow_html=True)
|
| 181 |
+
|
| 182 |
+
with tab3:
|
| 183 |
+
if st.session_state.initial_analysis:
|
| 184 |
+
st.markdown(st.session_state.initial_analysis)
|
| 185 |
|
| 186 |
# Chat interface
|
| 187 |
st.markdown("### ๐ฌ Ask about your chart")
|
| 188 |
+
|
| 189 |
+
# Suggested questions
|
| 190 |
+
questions = [
|
| 191 |
+
"What are the main themes in my chart?",
|
| 192 |
+
"What are my career strengths?",
|
| 193 |
+
"How does my chart influence relationships?",
|
| 194 |
+
"What are my biggest challenges?",
|
| 195 |
+
"What are my spiritual or creative gifts?"
|
| 196 |
+
]
|
| 197 |
+
|
| 198 |
+
selected_question = st.selectbox(
|
| 199 |
+
"Choose a question or type your own below:",
|
| 200 |
+
[""] + questions
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
user_question = st.text_input(
|
| 204 |
+
"Your question:",
|
| 205 |
+
value=selected_question,
|
| 206 |
+
key="user_input"
|
| 207 |
+
)
|
| 208 |
|
| 209 |
if user_question:
|
| 210 |
# Add user message to chat history
|
|
|
|
| 213 |
"content": user_question
|
| 214 |
})
|
| 215 |
|
| 216 |
+
# Get AI interpretation
|
| 217 |
+
with st.spinner("Generating interpretation..."):
|
| 218 |
+
response = st.session_state.ai_interpreter.get_interpretation(
|
| 219 |
+
st.session_state.birth_chart,
|
| 220 |
+
user_question
|
| 221 |
+
)
|
| 222 |
|
| 223 |
st.session_state.chat_history.append({
|
| 224 |
"role": "assistant",
|
|
|
|
| 226 |
})
|
| 227 |
|
| 228 |
# Display chat history
|
| 229 |
+
if st.session_state.chat_history:
|
| 230 |
+
st.markdown("#### Previous Interpretations")
|
| 231 |
+
for message in st.session_state.chat_history:
|
| 232 |
+
st.markdown(
|
| 233 |
+
f"""<div class="chat-message {'user-message' if message['role'] == 'user' else 'assistant-message'}">
|
| 234 |
+
<strong>{'๐ค You' if message['role'] == 'user' else '๐ฎ Assistant'}:</strong><br>
|
| 235 |
+
{message['content']}
|
| 236 |
+
</div>""",
|
| 237 |
+
unsafe_allow_html=True
|
| 238 |
+
)
|
| 239 |
|
| 240 |
if __name__ == "__main__":
|
| 241 |
main()
|