import os import random import requests import streamlit as st from dotenv import load_dotenv from PIL import Image import io load_dotenv() # Page config st.set_page_config( page_title="šŸ§™ā€ā™‚ļø Wizard Spells Generator", page_icon="šŸŖ„", layout="centered" ) # Title and description st.title("šŸ§™ā€ā™‚ļø Magical Spell Visualizer šŸŖ„") st.markdown("Generate a random wizard spell with magical visualization!") # Get HF token hf_token = os.getenv("HF_TOKEN") def fetch_random_spell(): """Fetch one random spell from Wizard World API""" try: response = requests.get("https://wizard-world-api.herokuapp.com/Spells") response.raise_for_status() spells = response.json() # Select one random spell random_spell = random.choice(spells) name = random_spell.get("name", "Unknown Spell") effect = random_spell.get("effect", "unknown effect") return {"name": name, "effect": effect} except Exception as e: st.error(f"Error fetching spell: {e}") return None def generate_spell_image(spell_name, spell_effect): """Generate image for spell using HuggingFace API""" if not hf_token: st.warning("HF_TOKEN not found. Image generation disabled.") return None try: API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell" headers = {"Authorization": f"Bearer {hf_token}"} prompt = f"magical wizard casting spell: {spell_name}, effect: {spell_effect}, fantasy art, mystical, detailed" with st.spinner("Generating magical image..."): response = requests.post(API_URL, headers=headers, json={"inputs": prompt}) if response.status_code == 200: image = Image.open(io.BytesIO(response.content)) return image else: st.error(f"Image generation failed: {response.status_code}") return None except Exception as e: st.error(f"Error generating image: {e}") return None # Main interface if st.button("šŸŽ² Generate Random Spell!", type="primary", use_container_width=True): # Fetch one spell spell = fetch_random_spell() if spell: spell_name = spell["name"] spell_effect = spell["effect"] # Create two columns col1, col2 = st.columns([1, 1]) with col1: st.subheader("šŸ“œ Your Spell") st.markdown(f"**{spell_name}**") st.markdown(f"*{spell_effect}*") with col2: st.subheader("šŸŽØ Spell Visualization") # Generate and display image image = generate_spell_image(spell_name, spell_effect) if image: st.image(image, caption=f"Visualization of {spell_name}", use_container_width=True) else: st.info("šŸ–¼ļø Image generation not available") # Fallback: show a magical emoji pattern st.markdown("```\nāœØšŸŒŸā­šŸŒ™āœØ\nšŸ”®šŸŖ„šŸ§™ā€ā™‚ļøšŸŖ„šŸ”®\nāœØšŸŒŸā­šŸŒ™āœØ\n```") # Sidebar with info with st.sidebar: st.header("šŸ§™ā€ā™‚ļø About") st.markdown(""" This magical app generates a random wizard spell and creates a visualization for it! **Features:** - šŸŽ² Random spell generation - šŸŽØ AI-powered spell visualizations - šŸ“œ Clean spell display - šŸ”® Mystical interface **How to use:** Click the "Generate Random Spell!" button to get a random spell with its magical visualization. """) st.header("šŸŖ„ Spell Sources") st.markdown("Spells sourced from the [Wizard World API](https://wizard-world-api.herokuapp.com/)") if hf_token: st.success("šŸŽØ Image generation enabled") else: st.warning("šŸŽØ Image generation disabled (no HF token)") # Footer st.markdown("---") st.markdown("*May your spell be ever powerful!* āš”šŸ§™ā€ā™‚ļøāœØ")