import os import random import gradio as gr import torch from transformers import pipeline from datetime import datetime # Game configuration FORTUNE_TYPES = ["love", "career", "health", "wealth", "general"] TAROT_CARDS = [ "The Fool", "The Magician", "The High Priestess", "The Empress", "The Emperor", "The Hierophant", "The Lovers", "The Chariot", "Strength", "The Hermit", "Wheel of Fortune", "Justice", "The Hanged Man", "Death", "Temperance", "The Devil", "The Tower", "The Star", "The Moon", "The Sun", "Judgement", "The World" ] ZODIAC_SIGNS = [ "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces" ] # Initialize model generator = None def load_model(): """Load the model with GPU support""" global generator try: print("Loading model...") # Use a simple text generation model model_name = "distilgpt2" # Set device device = 0 if torch.cuda.is_available() else -1 # Create text generation pipeline generator = pipeline( "text-generation", model=model_name, device=device ) if torch.cuda.is_available(): print(f"Model loaded on GPU: {torch.cuda.get_device_name(0)}") else: print("Model loaded on CPU") return True except Exception as e: print(f"Error loading model: {str(e)}") return False def generate_fortune(fortune_type, user_input=""): """Generate a fortune based on type and user input""" # Base prompts for different fortune types prompts = { "love": "Tell me a romantic fortune about love and relationships:", "career": "Tell me a professional fortune about career and success:", "health": "Tell me a health-related fortune about wellness:", "wealth": "Tell me a financial fortune about money and prosperity:", "general": "Tell me a general fortune about life and future:" } # Select random tarot card and zodiac sign tarot_card = random.choice(TAROT_CARDS) zodiac_sign = random.choice(ZODIAC_SIGNS) # Create prompt prompt_text = prompts.get(fortune_type, prompts["general"]) full_prompt = f"{prompt_text} The {tarot_card} card is present. Zodiac sign: {zodiac_sign}. {user_input}" fortune_text = "" try: if generator: # Generate fortune text generated = generator( full_prompt, max_length=100, num_return_sequences=1, temperature=0.9, do_sample=True, truncation=True ) fortune_text = generated[0]['generated_text'] # Clean up the text if fortune_text.startswith(full_prompt): fortune_text = fortune_text[len(full_prompt):].strip() # Ensure we have some text if not fortune_text or len(fortune_text) < 10: fortune_text = f"The {tarot_card} reveals mysteries for {zodiac_sign}. Good fortune awaits." else: # Fallback fortunes fallback_fortunes = [ f"The {tarot_card} card shines brightly. As a {zodiac_sign}, your path is clear.", f"{tarot_card} guides you. {zodiac_sign} energy brings unexpected joy.", f"Through {tarot_card}'s wisdom, your {zodiac_sign} journey continues.", f"Stars align with {tarot_card}. {zodiac_sign}'s destiny unfolds.", f"{tarot_card} whispers secrets. {zodiac_sign}'s future is bright." ] fortune_text = random.choice(fallback_fortunes) except Exception as e: print(f"Error generating fortune: {str(e)}") fortune_text = f"The {tarot_card} card appears. {zodiac_sign}'s journey continues with promise." # Create fortune details fortune_details = { "Tarot Card": tarot_card, "Zodiac Sign": zodiac_sign, "Lucky Number": str(random.randint(1, 99)), "Lucky Color": random.choice(["Red", "Blue", "Green", "Gold", "Purple", "Silver"]), "Element": random.choice(["Fire", "Water", "Earth", "Air"]), "Time": datetime.now().strftime("%Y-%m-%d %H:%M:%S") } return fortune_text, fortune_details def get_fortune(fortune_type, question): """Gradio interface function""" fortune_text, details = generate_fortune(fortune_type, question) # Format details details_html = "