cryogenic22 commited on
Commit
f794e31
·
verified ·
1 Parent(s): 4ae008c

Create ai_interpreter.py

Browse files
Files changed (1) hide show
  1. ai_interpreter.py +68 -0
ai_interpreter.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ai_interpreter.py
2
+ """
3
+ AI interpretation module using Claude API.
4
+ Handles astrological interpretations and user interactions.
5
+ Uses Hugging Face Space secrets for API credentials.
6
+ """
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
+ # Get API key from Hugging Face Space secrets
17
+ api_key = st.secrets.get("CLAUDE_API_KEY")
18
+ if not api_key:
19
+ raise ValueError("CLAUDE_API_KEY not found in secrets. Please configure it in your Hugging Face Space settings.")
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 Positions:\n\n"
26
+
27
+ # Format planetary positions
28
+ formatted += "Planets:\n"
29
+ for planet, data in chart_data['planets'].items():
30
+ formatted += f"{planet}: {data['degrees']}° {data['sign']} in House {data['house']}\n"
31
+
32
+ # Format house cusps
33
+ formatted += "\nHouse Cusps:\n"
34
+ for house, data in chart_data['houses'].items():
35
+ formatted += f"{house}: {data['degrees']}° {data['sign']}\n"
36
+
37
+ return formatted
38
+
39
+ def get_interpretation(self, chart_data: Dict[str, Any], question: str) -> str:
40
+ """
41
+ Generate an astrological interpretation based on the chart and user's question
42
+ """
43
+ formatted_chart = self._format_chart_data(chart_data)
44
+
45
+ # Construct the prompt
46
+ prompt = f"""As an astrological AI assistant, provide an interpretation based on the following birth chart data:
47
+
48
+ {formatted_chart}
49
+
50
+ User's Question: {question}
51
+
52
+ Please provide a clear and insightful interpretation focusing specifically on the question asked."""
53
+
54
+ # Get response from Claude
55
+ try:
56
+ response = self.client.messages.create(
57
+ model="claude-3-sonnet-20240229",
58
+ max_tokens=1000,
59
+ messages=[{
60
+ "role": "user",
61
+ "content": prompt
62
+ }]
63
+ )
64
+ return response.content[0].text
65
+ except Exception as e:
66
+ error_message = f"Error generating interpretation: {str(e)}"
67
+ st.error(error_message) # Display error in Streamlit UI
68
+ return f"I apologize, but I encountered an error: {error_message}"