Spaces:
Sleeping
Sleeping
Update ai_interpreter.py
Browse files- ai_interpreter.py +106 -25
ai_interpreter.py
CHANGED
|
@@ -7,62 +7,143 @@ Uses Hugging Face Space secrets for API credentials.
|
|
| 7 |
|
| 8 |
from typing import Dict, Any
|
| 9 |
import streamlit as st
|
|
|
|
| 10 |
from anthropic import Anthropic
|
| 11 |
|
| 12 |
class AstroAI:
|
| 13 |
"""Handles AI-powered astrological interpretations"""
|
| 14 |
|
| 15 |
def __init__(self):
|
| 16 |
-
#
|
| 17 |
-
api_key =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if not api_key:
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
self.client = Anthropic(api_key=api_key)
|
| 22 |
|
| 23 |
def _format_chart_data(self, chart_data: Dict[str, Any]) -> str:
|
| 24 |
"""Convert chart data to a readable format for the AI"""
|
| 25 |
-
formatted = "Birth Chart
|
| 26 |
|
| 27 |
# Format planetary positions
|
| 28 |
-
formatted += "
|
| 29 |
for planet, data in chart_data['planets'].items():
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# Format
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
return formatted
|
| 38 |
|
| 39 |
def get_interpretation(self, chart_data: Dict[str, Any], question: str) -> str:
|
| 40 |
-
"""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
{formatted_chart}
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
Please provide
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
response = self.client.messages.create(
|
| 57 |
model="claude-3-sonnet-20240229",
|
| 58 |
-
max_tokens=
|
|
|
|
| 59 |
messages=[{
|
| 60 |
"role": "user",
|
| 61 |
-
"content":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
}]
|
| 63 |
)
|
|
|
|
| 64 |
return response.content[0].text
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
-
error_message = f"Error generating
|
| 67 |
-
st.error(error_message)
|
| 68 |
return f"I apologize, but I encountered an error: {error_message}"
|
|
|
|
| 7 |
|
| 8 |
from typing import Dict, Any
|
| 9 |
import streamlit as st
|
| 10 |
+
import os
|
| 11 |
from anthropic import Anthropic
|
| 12 |
|
| 13 |
class AstroAI:
|
| 14 |
"""Handles AI-powered astrological interpretations"""
|
| 15 |
|
| 16 |
def __init__(self):
|
| 17 |
+
# Try to get API key from different sources
|
| 18 |
+
api_key = None
|
| 19 |
+
|
| 20 |
+
# First try Hugging Face secrets through streamlit
|
| 21 |
+
try:
|
| 22 |
+
api_key = st.secrets["CLAUDE_API_KEY"]
|
| 23 |
+
except Exception:
|
| 24 |
+
st.write("No API key found in Streamlit secrets, trying environment variables...")
|
| 25 |
+
|
| 26 |
+
# If not found in streamlit secrets, try environment variable
|
| 27 |
if not api_key:
|
| 28 |
+
api_key = os.getenv("CLAUDE_API_KEY")
|
| 29 |
+
|
| 30 |
+
# If still no API key, raise error
|
| 31 |
+
if not api_key:
|
| 32 |
+
raise ValueError(
|
| 33 |
+
"CLAUDE_API_KEY not found. Please set it either in:\n"
|
| 34 |
+
"1. Hugging Face Space secrets, or\n"
|
| 35 |
+
"2. Environment variables"
|
| 36 |
+
)
|
| 37 |
|
| 38 |
self.client = Anthropic(api_key=api_key)
|
| 39 |
|
| 40 |
def _format_chart_data(self, chart_data: Dict[str, Any]) -> str:
|
| 41 |
"""Convert chart data to a readable format for the AI"""
|
| 42 |
+
formatted = "Birth Chart Analysis:\n\n"
|
| 43 |
|
| 44 |
# Format planetary positions
|
| 45 |
+
formatted += "Planetary Positions:\n"
|
| 46 |
for planet, data in chart_data['planets'].items():
|
| 47 |
+
if 'error' not in data:
|
| 48 |
+
retrograde = data.get('retrograde', False)
|
| 49 |
+
house = data.get('house', 'Unknown')
|
| 50 |
+
formatted += (f"{planet}: {data['degrees']:.1f}° {data['sign']} "
|
| 51 |
+
f"in House {house}"
|
| 52 |
+
f"{' (Retrograde)' if retrograde else ''}\n")
|
| 53 |
+
|
| 54 |
+
# Format aspects if available
|
| 55 |
+
if 'aspects' in chart_data and chart_data['aspects']:
|
| 56 |
+
formatted += "\nMajor Aspects:\n"
|
| 57 |
+
for aspect in chart_data['aspects']:
|
| 58 |
+
formatted += (f"{aspect['planet1']} {aspect['aspect']} {aspect['planet2']} "
|
| 59 |
+
f"(Orb: {aspect['orb']}°, {aspect['nature']})\n")
|
| 60 |
|
| 61 |
+
# Format element and modality balances
|
| 62 |
+
if 'patterns' in chart_data:
|
| 63 |
+
if 'elements' in chart_data['patterns']:
|
| 64 |
+
formatted += "\nElement Balance:\n"
|
| 65 |
+
for element, count in chart_data['patterns']['elements'].items():
|
| 66 |
+
formatted += f"{element}: {count} planets\n"
|
| 67 |
|
| 68 |
+
if 'modalities' in chart_data['patterns']:
|
| 69 |
+
formatted += "\nModality Balance:\n"
|
| 70 |
+
for modality, count in chart_data['patterns']['modalities'].items():
|
| 71 |
+
formatted += f"{modality}: {count} planets\n"
|
| 72 |
+
|
| 73 |
return formatted
|
| 74 |
|
| 75 |
def get_interpretation(self, chart_data: Dict[str, Any], question: str) -> str:
|
| 76 |
+
"""Generate an astrological interpretation based on the chart and user's question"""
|
| 77 |
+
try:
|
| 78 |
+
formatted_chart = self._format_chart_data(chart_data)
|
| 79 |
+
|
| 80 |
+
system_prompt = """You are an expert astrologer with deep knowledge of both modern and traditional astrology.
|
| 81 |
+
When interpreting charts:
|
| 82 |
+
- Focus on the specific question asked while considering the whole chart context
|
| 83 |
+
- Provide practical, actionable insights
|
| 84 |
+
- Balance technical astrological terms with clear explanations
|
| 85 |
+
- Consider aspect patterns, dignities, and dispositors
|
| 86 |
+
- Include both challenging and supportive elements
|
| 87 |
+
- Be honest but constructive in your interpretations
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
response = self.client.messages.create(
|
| 91 |
+
model="claude-3-sonnet-20240229",
|
| 92 |
+
max_tokens=1000,
|
| 93 |
+
system=system_prompt,
|
| 94 |
+
messages=[{
|
| 95 |
+
"role": "user",
|
| 96 |
+
"content": f"""Based on the following birth chart data:
|
| 97 |
|
| 98 |
{formatted_chart}
|
| 99 |
|
| 100 |
+
Question: {question}
|
| 101 |
|
| 102 |
+
Please provide an insightful astrological interpretation focusing specifically on the question asked."""
|
| 103 |
+
}]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
return response.content[0].text
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
error_message = f"Error generating interpretation: {str(e)}"
|
| 110 |
+
st.error(error_message)
|
| 111 |
+
return f"I apologize, but I encountered an error: {error_message}"
|
| 112 |
+
|
| 113 |
+
def get_initial_analysis(self, chart_data: Dict[str, Any]) -> str:
|
| 114 |
+
"""Generate an initial overview analysis of the birth chart"""
|
| 115 |
try:
|
| 116 |
+
formatted_chart = self._format_chart_data(chart_data)
|
| 117 |
+
|
| 118 |
+
system_prompt = """You are a skilled astrologer providing initial birth chart readings.
|
| 119 |
+
In your analysis:
|
| 120 |
+
- Start with the most prominent features of the chart
|
| 121 |
+
- Highlight significant patterns and configurations
|
| 122 |
+
- Discuss the balance of elements and modalities
|
| 123 |
+
- Note any particularly strong or challenged planets
|
| 124 |
+
- Keep the tone informative and encouraging
|
| 125 |
+
- Use clear language while preserving astrological accuracy
|
| 126 |
+
|
| 127 |
+
Structure your response with clear sections and insights.
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
response = self.client.messages.create(
|
| 131 |
model="claude-3-sonnet-20240229",
|
| 132 |
+
max_tokens=1500,
|
| 133 |
+
system=system_prompt,
|
| 134 |
messages=[{
|
| 135 |
"role": "user",
|
| 136 |
+
"content": f"""Please provide an initial overview analysis of this birth chart:
|
| 137 |
+
|
| 138 |
+
{formatted_chart}
|
| 139 |
+
|
| 140 |
+
Focus on the most significant patterns and features that form the core themes of this chart."""
|
| 141 |
}]
|
| 142 |
)
|
| 143 |
+
|
| 144 |
return response.content[0].text
|
| 145 |
+
|
| 146 |
except Exception as e:
|
| 147 |
+
error_message = f"Error generating initial analysis: {str(e)}"
|
| 148 |
+
st.error(error_message)
|
| 149 |
return f"I apologize, but I encountered an error: {error_message}"
|