Spaces:
Runtime error
Runtime error
Download app.py from DerrickHodge/oduIfaOracle: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/spaces/DerrickHodge/oduIfaOracle/resolve/main/app.py
- Command line
-
hf download hf://spaces/DerrickHodge/oduIfaOracle/app.py
-
curl -L -o app.py https://huggingface.co/spaces/DerrickHodge/oduIfaOracle/resolve/main/app.py
11 kB
| import numpy as np | |
| from typing import Tuple, Dict | |
| from itertools import product | |
| from groq import Groq | |
| import os | |
| import gradio as gr | |
| class OduIfa: | |
| def __init__(self): | |
| self.odu_names = { | |
| "α": "Eji Ogbe", "β": "Oyeku Meji", "γ": "Iwori Meji", "δ": "Odi Meji", | |
| "ε": "Irosun Meji", "η": "Owonrin Meji", "θ": "Obara Meji", "λ": "Okanran Meji", | |
| "μ": "Ogunda Meji", "ρ": "Osa Meji", "σ": "Ika Meji", "τ": "Oturupon Meji", | |
| "φ": "Otura Meji", "χ": "Irete Meji", "ψ": "Ose Meji", "ω": "Ofun Meji" | |
| } | |
| self.odu_matrices = self._initialize_odu_matrices() | |
| self.greek_to_matrix = {k: v for k, v in zip(self.odu_names.keys(), self.odu_matrices)} | |
| self.matrix_to_greek = {tuple(map(tuple, v.tolist())): k for k, v in self.greek_to_matrix.items()} | |
| self.odu_interpretations = self._initialize_odu_interpretations() | |
| def _initialize_odu_matrices(self): | |
| return [ | |
| np.array([[0,0],[0,0],[0,0],[0,0]]), np.array([[1,1],[1,1],[1,1],[1,1]]), | |
| np.array([[1,1],[0,0],[0,0],[1,1]]), np.array([[0,0],[1,1],[1,1],[0,0]]), | |
| np.array([[0,0],[0,0],[1,1],[1,1]]), np.array([[1,1],[1,1],[0,0],[0,0]]), | |
| np.array([[0,0],[1,1],[1,1],[1,1]]), np.array([[1,1],[1,1],[1,1],[0,0]]), | |
| np.array([[0,0],[0,0],[0,0],[1,1]]), np.array([[1,1],[1,1],[0,0],[1,1]]), | |
| np.array([[1,1],[0,0],[1,1],[1,1]]), np.array([[1,1],[0,0],[0,0],[0,0]]), | |
| np.array([[0,0],[1,1],[0,0],[0,0]]), np.array([[0,0],[0,0],[1,1],[0,0]]), | |
| np.array([[0,0],[1,1],[0,0],[1,1]]), np.array([[1,1],[0,0],[1,1],[0,0]]) | |
| ] | |
| def _initialize_odu_interpretations(self) -> Dict[str, str]: | |
| return { | |
| "α": "Perfect alignment with destiny through good character development.", | |
| "β": "End of a cycle, transformation, and new beginnings.", | |
| "γ": "Ability to identify and pursue real possibilities.", | |
| "δ": "Manifestation and irreversible change, like birth.", | |
| "ε": "Protection of memories and respect for the past.", | |
| "η": "Choices within one's potential affecting manifestation.", | |
| "θ": "Humility as a path to abundance and overcoming arrogance.", | |
| "λ": "Focused change and new beginnings towards specific goals.", | |
| "μ": "Courage to face obstacles and take responsibility.", | |
| "ρ": "Balance and consequences of broken promises or equilibrium.", | |
| "σ": "Avoidance of negativity and embracing positivity.", | |
| "τ": "Dealing with instability and spiritual consequences of choices.", | |
| "φ": "Harmony through self-examination for wisdom and growth.", | |
| "χ": "Potential for spiritual initiation and life fulfillment.", | |
| "ψ": "Sacrifice leading to family and financial blessings.", | |
| "ω": "Alignment with destiny and effectiveness of spiritual discipline." | |
| } | |
| def add_mod2(self, a: np.ndarray, b: np.ndarray) -> np.ndarray: | |
| return np.mod(a + b, 2) | |
| def cast_divination(self) -> Tuple[str, str, str]: | |
| right_hand_index = np.random.choice(len(self.odu_matrices)) | |
| left_hand_index = np.random.choice(len(self.odu_matrices)) | |
| right_hand = self.odu_matrices[right_hand_index] | |
| left_hand = self.odu_matrices[left_hand_index] | |
| right_odu = self.matrix_to_greek[tuple(map(tuple, right_hand.tolist()))] | |
| left_odu = self.matrix_to_greek[tuple(map(tuple, left_hand.tolist()))] | |
| combined = self.add_mod2(right_hand, left_hand) | |
| result_odu = self.matrix_to_greek[tuple(map(tuple, combined.tolist()))] | |
| return right_odu, left_odu, result_odu | |
| def interpret_odu(self, odu: str) -> str: | |
| return self.odu_interpretations[odu] | |
| def interpret_combination(self, right_odu: str, left_odu: str, result_odu: str) -> str: | |
| right_interp = self.interpret_odu(right_odu) | |
| left_interp = self.interpret_odu(left_odu) | |
| result_interp = self.interpret_odu(result_odu) | |
| return f"The combination of {self.odu_names[right_odu]} ({right_interp}) and {self.odu_names[left_odu]} ({left_interp}) results in {self.odu_names[result_odu]}. This suggests: {result_interp}" | |
| def perform_divination(self): | |
| right_odu, left_odu, result_odu = self.cast_divination() | |
| print(f"Right hand cast: {right_odu} - {self.odu_names[right_odu]}") | |
| print(f"Left hand cast: {left_odu} - {self.odu_names[left_odu]}") | |
| print(f"Combined Odu: {result_odu} - {self.odu_names[result_odu]}") | |
| print("\nInterpretation:") | |
| print(self.interpret_combination(right_odu, left_odu, result_odu)) | |
| def get_divination_result(self): | |
| right_odu, left_odu, result_odu = self.cast_divination() | |
| interpretation = self.interpret_combination(right_odu, left_odu, result_odu) | |
| analysis_result = self.perform_analyses(right_odu, left_odu, result_odu) | |
| return f""" | |
| Right hand cast: {right_odu} - {self.odu_names[right_odu]} | |
| Left hand cast: {left_odu} - {self.odu_names[left_odu]} | |
| Combined Odu: {result_odu} - {self.odu_names[result_odu]} | |
| Interpretation: | |
| {interpretation} | |
| Odu Ifa System Analysis: | |
| {analysis_result} | |
| """ | |
| # Analysis methods (not used in main divination flow) | |
| def generate_cayley_table(self): | |
| cayley_table = {} | |
| for a, b in product(self.odu_names.keys(), repeat=2): | |
| result = self.add_mod2(self.greek_to_matrix[a], self.greek_to_matrix[b]) | |
| cayley_table[(a, b)] = self.matrix_to_greek[tuple(map(tuple, result.tolist()))] | |
| return cayley_table | |
| def get_relevant_cayley_entries(self, right_odu, left_odu, result_odu): | |
| cayley_table = self.generate_cayley_table() | |
| return f""" | |
| {right_odu} + {left_odu} = {cayley_table[(right_odu, left_odu)]} | |
| {left_odu} + {right_odu} = {cayley_table[(left_odu, right_odu)]} | |
| {result_odu} + {result_odu} = {cayley_table[(result_odu, result_odu)]} | |
| """ | |
| def check_combination_symmetry(self, right_odu, left_odu): | |
| cayley_table = self.generate_cayley_table() | |
| return "Symmetric" if cayley_table[(right_odu, left_odu)] == cayley_table[(left_odu, right_odu)] else "Asymmetric" | |
| def is_self_inversed(self): | |
| return all(self.add_mod2(m, m).sum() == 0 for m in self.odu_matrices) | |
| def is_abelian(self): | |
| cayley_table = self.generate_cayley_table() | |
| return all(cayley_table[(a,b)] == cayley_table[(b,a)] for a, b in product(self.odu_names.keys(), repeat=2)) | |
| def find_idempotent_elements(self): | |
| return [k for k, v in self.greek_to_matrix.items() if (self.add_mod2(v, v) == v).all()] | |
| def is_center_whole_group(self): | |
| return self.is_abelian() # For abelian groups, the center is the whole group | |
| def perform_analyses(self, right_odu, left_odu, result_odu): | |
| cayley_entries = self.get_relevant_cayley_entries(right_odu, left_odu, result_odu) | |
| combination_symmetry = self.check_combination_symmetry(right_odu, left_odu) | |
| matrix_analysis = self.analyze_odu_matrix(result_odu) | |
| visual_representation = self.get_visual_representation(result_odu) | |
| return f""" | |
| Specific Divination Analysis: | |
| Relevant Cayley Table entries: | |
| {cayley_entries} | |
| Combination symmetry: {combination_symmetry} | |
| Matrix Analysis of result Odu ({result_odu}): | |
| {matrix_analysis} | |
| Visual Representation: | |
| {visual_representation} | |
| """ | |
| def analyze_odu_matrix(self, odu): | |
| matrix = self.greek_to_matrix[odu] | |
| row_sums = matrix.sum(axis=1) | |
| col_sums = matrix.sum(axis=0) | |
| total_sum = matrix.sum() | |
| pattern_analysis = self.analyze_matrix_pattern(matrix) | |
| return f""" | |
| Matrix representation: | |
| {matrix} | |
| Row sums: {row_sums} | |
| Column sums: {col_sums} | |
| Total sum: {total_sum} | |
| Pattern analysis: {pattern_analysis} | |
| """ | |
| def analyze_matrix_pattern(self, matrix): | |
| patterns = [] | |
| if np.all(matrix == 0): | |
| patterns.append("All elements are 0, representing complete absence or void.") | |
| elif np.all(matrix == 1): | |
| patterns.append("All elements are 1, representing completeness or fullness.") | |
| if np.all(matrix.sum(axis=1) % 2 == 0): | |
| patterns.append("All rows have even sums, suggesting balance in horizontal aspects.") | |
| if np.all(matrix.sum(axis=0) % 2 == 0): | |
| patterns.append("All columns have even sums, suggesting balance in vertical aspects.") | |
| if np.trace(matrix) == matrix.shape[0]: | |
| patterns.append("The diagonal is all 1s, indicating strong self-identity.") | |
| return " ".join(patterns) | |
| def get_visual_representation(self, odu): | |
| matrix = self.greek_to_matrix[odu] | |
| visual = "" | |
| for row in matrix: | |
| visual += "".join("I" if cell else "II" for cell in row) + "\n" | |
| return visual | |
| def get_ai_interpretation(divination_result, user_question): | |
| client = Groq(api_key="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") | |
| system_prompt = { | |
| "role": "system", | |
| "content": "You are a knowledgeable Bàbálawó Yoruba Priest schooled the science of Odu Ifa and African Spiritual Traditions. Provide concise yet insightful interpretations of Ifa divinations, drawing on traditional Yoruba wisdom and the 256 Odu proverbs and axioms. Correlate the mathematical properties and matrix analysis into your interpretation to the spiritual meaning. Conclude with key takeaways or practical steps for the querent." | |
| } | |
| chat_history = [ | |
| system_prompt, | |
| {"role": "user", "content": f"The querent asks: '{user_question}'\n\nBased on this question and the following Ifa divination result, please provide a concise interpretation, explaining its significance and potential meaning for the querent. Incorporate the matrix analysis in your interpretation:\n\n{divination_result}"} | |
| ] | |
| response = client.chat.completions.create( | |
| model="llama3-70b-8192", | |
| messages=chat_history, | |
| max_tokens=2000, | |
| temperature=0.7 | |
| ) | |
| return response.choices[0].message.content | |
| def perform_divination(question): | |
| divination = OduIfa() | |
| divination_result = divination.get_divination_result() | |
| ai_interpretation = get_ai_interpretation(divination_result, question) | |
| full_result = f""" | |
| Question: {question} | |
| {divination_result} | |
| AI-Powered Babalawo Interpretation: | |
| {ai_interpretation} | |
| """ | |
| return full_result | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=perform_divination, | |
| inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."), | |
| outputs="text", | |
| title="Odu Ifa Divination System", | |
| description="Consult the Odu Ifa oracle by entering your question. Receive a divination result and AI-powered interpretation.", | |
| theme="default" | |
| ) | |
| # Launch the Gradio app | |
| if __name__ == "__main__": | |
| iface.launch() |