Spaces:
Sleeping
Sleeping
# components/items.py | |
import gradio as gr | |
from image_utils import generate_item_image | |
from datetime import datetime | |
import json | |
import os | |
def save_to_gallery(content_type: str, data: dict, image_path: str): | |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
os.makedirs("gallery", exist_ok=True) | |
filename = f"gallery/{content_type}_{timestamp}.json" | |
with open(filename, "w") as f: | |
json.dump({"type": content_type, "data": data, "image": image_path}, f, indent=2) | |
def item_tab(): | |
with gr.TabItem("πͺ Loot Generator"): | |
with gr.Row(): | |
with gr.Column(): | |
name = gr.Textbox(label="Item Name", placeholder="Amulet of Twilight") | |
item_type = gr.Dropdown(label="Item Type", choices=["Weapon", "Armor", "Accessory", "Consumable", "Tool"], value="Accessory") | |
rarity = gr.Dropdown(label="Rarity", choices=["Common", "Uncommon", "Rare", "Very Rare", "Legendary"], value="Rare") | |
is_magical = gr.Checkbox(label="Magical", value=True) | |
is_cursed = gr.Checkbox(label="Cursed", value=False) | |
gen_button = gr.Button("β¨ Generate Item Image") | |
item_img = gr.Image(label="Item Image", height=512) | |
export_button = gr.Button("πΎ Save Item to Gallery") | |
export_status = gr.Textbox(label="Export Status") | |
def build_and_generate(name, item_type, rarity, is_magical, is_cursed): | |
data = { | |
"name": name, | |
"item_type": item_type, | |
"rarity": rarity, | |
"magical": is_magical, | |
"cursed": is_cursed | |
} | |
return generate_item_image(data, art_style="Concept Art") | |
gen_button.click( | |
build_and_generate, | |
inputs=[name, item_type, rarity, is_magical, is_cursed], | |
outputs=item_img | |
) | |
export_button.click( | |
lambda n, t, r, m, c, img: save_to_gallery( | |
"item", | |
{"name": n, "item_type": t, "rarity": r, "magical": m, "cursed": c}, | |
img | |
) or "β Item saved!", | |
inputs=[name, item_type, rarity, is_magical, is_cursed, item_img], | |
outputs=export_status | |
) | |