Spaces:
Sleeping
Sleeping
File size: 4,082 Bytes
9bdc0d6 f516371 707f320 9fbf170 707f320 9fbf170 707f320 9b5b26a 9fbf170 0570e7e 9fbf170 9bdc0d6 0570e7e f516371 707f320 f516371 0570e7e f516371 0570e7e 9fbf170 0570e7e 9b5b26a 9fbf170 1c3fcb7 9fbf170 1c3fcb7 9fbf170 1c3fcb7 9fbf170 1c3fcb7 9fbf170 31c1681 8fe992b 9fbf170 0570e7e 707f320 0570e7e 707f320 9fbf170 707f320 9fbf170 0570e7e 9fbf170 aeaf039 9fbf170 7691f8d 9fbf170 0570e7e 9fbf170 0570e7e 9fbf170 9b5b26a 9fbf170 0570e7e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 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!* โก๐งโโ๏ธโจ")
|