Spaces:
Sleeping

Wizard_Spells / app.py
tjrtm
Simplify to generate only one spell
0570e7e
Raw History Blame Contribute Delete
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!* โšก๐Ÿง™โ€โ™‚๏ธโœจ")