Spaces:
Sleeping
Sleeping
Download app.py from tjrtm/Wizard_Spells: direct link, hf CLI and curl.
- Browser
- Download file 4.08 kB
-
https://huggingface.co/spaces/tjrtm/Wizard_Spells/resolve/main/app.py
- Command line
-
hf download hf://spaces/tjrtm/Wizard_Spells/app.py
-
curl -L -o app.py https://huggingface.co/spaces/tjrtm/Wizard_Spells/resolve/main/app.py
4.08 kB
| 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!* โก๐งโโ๏ธโจ") | |