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+
import random
|
| 3 |
+
import re
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from huggingface_hub import InferenceClient, snapshot_download
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
import shutil
|
| 8 |
+
|
| 9 |
+
# Descargar y preparar dataset de tarot
|
| 10 |
+
def prepare_tarot_dataset():
|
| 11 |
+
dataset_path = Path("tarot_dataset")
|
| 12 |
+
image_dir = dataset_path / "images"
|
| 13 |
+
image_dir.mkdir(parents=True, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
# Descargar dataset
|
| 16 |
+
dataset = load_dataset("multimodalart/1920-raider-waite-tarot-public-domain", split="train")
|
| 17 |
+
|
| 18 |
+
# Procesar metadatos y organizar imágenes
|
| 19 |
+
card_map = {}
|
| 20 |
+
pattern = re.compile(r'"([^"]+)"$') # Para extraer el nombre de la carta
|
| 21 |
+
|
| 22 |
+
for item in dataset:
|
| 23 |
+
text = item["text"]
|
| 24 |
+
match = pattern.search(text)
|
| 25 |
+
if match:
|
| 26 |
+
card_name = match.group(1).lower()
|
| 27 |
+
src_path = Path(item["file_name"])
|
| 28 |
+
dest_path = image_dir / f"{card_name.replace(' ', '_')}.jpg"
|
| 29 |
+
|
| 30 |
+
if not dest_path.exists():
|
| 31 |
+
try:
|
| 32 |
+
# Copiar imagen desde el dataset descargado
|
| 33 |
+
shutil.copy(src_path, dest_path)
|
| 34 |
+
except:
|
| 35 |
+
continue
|
| 36 |
+
|
| 37 |
+
card_map[card_name] = str(dest_path)
|
| 38 |
+
|
| 39 |
+
return card_map
|
| 40 |
+
|
| 41 |
+
# Preparar dataset y obtener mapeo de cartas
|
| 42 |
+
CARD_MAP = prepare_tarot_dataset()
|
| 43 |
+
CARDS = list(CARD_MAP.keys())
|
| 44 |
+
|
| 45 |
+
# System Prompt mejorado
|
| 46 |
+
TAROTIST_PROMPT = """
|
| 47 |
+
Eres Madame Esmeralda, vidente tercera generación con dones psíquicos. Tu estilo:
|
| 48 |
+
🔮 Combinas simbolismo cabalístico, astrología y quiromancia
|
| 49 |
+
🌙 Usas un lenguaje poético con rimas sutiles
|
| 50 |
+
💫 Incluye predicciones a 3 plazos (corto, medio y largo)
|
| 51 |
+
🌿 Relaciona las cartas con elementos naturales
|
| 52 |
+
✨ Termina con un presagio y recomendación mágica
|
| 53 |
+
|
| 54 |
+
Técnica de lectura:
|
| 55 |
+
1. Analiza posición cósmica actual
|
| 56 |
+
2. Desentraña relaciones kármicas
|
| 57 |
+
3. Revela desafíos ocultos
|
| 58 |
+
4. Señala oportunidades místicas
|
| 59 |
+
5. Ofrece protección espiritual
|
| 60 |
+
|
| 61 |
+
Formato respuesta:
|
| 62 |
+
📜 **Profecía Celestial** (emoji relacionado)
|
| 63 |
+
Verso poético de 4 líneas
|
| 64 |
+
|
| 65 |
+
🌌 **Desglose Arcano**:
|
| 66 |
+
- Influencia planetaria
|
| 67 |
+
- Elemento dominante
|
| 68 |
+
- Chakras afectados
|
| 69 |
+
|
| 70 |
+
🗝️ **Clave del Destino**:
|
| 71 |
+
Consejo práctico con hierba/color/cristal
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
client = InferenceClient(provider="hf-inference", api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxx")
|
| 75 |
+
|
| 76 |
+
def get_reading(cards):
|
| 77 |
+
messages = [
|
| 78 |
+
{"role": "system", "content": TAROTIST_PROMPT},
|
| 79 |
+
{"role": "user", "content": f"Realiza lectura completa para: {', '.join(cards)}. Incluye predicciones temporales."}
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
+
completion = client.chat.completions.create(
|
| 83 |
+
model="meta-llama/Llama-3.3-70B-Instruct",
|
| 84 |
+
messages=messages,
|
| 85 |
+
max_tokens=700,
|
| 86 |
+
)
|
| 87 |
+
return completion.choices[0].message.content
|
| 88 |
+
|
| 89 |
+
def draw_cards(spread_type):
|
| 90 |
+
spreads = {
|
| 91 |
+
"Oráculo Rápido": 1,
|
| 92 |
+
"Tríada del Destino": 3,
|
| 93 |
+
"Cruz Mística": 5,
|
| 94 |
+
"Círculo Zodiacal": 12
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
num_cards = spreads.get(spread_type, 3)
|
| 98 |
+
drawn = random.sample(CARDS, num_cards)
|
| 99 |
+
|
| 100 |
+
images = [CARD_MAP[card] for card in drawn if card in CARD_MAP]
|
| 101 |
+
return images, drawn
|
| 102 |
+
|
| 103 |
+
def process_spread(spread_type):
|
| 104 |
+
images, cards = draw_cards(spread_type)
|
| 105 |
+
return images, "\n".join(f"🔮 {c.capitalize()}" for c in cards)
|
| 106 |
+
|
| 107 |
+
def full_reading(_, cards_text):
|
| 108 |
+
cards = [line.replace("🔮 ", "").lower() for line in cards_text.split("\n")]
|
| 109 |
+
reading = get_reading(cards)
|
| 110 |
+
return f"## 📜 Lectura Mística 📜\n\n{reading}"
|
| 111 |
+
|
| 112 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
| 113 |
+
gr.Markdown("# <center>🔮 Vidente del Alma 🔮</center>")
|
| 114 |
+
|
| 115 |
+
with gr.Row(variant="panel"):
|
| 116 |
+
gr.Image("mystic_banner.png", label="Madame Esmeralda", width=200)
|
| 117 |
+
with gr.Column():
|
| 118 |
+
spread_btn = gr.Radio(
|
| 119 |
+
choices=["Oráculo Rápido", "Tríada del Destino", "Cruz Mística", "Círculo Zodiacal"],
|
| 120 |
+
label="Selección del Oráculo",
|
| 121 |
+
elem_classes="mystic-radio"
|
| 122 |
+
)
|
| 123 |
+
draw_btn = gr.Button("🌌 Invocar Cartas 🌌", variant="primary")
|
| 124 |
+
|
| 125 |
+
with gr.Row():
|
| 126 |
+
gallery = gr.Gallery(
|
| 127 |
+
label="Cartas Invocadas",
|
| 128 |
+
columns=6,
|
| 129 |
+
height=300,
|
| 130 |
+
object_fit="cover"
|
| 131 |
+
)
|
| 132 |
+
cards_text = gr.Textbox(
|
| 133 |
+
label="Energías Reveladas",
|
| 134 |
+
interactive=False,
|
| 135 |
+
elem_classes="mystic-text"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
reading_btn = gr.Button("🧿 Revelar Profecía 🧿", variant="primary")
|
| 139 |
+
reading_output = gr.Markdown("### Que los astros guíen tu camino...", elem_id="prophecy-box")
|
| 140 |
+
|
| 141 |
+
draw_btn.click(
|
| 142 |
+
process_spread,
|
| 143 |
+
inputs=spread_btn,
|
| 144 |
+
outputs=[gallery, cards_text]
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
reading_btn.click(
|
| 148 |
+
full_reading,
|
| 149 |
+
inputs=[spread_btn, cards_text],
|
| 150 |
+
outputs=reading_output
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
if __name__ == "__main__":
|
| 154 |
+
demo.launch(
|
| 155 |
+
css=".mystic-radio {color: #4a148c} .mystic-text {font-family: 'Papyrus'} #prophecy-box {background: #f3e5f5}"
|
| 156 |
+
)
|